Android json和http客户端速度慢
我有一个从url解析的json解析器,该应用程序在WiFi上运行没有问题,速度足够快,但在移动网络上速度非常慢,大部分时间不工作。Android json和http客户端速度慢,android,httpclient,Android,Httpclient,我有一个从url解析的json解析器,该应用程序在WiFi上运行没有问题,速度足够快,但在移动网络上速度非常慢,大部分时间不工作。 移动网络的速度足够快,whatsapp工作得很快,所以我不知道问题是否与Json解析器或http客户端有关 // Async Task to access the web public String makeHttpRequest(String url, String method, List<NameValuePair>
移动网络的速度足够快,whatsapp工作得很快,所以我不知道问题是否与Json解析器或http客户端有关
// Async Task to access the web
public String makeHttpRequest(String url, String method,
List<NameValuePair> params) {
HttpParams params1 = new BasicHttpParams();
params1.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpClient httpclient = new DefaultHttpClient(params1);
HttpPost httppost = new HttpPost(url);
try {
httppost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return JSON String
return jsonResult;
}
//访问web的异步任务
公共字符串makeHttpRequest(字符串url、字符串方法、,
列表参数){
HttpParams params1=新的BasicHttpParams();
params1.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);
HttpClient HttpClient=新的默认HttpClient(参数1);
HttpPost HttpPost=新的HttpPost(url);
试一试{
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse response=httpclient.execute(httppost);
jsonResult=inputStreamToString(
response.getEntity().getContent()).toString();
}捕获(不支持DencodingException e1){
e1.printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
//返回JSON字符串
返回jsonResult;
}
使用任何第三方工具,这些工具具有内置功能,可以快速提出请求
:
HTTP请求发生在UI线程之外
自动gzip响应解码支持超快速请求
最后
Volley是一个HTTP库,它使Android应用程序的联网更容易,最重要的是,更快
您可以从中看到这些信息,正如我所看到的,您将所有这些代码放在一个AsyncTask中,因为AsyncTask在后台运行,所以它不会减慢您的应用程序。一些简单的原因可能是,下载的数据花费了太多的时间,应用程序正在等待异步任务完成;你的数据网络很慢;您的httpServer响应不够快。不,数据只有5 kb,网络速度不慢,whatsapp工作正常