I'm developing an android application in which I use an AsyncTask to get information from a Web service using JSON, which is then loaded in a ListView. Until here everything works fine.
I used an ProgressDialog which is initiated in onPreExecute() method and dismissed in the onPostExecute() method. In the emulator running 2.3.3 android version, all this action takes up to 7/8 seconds. I've tested with a real device running 2.3.6 and the app has the same behavior.
However, when I use an emulator running 4.1 android version, this action takes 65 to 70 seconds. When I remove the ProgressDialog, the action takes 7/8 seconds in both android 2.3 and 4.1 versions. I've tested all these scenarios many times.
Why does this happen? I don't have any real device with 4.1 android version to test it so I'm wondering if is this an emulator issue? I've tried to use a normal progress bar circle, instead of the ProgressDialog, but the same thing happens, it just takes too long in the android 4.1 version.
Any ideas on which may be the reason for this strange behavior? :S
Thank you.
dmon and James McCracken here is the code from my activity, can you help me on this please?
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();
}
}