2012-11-14 117 views
0

我在我的應用程序有問題。一開始我會介紹它是如何工作的。它向服務器發送請求(不是由我創建的),並且在響應中我得到了JSON字符串(它在JSONarray中的平均10個記錄不是那麼大),在一個參數中有我下載的圖片的URL和保存爲每個JSON對象的位圖。總結一下,我下載了一個JSON,如下所示:Android真的很長的HTTP請求執行時間

{ 
    "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; 
     } 
    } 

和AsyncClass爲jsonString:

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

進行一些分析以找出需要花費的時間。在每個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"); 
} 

然後從那裏開始。