Android HttpResponse - 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 HttpResponse class.

This code example shows how to use the following methods:getEntity, getStatusLine
    String uri = BASE_PRODUCT_URI + encodedProductID;
 
    HttpUriRequest head = new HttpGet(uri);
    AndroidHttpClient client = AndroidHttpClient.newInstance(null);
    HttpResponse response = client.execute(head);
    int status = response.getStatusLine().getStatusCode();
    if (status != 200) {
      return; 
    } 
 
    String content = consume(response.getEntity());
    Matcher matcher = PRODUCT_NAME_PRICE_PATTERN.matcher(content);
    if (matcher.find()) {
      append(matcher.group(1));
      append(matcher.group(2));
    } 
    setLink(uri);
  } 
 
  private static String consume(HttpEntity entity) {
9
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getEntity, getStatusLine
 
    private Object handleResponse(HttpResponse response) throws HttpException, IOException {
        if (response == null) {
            throw new HttpException("response is null");
        } 
        StatusLine status = response.getStatusLine();
        int statusCode = status.getStatusCode();
        if (statusCode < 300) {
            HttpEntity entity = response.getEntity();
            Object responseBody = null;
            if (entity != null) {
                lastUpdateTime = SystemClock.uptimeMillis();
                if (isDownloadingFile) {
                    String responseFileName = autoRename ? OtherUtils.getFileNameFromHttpResponse(response) : null;
                    responseBody = mFileDownloadHandler.handleEntity(entity, this, fileSavePath, autoResume, responseFileName);
                } else { 
 
                    // Set charset from response header info if it's exist. 
                    String responseCharset = OtherUtils.getCharsetFromHttpResponse(response);
                    charset = TextUtils.isEmpty(responseCharset) ? charset : responseCharset;

8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getEntity, getStatusLine
  @Test 
  public void testExecute() throws IOException { 
    AndroidHttpClient client = AndroidHttpClient.newInstance("foo");
    Robolectric.addPendingHttpResponse(200, "foo"); 
    HttpResponse resp = client.execute(new HttpGet("/foo"));
    assertThat(resp.getStatusLine().getStatusCode()).isEqualTo(200);
    assertThat(Strings.fromStream(resp.getEntity().getContent())).isEqualTo("foo");
  } 
} 
8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getEntity
            HttpConnectionParams.setSoTimeout(httpParams, SOCKET_TIMEOUT);
            HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIMEOUT);
            HttpClient httpClient = new DefaultHttpClient(httpParams);
            HttpGet httpget = new HttpGet(TIMER_URL);
            HttpResponse response = httpClient.execute(httpget);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                time = Long.parseLong(EntityUtils.toString(entity),10);
                logger.debug("Got remote timestamp {}", time);
            } 
        } catch (IOException e) {
            logger.warn("Error obtaining the synchronized remote timestamp, defaulting to the local one",e);
        } 
         
        if (time == -1) {
            time = System.currentTimeMillis();
        } 
         
        return Long.toString(time / 1000L);
    } 
