android的CPU使用率高,如何降低?
在我的应用程序中,我使用android的CPU使用率高,如何降低?,android,performance,android-asynctask,Android,Performance,Android Asynctask,在我的应用程序中,我使用JSON从服务器获取数据。对于每次使用doInBackground过程获取数据,如下面的代码。像这样,我对不同的链接使用三个doInBackground过程来获取单个类中的数据。我的应用程序使用了超过25%的CPU来纠正它 protected String doInBackground(String... params) { try { HttpClient client = new DefaultHttpClient();
JSON
从服务器获取数据。对于每次使用doInBackground
过程获取数据,如下面的代码。像这样,我对不同的链接使用三个doInBackground
过程来获取单个类中的数据。我的应用程序使用了超过25%的CPU来纠正它
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;
}
受保护的字符串doInBackground(字符串…参数){
试一试{
HttpClient=new DefaultHttpClient();
HttpResponse响应;
HttpPost=新的HttpPost(
"http://www.qwerty.in/foodnew/customer/viewFav?json=");
json.put(“cus”、“cus+CUTOMER\u ID”);
setHeader(“json”,json.toString());
StringEntity se=新的StringEntity(json.toString());
se.setContentEncoding(新的BasicHeader(HTTP.CONTENT\u类型,
“应用程序/json”);
邮政实体(se);
响应=client.execute(post);
//获取数据
InputStream in=response.getEntity().getContent();
字符串a=convertStreamToString(in);
//Log.v(“id”和“+a”);
试一试{
jsonarray=新jsonarray(“[”+a+“]);
json=jsonarray.getJSONObject(0);
字符串菜单=(json.getString(“favous”);
viewFavoriteSuccess=json
.getString(“ViewFavAccess”);
试一试{
jsonarray=新的jsonarray(菜单);
for(int i=0;i
返回的json数据有多大?在您的代码中,唯一可能占用cpu的是convertStreamToString(in)
方法、循环或json.getString
,这意味着您对此无能为力。尝试不同的json库可能会有所帮助。@gigadot返回json数据。5到10个值。你能说说json中的其他库吗?我在android应用程序中使用了gson。但对于一个小型json,这应该无关紧要。它可能是convertStreamToString(in)
方法。@gigadot提供任何教程链接以清楚了解gson。谷歌搜索“gson”一词应该足够了,即