Cannot get httpResponse content

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Cannot get httpResponse content

Postby lucj » Tue Dec 29, 2009 10:47 am

Hello, 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
lucj
Freshman
Freshman
 
Posts: 5
Joined: Tue Dec 22, 2009 10:20 pm

Top

Postby jwei512 » Wed Dec 30, 2009 4:17 am

Hmm, I think it might just be a problem with the fact that you're converting a long to an int, namely at this line:

int length = (int) httpResponse.getEntity().getContentLength();

I've had problems with this before because of the way longs get converted to ints (see http://www.coderanch.com/t/374865/Java- ... nt-when-it for more details) but for now just try doing something like:

long length = httpResponse.getEntity().getContentLength();

and printing that out.
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Postby lucj » Wed Dec 30, 2009 9:35 am

Hello,
I an not sure this is link to casting error. Yesterday I changed something which seems to clear this issue.
In fact calling getEntity().getContent() seems to consume the content.
Getting the content as InputStream seems to fix this.
I do not have the code right here but let me know if you want it.
thanks a lot,
Regards,
Luc
lucj
Freshman
Freshman
 
Posts: 5
Joined: Tue Dec 22, 2009 10:20 pm

Postby jwei512 » Wed Dec 30, 2009 8:26 pm

Hey lucj,

Well if it's not a casting thing, then maybe this will help:

http://thinkandroid.wordpress.com/2009/ ... presponse/

I wrote a little something about it and attached some sample code.

Hope this gets the job done!
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Postby lucj » Wed Dec 30, 2009 10:26 pm

hello Jwei,
thanks a lot, that really helps.
I got stuck in the Content already comsumed, but that's ok now.
great job guy,
Lucj
lucj
Freshman
Freshman
 
Posts: 5
Joined: Tue Dec 22, 2009 10:20 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 3 guests