我的应用中存在问题。首先,我将描述它是如何工作的。它向服务器发送请求(这不是我创建的),并且在响应中我得到JSON字符串(它在JSONarray中不是那么大的平均10条记录),在一个参数中有我下载的图片的URL和保存为每个JSON对象的位图。总结一下,我下载了一个类似于:
的JSON{
"id":"125",
"description":"desc",
"type":"type",
"state":"state",
"date":"2012-09-22 10:40:46.0",
"lat":"52.321911",
"lng":"19.464111",
"author":"user",
"photo":"GetImage?size=small&id=0",
"comments":[
]
}
例如,x 10,然后我从每个对象的URL“照片”图像下载。问题在于执行时间,真的很长,不应该是大数据。这是我如何做到这一点:下载图像的AsyncClass:
private class HttpGetImage extends AsyncTask<Object, Integer, Integer>
{
public boolean ready = false;
public boolean success = false;
@Override
protected Integer doInBackground(Object... obj) {
DefaultHttpClient client = new MyHttpClient(ViewEdit.this);
HttpGet get = new HttpGet(url+photoUrl);
HttpResponse getResponse = null;
try {
getResponse = client.execute(get);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ready=true; return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ready=true; return null;
}
HttpEntity responseEntity = getResponse.getEntity();
BufferedHttpEntity httpEntity = null;
try {
httpEntity = new BufferedHttpEntity(responseEntity);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
ready=true; return null;
}
InputStream imageStream = null;
try {
imageStream = httpEntity.getContent();
m_orders.get((Integer)obj[1])
.setOrderBitmap(BitmapFactory.decodeStream(imageStream));
success = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ready=true; return null;
} finally {
try {
imageStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ready=true; return null;
}
}
ready = true;
return null;
}
}
和jsonString的AsyncClass:
private class HttpGetNotifications extends AsyncTask<Double, Integer, Integer>
{
public boolean ready = false;
public boolean success = false;
@Override
protected Integer doInBackground(Double... params)
{
DefaultHttpClient client = new MyHttpClient(ViewEdit.this);
HttpGet get = new HttpGet(url + Double.toString(params[0]) + "&longitude=" + Double.toString(params[1]) + "&radius="+distance);
HttpResponse getResponse = null;
try {
getResponse = client.execute(get);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ready=true; return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ready=true; return null;
}
HttpEntity responseEntity = getResponse.getEntity();
String entityContents="";
try {
entityContents = EntityUtils.toString(responseEntity);
loadNotifications(entityContents);
success = true;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ready=true; return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ready=true; return null;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ready=true; return null;
}
ready = true;
return null;
}
public void loadNotifications(String jsonstring) throws JSONException
{
JSONObject jsonResponse = new JSONObject(jsonstring);
JSONArray notifi = jsonResponse.getJSONArray("notifications");
for (int i =0, count = notifi.length(); i <count; i++){
//storage of data
}
}
}
也许你们有个想法如何优化代码以减少执行时间?
答案 0 :(得分:0)
进行一些剖析以找出花时间的东西。在每个AsyncTasks中:
private long time0, time1;
@Override protected void onPreExecute() {
time0 = System.currentTimeMillis();
}
@Override protected void onPostExecute(HttpResponse response) {
time1 = System.currentTimeMillis();
long deltaT = (time1 - time0);
Log.d(TAG, "Execute took "+deltaT+"ms");
}
然后从那里开始。