如何在Android中加速JSON解析?

时间:2014-02-11 12:03:14

标签: android json android-asynctask

我在JSON响应中有数千个数据,我使用基本适配器解析并在自定义列表视图中显示此数据,但问题是显示时间太长

我正在解析JSON的我的AsyncTask是Follows

    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 个答案:

答案 0 :(得分:2)

您可以通过整个jArray来提高性能,以便在doInBackground(..)中映射对象。而是将jArray传递给你的适配器(我假设PostalAdapter是自定义的),并且只解析adapter.getView(..)中所需的数据。