android的CPU使用率很高,如何降低呢?

时间:2013-02-28 11:07:01

标签: android performance android-asynctask

在我的应用程序中,我使用JSON从服务器获取数据。每次使用doInBackground进程获取数据,如下面的代码。像这样我使用三个doInBackground进程来获取不同的链接来获取单个类中的数据。我的应用程序使用CPU超过25%如何纠正它。

protected String doInBackground(String... params) {

        try {

            HttpClient client = new DefaultHttpClient();
            HttpResponse response;

            HttpPost post = new HttpPost(
                    "http://www.qwerty.in/foodnew/customer/viewFav?json=");

            json.put("cus", "" + CUTOMER_ID);

            post.setHeader("json", json.toString());
            StringEntity se = new StringEntity(json.toString());

            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                    "application/json"));
            post.setEntity(se);
            response = client.execute(post);

            // get a data
            InputStream in = response.getEntity().getContent();
            String a = convertStreamToString(in);
            // Log.v("id", ""+a);

            try {

                jsonarray = new JSONArray("[" + a + "]");
                json = jsonarray.getJSONObject(0);
                String menus = (json.getString("favous"));
                viewfavouriteSuccess = json
                        .getString("viewfavccess");

                try {

                    jsonarray = new JSONArray(menus);
                    for (int i = 0; i < jsonarray.length(); i++) {
                        json = jsonarray.getJSONObject(i);
                        String id = json.getString("faem_id");
                        String name = json.getString("fav");
                        String price = json.getString("fae");
                        String stock = json.getString("fack");
                        // String fav_success =
                        // json.getString("viewfavouess");

                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put(FAV_PRODUCT_ID, id);
                        map.put(FAV_ITEM_NAME, name);
                        map.put(FAV_PRICE, price);
                        map.put(FAV_STOCK, stock);
                        fav_alist.add(map);
                        // Log.v("fav_list", ""+fav_alist);
                    }

                } catch (Exception e) {
                    // TODO: handle exception
                }

            } catch (Exception e) {

                Toast.makeText(getApplicationContext(),
                        "You are a new user, No favourite items", 700)
                        .show();

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return viewfavouriteSuccess;
    }

0 个答案:

没有答案
相关问题