我正在开发一个Android应用程序,其中使用AsyncTask从Web服务中使用JSON获取信息,然后将该信息加载到ListView中。直到这里一切正常。ProgressDialog或ProgressBar会降低运行Android 4.1的应用程序
我使用了一个在onPreExecute()方法中启动并在onPostExecute()方法中被解散的ProgressDialog。在运行2.3.3 android版本的模拟器中,所有这些操作最多需要7/8秒。我已经使用运行2.3.6的真实设备进行了测试,并且该应用程序具有相同的行为。
但是,当我使用运行4.1 android版本的模拟器时,此操作需要65到70秒。当我删除ProgressDialog时,动作在Android 2.3和4.1版本中都需要7/8秒。我已经多次测试过所有这些场景。
为什么会发生这种情况?我没有任何真正的4.1 Android版本的设备来测试它,所以我想知道这是一个模拟器问题?我试过使用普通的进度条圈,而不是ProgressDialog,但同样的事情发生,它只需要很长的时间在android 4.1版本。
任何想法可能是这种奇怪行为的原因? :S
谢谢。
dmon和James McCracken这里是我的活动代码,请问您能帮我吗?
public class MainActivity extends Activity {
private TextView tvTitleList;
private ListView lvDataSearch;
private ProgressDialog dialog;
private long timeStart;
private String[] dataSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTitleList = (TextView) findViewById(R.id.tvTitleList);
lvDataSearch = (ListView) findViewById(R.id.lvDataSearch);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class ProcessUpdateList extends AsyncTask<Integer, String, String[]> {
@Override
protected void onPreExecute() {
timeStart = System.currentTimeMillis();
System.out.println("pre execute");
try {
tvTitleList.setText("working....");
dialog = ProgressDialog.show(MainActivity.this, "", "Loading. Please wait...", true);
} catch (Exception e) {
System.out.println("Error Async 1: " + e.getMessage());
}
}
@Override
protected String[] doInBackground(Integer... paramss) {
try {
System.out.println("Get Data");
dataSearch = getDataSearch();
} catch (Exception e) {
System.out.println("Error Async 2: " + e.getMessage());
}
System.out.println("Return Data "
+ ((System.currentTimeMillis() - timeStart)/1000));
return dataSearch;
}
@Override
protected void onPostExecute(String[] values) {
try {
System.out.println("Post Execute Data");
tvTitleList.setText("done in " + ((System.currentTimeMillis() - timeStart)/1000));
updateViewList();
dialog.dismiss();
} catch (Exception e) {
System.out.println("Error Async 3: " + e.getMessage());
}
}
}
public void eventUpdateList() {
ProcessUpdateList process = new ProcessUpdateList();
try {
process.execute();
} catch (Exception e) {
System.out.println("NEW ERROR 2: " + e.getMessage());
}
}
public String[] getDataSearch() {
try {
String readTwitterFeed = readJSON2("http://search.twitter.com/search.json?q=hey");
JSONObject jsonObject = new JSONObject(readTwitterFeed);
JSONArray jsonArray = new JSONArray();
jsonArray.put(jsonObject);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(jsonObject);
JSONObject jsonObject2 = new JSONObject(json);
JSONObject level1 = jsonObject2.getJSONObject("nameValuePairs");
JSONObject level2 = level1.getJSONObject("results");
JSONArray level3 = level2.getJSONArray("values");
dataSearch = new String[level3.length()];
System.out.println(level3.length()+" resultados encontrados.");;
for(int i=0; i<level3.length(); i++) {
dataSearch[i] = level3.getJSONObject(i).getJSONObject("nameValuePairs").getString("text");
}
}
catch(JSONException e) {
System.out.println(e.getMessage());
}
return dataSearch;
}
public String readJSON2(String command) {
JSONObject jObj = null;
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(command);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
reader.close();
jObj = new JSONObject(builder.toString());
} else {
System.out.println(MainActivity.class.toString() + " Failed to download file");
}
} catch (Exception e) {
return null;
}
return builder.toString();
}
public void updateViewList() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, dataSearch);
lvDataSearch.setAdapter(adapter);
}
public void clickBRefreshList(View v) {
eventUpdateList();
}
}
我想不出会发生这种情况的原因。你可以发布日志吗? –
这里有点可疑,显示任何视图都不应该花费任何时间。目前还不清楚你的意思是0.875秒还是“7或8”秒。如果你的意思是7 - 8,那么肯定是错的。你应该发布你的AsyncTask的代码。 – dmon