无法获取HTT presponse内容 [英] Cannot get httpResponse content

查看:102
本文介绍了无法获取HTT presponse内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我didn'y管理查询到Android模拟器Web服务(previously我有一个UnresolvedHostException,但是这是确定)。现在,我可以再往前走,我也没有在Htt的presponse的实体返回任何东西(长度为-1)。

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);

日志:

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

我不使用代理,并增加了清单中的INTERNET权限。 有什么可以为无法检索GET查询(http://www.google.com/search?q=android)的内容的原因。 非常感谢您的帮助, 吕克

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

推荐答案

不要依赖于内容长度。 有时它是无法找到的内容长度,使 只是检索返回的数据。

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}

这篇关于无法获取HTT presponse内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