2014-02-11 42 views
0

我有成千上萬的JSON響應數據的,我解析和使用基本適配器顯示在自定義列表視圖這個數據,但問題是,它花費的時間太長顯示如何加快Android中的JSON解析?

我的AsyncTask在我解析JSON是如下

private class JsonParseTask extends AsyncTask<Void,Void,Void>{ 

    private boolean network_status; 
    private AsyncTask<Void,Void,Void> thisTask; 
    private boolean showServerError=false; 
    private boolean showConnectionError=false; 
    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     thisTask=this; 
     network_status=CheckNetworkStatus.isConnected(mContext); 
     if(!network_status) 
     { 
      showAlertDialog(mContext,"No Internet Connection" ,"You don't have internet connection.",false); 
      thisTask.cancel(true); 
     } 
     super.onPreExecute(); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     try{ 
     //res=GetJson.getJsonFromWeb(uri); 
     network_status=CheckNetworkStatus.isConnected(mContext); 

     if(network_status) 
     { 
      JSONObject jObj=GetJson.getJsonFromWeb(uri); 

      if(jObj!=null){ 
         JSONArray jArray=jObj.getJSONArray("postal_code"); 
          for(int i=0;i<=jArray.length();i++) 
          { 
          JSONObject postalObj=jArray.getJSONObject(i); 
          /* 
          PostalCode pc=new PostalCode(); 
          pc.setStatus(postalObj.getString("status")); 
          pc.setCp(postalObj.getString("cp")); 
          pc.setVille(postalObj.getString("ville"));*/ 
          HashMap<String,String> map=new HashMap<String, String>(); 
          map.put("status",postalObj.getString("status")); 
          map.put("cp",postalObj.getString("cp")); 
          map.put("ville",postalObj.getString("ville")); 

          postalCodeList.add(map); 
          } 

         } 
      else{ 
       showServerError=true; 
      } 
     } 
     else{ 
      showConnectionError=true; 
     } 


     } 
     catch(Exception e){ 

     } 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void args){ 
    //res_txt.setText(res); 
     if(showConnectionError){ 
      showAlertDialog(mContext,"No Internet Connection" ,"You don't have internet connection.",false); 
     } 
     else if(showServerError){ 
      showAlertDialog(mContext,"No Server Connection" ,"You don't have Server connection.",false); 
     } 
     else { 
      PostalAdapter adapter=new PostalAdapter(postalCodeList, mContext); 
      CustomListView.setAdapter(adapter); 


     } 


    } 

} 
+1

不會在單個請求中加載整個數據。你應該使用分頁,就像你有1000條記錄下載你應該分成10頁 –

+0

我試圖實現分頁,但沒有改進... – HemangNirmal

回答

2

可以提高性能by_not_parsing整個jArray映射在doInBackground(..)對象。相反,將jArray傳遞給您的適配器(我假設PostalAdapter是自定義的),並且只解析adapter.getView(..)中所需的數據。

+0

我試試這個,它大大減少了時間,謝謝... – HemangNirmal