Android 如何在类似安卓的I-Phone中让webservice响应更快
我正在成功地从web服务获取数据。但与iPhone相比,它花费的时间更多。我不知道确切的原因 是否有人可以在android中快速获取数据(如I-Phone)… 我的代码是:Android 如何在类似安卓的I-Phone中让webservice响应更快,android,performance,android-service,webservice-client,Android,Performance,Android Service,Webservice Client,我正在成功地从web服务获取数据。但与iPhone相比,它花费的时间更多。我不知道确切的原因 是否有人可以在android中快速获取数据(如I-Phone)… 我的代码是: HttpResponse res = null; HttpParams httpParameters = new BasicHttpParams(); int timeout1 = 1000*8; int timeout2 = 1000*8; HttpConne
HttpResponse res = null;
HttpParams httpParameters = new BasicHttpParams();
int timeout1 = 1000*8;
int timeout2 = 1000*8;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeout1);
HttpConnectionParams.setSoTimeout(httpParameters, timeout2);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
//HttpClient httpClient=new DefaultHttpClient();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("Id", user_Id));
nameValuePairs.add(new BasicNameValuePair("kAreaNo",Chapter_Id));
nameValuePairs.add(new BasicNameValuePair("QuestionCount",question_Count+""));
nameValuePairs.add(new BasicNameValuePair("StartingQno",start_From+""));
String paramString = URLEncodedUtils.format(nameValuePairs, "utf-8");
URL+=paramString;
HttpGet post=new HttpGet(URL);
try {
res=httpClient.execute(post);
Log.i("Response=",res.getEntity().toString()+"Response aa");
Log.i("Response=",res.getStatusLine().getStatusCode()+"Response aa");
}
catch (Exception e) {
// TODO: handle exception
}
try{
if (res.getEntity() != null) {
String retSrc = EntityUtils.toString(res.getEntity());
} catch(Exception e){
......
}
HttpResponse res=null;
HttpParams httpParameters=新的BasicHttpParams();
int timeout1=1000*8;
int timeout2=1000*8;
HttpConnectionParams.setConnectionTimeout(httpParameters,timeout1);
HttpConnectionParams.setSoTimeout(httpParameters,timeout2);
HttpClient HttpClient=新的默认HttpClient(httpParameters);
//HttpClient HttpClient=新的DefaultHttpClient();
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“Id”,用户Id));
添加(新的BasicNameValuePair(“kAreaNo”,第十一章));
添加(新的BasicNameValuePair(“问题计数”,问题计数+);
添加(新的BasicNameValuePair(“StartingQno”,start_From+”);
String paramString=URLEncodedUtils.format(nameValuePairs,“utf-8”);
URL+=paramString;
HttpGet post=新的HttpGet(URL);
试一试{
res=httpClient.execute(post);
Log.i(“Response=,res.getEntity().toString()+“Response aa”);
Log.i(“Response=,res.getStatusLine().getStatusCode()+“Response aa”);
}
捕获(例外e){
//TODO:处理异常
}
试一试{
if(res.getEntity()!=null){
字符串retSrc=EntityUtils.toString(res.getEntity());
}捕获(例外e){
......
}
使用Traceview可以准确地确定你在哪里花费时间。谢谢……好的,我会尝试一下。但我从来没有使用过Traceview。据我们所知,问题不在上面显示的代码中,而是在其他地方。使用Traceview可以准确地确定你在哪里花费时间。没有任何东西可以告诉我们为什么会是slo比等效的iOS调用低。如果响应较大,则添加gzip头可能会有所帮助。如果添加“Accept Encoding:gzip”头,则服务器将使用gzip(如果它具有此功能且已启用)。大多数现代web服务器支持gzip编码。但在尝试随机修复之前,我仍然会分析您的代码。