Android BasicHttpEntity - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android BasicHttpEntity class.

This code example shows how to use the following methods:setChunked, setContent, setContentLength, setContentEncoding
 
    return (T) gson.fromJson(response.getResponseBody(), type);
  } 
 
  private void setFormUrlEncodedBody(HttpEntityEnclosingRequestBase request, String requestBody) {
    BasicHttpEntity entity = new BasicHttpEntity();
 
    try { 
      byte[] contentBytes = requestBody.getBytes(Charsets.UTF_8.name());
 
      ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(contentBytes);
      System.out.println(">>>>>>>" + Strings.toString(byteArrayInputStream));
 
      entity.setContent(new ByteArrayInputStream(contentBytes));
      entity.setContentLength(contentBytes.length);
      entity.setContentEncoding(Charsets.UTF_8.name());
      entity.setChunked(false);
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException("Unable to set request body.", e);
    } 

6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:setChunked, setContentEncoding, setContentType, setContent, setContentLength
            return new ContentLengthInputStream(inbuffer, len);
        } 
    } 
 
    protected HttpEntity prepareInput(final HttpMessage message) throws HttpException {
        final BasicHttpEntity entity = new BasicHttpEntity();
 
        final long len = this.incomingContentStrategy.determineLength(message);
        final InputStream instream = createInputStream(len, this.inbuffer);
        if (len == ContentLengthStrategy.CHUNKED) {
            entity.setChunked(true);
            entity.setContentLength(-1);
            entity.setContent(instream);
        } else if (len == ContentLengthStrategy.IDENTITY) {
            entity.setChunked(false);
            entity.setContentLength(-1);
            entity.setContent(instream);
        } else { 
            entity.setChunked(false);
            entity.setContentLength(len);

6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:setContentEncoding, setContentType, setContent, setContentLength
     * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}. 
     * @param connection 
     * @return an HttpEntity populated with data from <code>connection</code>. 
     */ 
    private static HttpEntity entityFromConnection(HttpURLConnection connection) {
        BasicHttpEntity entity = new BasicHttpEntity();
        InputStream inputStream;
        try { 
            inputStream = connection.getInputStream();
        } catch (IOException ioe) {
            inputStream = connection.getErrorStream();
        } 
        entity.setContent(inputStream);
        entity.setContentLength(connection.getContentLength());
        entity.setContentEncoding(connection.getContentEncoding());
        entity.setContentType(connection.getContentType());
        return entity;
    } 
 
    /** 
     * Opens an {@link HttpURLConnection} with parameters. 
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:setContentEncoding, setContentType, setContent
This code example shows how to use the following methods:setContentEncoding, setContentType
    } 
 
    public void testResponseContentEntityContentTypeAndEncoding() throws Exception { 
        HttpContext context = new BasicHttpContext(null);
        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
        BasicHttpEntity entity = new BasicHttpEntity();
        entity.setContentEncoding("whatever");
        entity.setContentType("whatever");
        response.setEntity(entity);
        ResponseContent interceptor = new ResponseContent();
        interceptor.process(response, context);
        Header h1 = response.getFirstHeader(HTTP.CONTENT_TYPE);
        assertNotNull(h1);
        assertEquals("whatever", h1.getValue());
        Header h2 = response.getFirstHeader(HTTP.CONTENT_ENCODING);
        assertNotNull(h2);
        assertEquals("whatever", h2.getValue());
    } 
 
    public void testResponseContentInvalidInput() throws Exception { 
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
    @Test 
    public void testSetResponseEntity() { 
        final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
        Assert.assertNull(response.getEntity());
 
        final HttpEntity entity = new BasicHttpEntity();
        response.setEntity(entity);
        Assert.assertTrue(entity == response.getEntity());
    } 
 
    @Test 
    public void testDefaultRequestConstructors() { 
        HttpRequest request = new BasicHttpRequest("WHATEVER", "/");
        Assert.assertNotNull(request.getProtocolVersion());
        Assert.assertEquals("WHATEVER", request.getRequestLine().getMethod());
        Assert.assertEquals("/", request.getRequestLine().getUri());
 
        request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_0);
        Assert.assertEquals(HttpVersion.HTTP_1_0, request.getProtocolVersion());
        Assert.assertEquals("GET", request.getRequestLine().getMethod());
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:setContent, setContentLength
            } 
        } else if (resource != null) {
            if (file != null || value != null) {
                throw new BuildException("Only one of 'file' or 'value' attribute or nested resource is supported"); 
            } 
            BasicHttpEntity basicEntity = new BasicHttpEntity();
            try { 
                basicEntity.setContent(resource.getInputStream());
            } catch (IOException e) {
                throw new BuildException("I/O error while getting a stream out of the resource " + resource, e);
            } 
            basicEntity.setContentLength(resource.getSize());
            entity = basicEntity;
        } else { 
            throw new BuildException("At least one of 'file' or 'value' attribute or nested resource is required"); 
        } 
        if (contentType != null) {
            entity.setContentType(contentType);
        } 
        if (contentEncoding != null) {
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:setContentType
        Assert.assertNull(ContentType.get(null));
    } 
 
    @Test 
    public void testExtractNullContentType() throws Exception { 
        BasicHttpEntity httpentity = new BasicHttpEntity();
        httpentity.setContentType((Header)null);
        Assert.assertNull(ContentType.get(httpentity));
    } 
 
    @Test 
    public void testExtract() throws Exception { 
        BasicHttpEntity httpentity = new BasicHttpEntity();
        httpentity.setContentType(new BasicHeader("Content-Type", "text/plain; charset = UTF-8"));
        ContentType contentType = ContentType.get(httpentity);
        Assert.assertNotNull(contentType);
        Assert.assertEquals("text/plain", contentType.getMimeType());
        Assert.assertEquals("UTF-8", contentType.getCharset().name());
    } 
 
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:setContentType, setContent
		InputStream in = null;
		try { 
			URI uri = new URI(url);
			if (HTTP_METHOD_POST.equalsIgnoreCase(method)) {
				HttpPost httPost = new HttpPost();
				BasicHttpEntity basicEntity = new BasicHttpEntity();
				in = new ByteArrayInputStream(json.getBytes());
				basicEntity.setContent(in);
				basicEntity.setContentType(contentType);
				// StringEntity stringEntity = new StringEntity(json); 
				// stringEntity.setContentType(contentType); 
				// httPost.setEntity(stringEntity); 
				httPost.setEntity(basicEntity);
				request = httPost;
			} else if (HTTP_METHOD_GET.equalsIgnoreCase(method)) {
				request = new HttpGet();
			} 
			request.setURI(uri);
		} catch (URISyntaxException e) {
			e.printStackTrace();
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:setContent, setContentLength
    } 
 
    @Test 
    public void testMaxIntContentToByteArray() throws Exception { 
        final byte[] content = "Message content".getBytes("ISO-8859-1");
        final BasicHttpEntity httpentity = new BasicHttpEntity();
        httpentity.setContent(new ByteArrayInputStream(content));
        httpentity.setContentLength(Integer.MAX_VALUE + 100L);
        try { 
            EntityUtils.toByteArray(httpentity);
            Assert.fail("IllegalArgumentException should have been thrown");
        } catch (final IllegalArgumentException ex) {
            // expected 
        } 
    } 
 
    @Test 
    public void testUnknownLengthContentToByteArray() throws Exception { 
        final byte[] bytes = "Message content".getBytes("ISO-8859-1");
        final BasicHttpEntity httpentity = new BasicHttpEntity();
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java

Related Android BasicHttpEntity Questions & Answers:

See Code Examples for Android BasicHttpEntity Methods: