AsyncTask在读取本地主机json时需要3秒



这是mycode,请建议如何加快速度,它变得非常慢,第一次需要5秒,下一次需要3秒

public ArrayList<HealthBean> getIPList(String user,String mac) {
        ArrayList<HealthBean> list=new ArrayList<HealthBean>();
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("client", user));
        params.add(new BasicNameValuePair("mac", mac));
        Log.d(login_tag, params.toString());
        JSONObject json=jsonParser.getJSONFromUrl(getIPList, params);        
        Log.d(login_tag, params.toString());
        try {
            int status=json.getInt("success");
            System.out.println("---------------------"+status);
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            jArray = json.getJSONArray("products");
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                HealthBean bean=new HealthBean();
                bean.setIp(json_data.getString("ip"));
                bean.setDb(json_data.getInt("db"));
                bean.setLinux(json_data.getInt("linux"));
                bean.setSlave(json_data.getInt("slave"));
                bean.setApplication(json_data.getString("app"));
                bean.setDbPort(json_data.getString("mysql_port"));
                bean.setLinuxPort(json_data.getString("linux_port"));
                list.add(bean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }        

        return list;
    }

//解析json

public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) { 
        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 int i=0;
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "n");
            }
            is.close();
            json = sb.toString();
            Log.e("JSON", json);
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);            
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
        // return JSON String
        return jObj;
    }

请尝试使用retrofit/volley在查询API(任何网络调用)时获得更好的性能。

下面的链接给出了基准测试和测试结果。

http://instructure.github.io/blog/2013/12/09/volley-vs-retrofit/

如果你认为你的目的可以通过改造来解决(我个人的选择),你可以在http://www.truiton.com/2015/04/android-retrofit-tutorial/

最新更新