tags:

views:

428

answers:

1

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

+1  A: 

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}
Bohemian
Hello, thanks for your quick reply. I tryed your code but was not able to understand what "conn" is. Do you get the InputStream directly from the HttpResponse ?Thanks a lot for your help,Luc
Luc
ok. I m makin a urlconnection object "conn" like:URL url = new URL("http://www.xyz.com");URLConnection conn = url.openConnection();yes i just open the input stream after makin the connection n get my response.So I guess it might be different from the class u r using.Anyhow u can try using these classes :java.net.URL;java.net.URLConnection;
Bohemian
chk out the better code above. I have edited the previous one
Bohemian
Ok, I'll try this when I get home. Just one thing that I do not really understand: I do not use the HttpGet / HttpResponse classes at all in this case ? or is this done behind the scene ?Thanks a lot
Luc
Hi,The solution you exposed is correct but I got the InputStream directly from the httpResponse's content. This seems to work fine now.Thanks a lot,Luc
Luc