Android HttpClient获取json数据丢失 androidjsonhttpHttpClient 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你用电脑的浏览器IE测试一下那个URL,看看返回什么,先排除后台的原因 也就是才能取到一半的json数据,浏览器里测试是好的,能得到全部数据,我用HttpClient就只能取到接口返回数据的一半。 试一下如下代码看看:HttpClient hc = new DefaultHttpClient(); try { HttpResponse ht = hc.execute(httpGet); if(ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ HttpEntity he = ht.getEntity(); InputStream is = he.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String response = ""; String readLine = null; while((readLine =br.readLine()) != null){ //response = br.readLine(); response = response + readLine; } is.close(); br.close(); //String str = EntityUtils.toString(he); System.out.println("========="+response); return response; }else{ return "error"; } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); return "exception"; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return "exception"; }另外何不考虑使用HttpUrlConnection?这个帖子对比了HttpUrlConnection和HttpClient,我觉得说的不错http://http://blog.csdn.net/huzgd/article/details/8712187 哥们,你给的这个函数我使用后还是不行,和原来的效果一样。为什么会这样呢?求解答啊,,,???????????public static String test(String url,List<NameValuePair> params){ HttpParams httpParams; HttpClient httpClient; HttpPost httpRequest = new HttpPost(url); String response = "doPostError"; httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, 4*1000); HttpConnectionParams.setSoTimeout(httpParams, 6*1000); HttpConnectionParams.setSocketBufferSize(httpParams, 8192); HttpClientParams.setRedirecting(httpParams, true); String userAgent ="Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2) Gecko/20100115 Firefox/3.6"; HttpProtocolParams.setUserAgent(httpParams, userAgent); httpClient = new DefaultHttpClient(httpParams); try { if (params != null) { httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); } httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_0); httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET,"UTF-8"); HttpResponse httpResponse = httpClient.execute(httpRequest); if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity httpEntity=httpResponse.getEntity(); InputStream is = httpEntity.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String readLine = null; while((readLine =br.readLine()) != null){ //response = br.readLine(); response = response + readLine; } is.close(); br.close(); } else { response = "Error Response: " + httpResponse.getStatusLine().toString();//获取响应码 } } catch (ClientProtocolException e) { // strResult = e.getMessage().toString(); e.printStackTrace(); } catch (IOException e) { // strResult = e.getMessage().toString(); e.printStackTrace(); }catch (Exception e) { // strResult = e.getMessage().toString(); e.printStackTrace(); } System.out.println("strResult =" + response); return response; } while((readLine =br.readLine()) != null){ 是不是你的循环语句写的有问题啊,看着别扭呢HttpEntity httpEntity = new UrlEncodedFormEntity(pairs); httpPost.setEntity(httpEntity); HttpResponse httpResponse = httpClient.execute(httpPost); if (httpResponse.getStatusLine().getStatusCode() == 200) { HttpEntity entity = httpResponse.getEntity(); return EntityUtils.toString(entity, "utf-8");用这个,直接转成String试试。 你看下回来的那个String,是否完整的,,, 安卓访问浏览器的cookie或者localstorage phonegap的调用地理位的gps接口,还有人成功调用过啊,为什么我在模拟器上测试没有任何返回信息呢,求大神 像瑞星卡卡助手那样的弹出动画怎样实现? 请教大家一个关于android双卡拨号的问题 有没有现在做Android开发工作的啊? 如何让桌面壁纸每秒更换一次 堆变量与栈变量的区别(问题更像是java语言的基础问题) 反编译失败,请看看为什么 手机通讯录号码归属地批量查询的问题 请问怎样Hook安卓内核函数? eclipse 导入带有C++的android项目。 android EdiText文本滑动
HttpClient hc = new DefaultHttpClient();
try {
HttpResponse ht = hc.execute(httpGet);
if(ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
HttpEntity he = ht.getEntity();
InputStream is = he.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String response = "";
String readLine = null;
while((readLine =br.readLine()) != null){
//response = br.readLine();
response = response + readLine;
}
is.close();
br.close();
//String str = EntityUtils.toString(he);
System.out.println("========="+response);
return response;
}else{
return "error";
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "exception";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "exception";
}
另外何不考虑使用HttpUrlConnection?这个帖子对比了HttpUrlConnection和HttpClient,我觉得说的不错
http://http://blog.csdn.net/huzgd/article/details/8712187
HttpParams httpParams;
HttpClient httpClient;
HttpPost httpRequest = new HttpPost(url);
String response = "doPostError";
httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 4*1000);
HttpConnectionParams.setSoTimeout(httpParams, 6*1000);
HttpConnectionParams.setSocketBufferSize(httpParams, 8192);
HttpClientParams.setRedirecting(httpParams, true);
String userAgent ="Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2) Gecko/20100115 Firefox/3.6";
HttpProtocolParams.setUserAgent(httpParams, userAgent);
httpClient = new DefaultHttpClient(httpParams);
try {
if (params != null) {
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
}
httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_0);
httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET,"UTF-8");
HttpResponse httpResponse = httpClient.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity httpEntity=httpResponse.getEntity();
InputStream is = httpEntity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String readLine = null;
while((readLine =br.readLine()) != null){
//response = br.readLine();
response = response + readLine;
}
is.close();
br.close();
} else {
response = "Error Response: " + httpResponse.getStatusLine().toString();//获取响应码
}
} catch (ClientProtocolException e) {
// strResult = e.getMessage().toString();
e.printStackTrace();
} catch (IOException e) {
// strResult = e.getMessage().toString();
e.printStackTrace();
}catch (Exception e) {
// strResult = e.getMessage().toString();
e.printStackTrace();
} System.out.println("strResult =" + response); return response;
}
httpPost.setEntity(httpEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = httpResponse.getEntity();
return EntityUtils.toString(entity, "utf-8");
用这个,直接转成String试试。