8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getEntity, getStatusLine
            req.addHeader(
                    "x-wap-profile", 
                    "http://www.openmobilealliance.org/tech/profiles/UAPROF/ccppschema-20021212#"); 
 
            HttpResponse response = client.execute(req);
            StatusLine status = response.getStatusLine();
            if (status.getStatusCode() != 200) { // HTTP 200 is success.
                if (DEBUG) Log.d(TAG, "HTTP error: " + status.getReasonPhrase());
                return null; 
            } 
 
            HttpEntity entity = response.getEntity();
            byte[] body = null;
            if (entity != null) {
                try { 
                    if (entity.getContentLength() > 0) {
                        body = new byte[(int) entity.getContentLength()];
                        DataInputStream dis = new DataInputStream(entity.getContent());
                        try { 
                            dis.readFully(body);
8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:containsHeader, getAllHeaders
        } 
    } 
 
    private static void checkResponseForHeader(HttpResponse response, String headerName) {
        Assert.assertTrue("response doesn't contain header '" + headerName + "', all response headers=" +
                showHeaders(response.getAllHeaders()), response.containsHeader(headerName));
    } 
 
    private static String showHeaders(final org.apache.http.Header[] headers) {
        StringBuilder stringBuilder = new StringBuilder();
        try (Formatter result = new Formatter(stringBuilder)) {
            for (Header header : headers) {
                result.format("{name=%s, value=%s}, ", header.getName(), header.getValue());
            } 
            return result.toString();
        } 
    } 
} 
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getEntity, getStatusLine
      request.setParams(client.getParams());
      request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
//      request.addHeader("x-wap-profile", "http://www.htcmms.com.tw/Android/Common/nexusone/ua-profile.xml"); 
      request.addHeader("x-wap-profile", "http://www.google.com/oha/rdf/ua-profile-kila.xml");
      HttpResponse response = client.execute(target, request);
      StatusLine status     = response.getStatusLine();
 
      if (status.getStatusCode() != 200)
        throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase());
 
      return parseResponse(response.getEntity());
    } catch (URISyntaxException use) {
      Log.w("MmsSendHelper", use);
      throw new IOException("Couldn't parse URI.");
    } finally { 
      if (client != null)
        client.close();
    } 
  } 
 
8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getStatusLine
                return false; 
            else // in case substring is defined, rethrow exception so, we can easier analyze the cause 
                throw new Exception(e);
        } 
 
        int statusCode = hr.getStatusLine().getStatusCode();
        if (statusCode != 200) {
            log.info("statusCode not expected. statusCode=" + statusCode + ", URL=" + url);
            return false; 
        } 
 
        if (responseSubstring == null) {
            // this indicates that negative test had problems 
            log.info("statusCode==200 on URL=" + url);
            return true; 
        } 
 
        String response = EntityUtils.toString(hr.getEntity());
        if (response.indexOf(responseSubstring) != -1) {
            return true; 
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getEntity
	} 
 
	private InputStream getUrl(String url) throws Exception {
		HttpGet request = new HttpGet(url);
		HttpResponse response = executeRequest(request);
		HttpEntity entity = response.getEntity();
		if (entity != null) {
			InputStream instream = entity.getContent();
            return instream;
		} 
		return null; 
	} 
 
    private void putUrl(String url, String data) throws Exception {
        HttpPut put = new HttpPut(url);
        put.setEntity(new StringEntity(data));
        HttpResponse response = executeRequest(put);
    } 
 
	protected HttpResponse executeRequest(HttpUriRequest request)
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getFirstHeader, getEntity, getStatusLine
        throws JSONException, InvalidRequestException, 
               InvalidClientException, InvalidGrantException, 
               UnauthorizedClientException, UnsupportedGrantTypeException, 
               InvalidScopeException, OAuthException { 
 
        StatusLine statusLine = this.httpResponse.getStatusLine();
        this.statusCode = statusLine.getStatusCode();
        try { 
            getStringEntity(); 
        } catch (IOException ex) {
            Logger.getLogger(Response.class.getName()).log(Level.SEVERE, null, ex);
        } 
 
        if (parseStringEntityForJson()) { 
            JsonParser.parseForErrors(jsonResponse);
        } else if (parseStringEntityForParameters()) { 
        } 
 
        switch (statusCode) {
            case (200): { 

7
CodeRank
Experience pair programming with AI  Get Codota for Java

Related Android HttpResponse Questions & Answers:

Other Android HttpResponse Examples

1:	try {
2:		HttpResponse response = httpClient.execute(httpPost);
3:	
4:		// writing response to log

Read More

6:				HttpPost httpPost = new HttpPost(url);
7:	
8:				HttpResponse httpResponse = httpClient.execute(httpPost);
9:				HttpEntity httpEntity = httpResponse.getEntity();
10:				xml = EntityUtils.toString(httpEntity);

Read More

47:			// Making HTTP Request
48:			try {
49:				HttpResponse response = httpClient.execute(httpPost);
50:	
51:				// writing response to log

Read More

See Code Examples for Android HttpResponse Methods: