AsyncTask在localhost json读取时需要3秒

时间:2015-08-27 12:05:02

标签: android

这是我的代码请建议如何加速,它变得非常慢,下次第一次服用3秒钟需要5秒

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;

    }

1 个答案:

答案 0 :(得分:0)

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

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

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

如果您认为您的目的将通过改造(我个人选择)来解决,您可以在以下地方寻找教程: http://www.truiton.com/2015/04/android-retrofit-tutorial/

希望这会有所帮助。