Cannot get httpResponse content

I didn'y manage to query a web service from android emulator (previously I had a UnresolvedHostException but this is ok). Now that I can go any further, I do not have anything returned in the HttpResponse's Entity (length is -1).

String url = serverUrl + resource;
Log.d(TAG, "GET: " + url);
HttpClient client = new DefaultHttpClient();
HttpGet getMethod = new HttpGet((url));
getMethod.setHeader("User-Agent", USER_AGENT);
getMethod.addHeader("Authorization", "Basic " + getCredentials());   
HttpResponse httpResponse = client.execute(getMethod);
Log.e(TAG, "RESPONSE:" + httpResponse);
Log.i(TAG,httpResponse.getStatusLine().toString());
Log.i(TAG + "1",httpResponse.getLocale().toString());
Log.i(TAG + "2",httpResponse.getHeaders(USER_AGENT).toString());
Log.i(TAG + "3",httpResponse.getEntity().toString());
Log.i(TAG + "4",httpResponse.getEntity().getContent().toString()); 
int length = (int) httpResponse.getEntity().getContentLength();
Log.e(TAG + "5", "LENGTH:" + length);

The logs:

D/HttpServices(  275): GET: http://www.google.com/search?q=android
D/dalvikvm(  275): GC freed 2992 objects / 217016 bytes in 103ms
E/HttpServices(  275): RESPONSE:org.apache.http.message.BasicHttpResponse@43ca4920
I/HttpServices(  275): HTTP/1.1 200 OK
I/HttpServices1(  275): en_US
I/HttpServices2(  275): [Lorg.apache.http.Header;@43c44e10
I/HttpServices3(  275): org.apache.http.conn.BasicManagedEntity@43c40be8
I/HttpServices4(  275): org.apache.http.conn.EofSensorInputStream@43c51c60
E/HttpServices5(  275): LENGTH:-1

I'm not behind a proxy and have added the INTERNET permission in the manifest. What can be the reason for not retrieving the content of the get query (http://www.google.com/search?q=android). Thanks a lot for your help, Luc


Asked by: Briony247 | Posted: 24-01-2022






Answer 1

Don't rely on the content length. Sometimes it is unable to find the content length so just retrieve the returned data

URL url = new URL("xyz.com";); 
URLConnection conn = url.openConnection();
conn.setRequestProperty("Authorization", "Basic"+getcredentials());
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//read d response till d end
while ((line = rd.readLine()) != null) {//process the line response}

Answered by: Maddie224 | Posted: 25-02-2022



Similar questions

android - How do I parse JSON from a Java HTTPResponse?

I have an HttpResponse object for a web request I just made. The response is in the JSON format, so I need to parse it. I can do it in an absurdly complex way, but it seems like there must be a better way. Is this really the best I can do? HttpResponse response; // some response object Reader in = new BufferedReader( new InputStreamReader(response.getEntity().getContent(), "UTF-8"))...


android - How do I parse JSON from a Java HTTPResponse?

I have an HttpResponse object for a web request I just made. The response is in the JSON format, so I need to parse it. I can do it in an absurdly complex way, but it seems like there must be a better way. Is this really the best I can do? HttpResponse response; // some response object Reader in = new BufferedReader( new InputStreamReader(response.getEntity().getContent(), "UTF-8"))...


android - Display HttpResponse (string from Handler) in new WebView

I have the following code in a form's submit button onClickListener: String action, user, pwd, user_field, pwd_field; action = "theURL"; user_field = "id"; pwd_field = "pw"; user = "username"; pwd = "password!!"; List<NameValuePair> myList = new ArrayList<NameValuePair>(); myList.add(new BasicNameValuePair(user_field, user)); my...


android - Is there a limit to the content length for HTTPResponse?

Running my unit tests I have found that on android I am not receiving the complete atom document while the code works ok for JSE. The URL is: https://www.googleapis.com/buzz/v1/activities/@me/@consumption?max-results=25 The request headers are: authorization: [OAuth oauth_token="validToken"...


HTTPResponse debugging with the android emulator

I would like to know if there is anyway to debug the HTTP response with the android emulator. my logs show that the response is not arriving complete to the application and i would like to know if it is the server that does not send the complete response or if my application is not processing the response completely. I tried hooking up Fiddler and Blurp proxy, it actually works with Blurp proxy but only for HTTP an...


android - i am getting httpresponse exception : unauthorised for the twitter application + oauth.. how to solve it?

please help me. i need a code that will tweet using oauth + twitter api... http://www.youtube.com/watch?v=25o0b2aEw0E i have used this project and i m getting error 08-25 11:47:32.747: WARN/System.err(2029): org.apache.http.client.HttpResponseException: Unauthorized 08-25 11:47:32.747: WARN/System.err(2029): at o...


android - How can I get the URL of an HTTPResponse

how can I get the URL of an HTTPResponse? I tried: response.getHeaders("Locations") But I obtained: 11-15 21:14:03.355: INFO/System.out(880): [Lorg.apache.http.Header;@43ea9568


java - android 1.6 httpresponse doesn't include a location header

i have an android 1.6 application that i'm trying to get the location header of a HTTP GET response. however, when i call getLastHeader("location") it returns null. i even go into the response variable via debugger and there is no location header sent. i need to do this on an arbitrary site, but using google for testing purposes still produces the mishap. i thought location was a default header to be read? does android 1.6...


java - unable to retrieve all data from httpResponse

After I try different way, i cannot able to retrieve all data from HttpResponse. Here is my code : private static final DefaultHttpClient httpClient = new DefaultHttpClient(); public void get() throws Exception { HttpPost httpPost = new HttpPost("https://dt.abc.net/abc.exe"); httpPost.addHeader("Accept", "text/xml"); httpPost.setHeader("Content-Type","application/xml;c...


android - How can I read a bytearray from an HttpResponse?

I'm making an http connection using following code: HttpClient client = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpResponse response = client.execute(httpPost); I want to read a bytearray from the response object. How would I do that?


How to parse a xml type HTTPResponse in Android

I have an android application where I use POST method to get a response. here is my code: .......................................... HttpResponse httpResponse = httpclient.execute(httppost); HttpEntity resEntity = httpResponse.getEntity(); this works fine and i get a response in xml format but i want to parse that xml file and get the node values. i tried this : DocumentBu...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top