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();
}
}
"Deprecated" refers to functions or elements that are in the process of being replaced by newer ones. ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar , which can be embedded in your app's UI.
because It prevents the user to interact with the app. Let me explain to you why this is bad. As we show the Progress Dialog It runs and blocks the UI and user can't interact with the App.
Android ProgressBar is a graphical view indicator that shows some progress. Android progress bar displays a bar representing the completing of the task. Progress bar in android is useful since it gives the user an idea of time to finish its task.
I can confirm this same behavior in the emulator only, testing on 4.1. On my actual device (Galaxy S3 - 4.1) this does not happen. Also, it only seems to happen with indeterminate progress bars. I don't know why this happens, but maybe you will feel better knowing this happens to others. I'm performing almost the same actions as you, connecting to a webservice, parsing an xml response, then displaying the parsed response. FYI, I am using a ProgressBar
for the android:empty
function of a ListView
.
EDIT: Ran some tests
I tested parsing the xml file and with the ProgressBar
in the layout I got this:
Parse time: 94785 ms
So, I changed this
<ProgressBar
android:id="@id/android:empty"
style="@android:style/Widget.Holo.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_weight="1"/>
to this
<TextView
android:id="@id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Searching..."/>
and got this Parse time: 2642 ms
Note: my ProgressBar
and ListView
are in a LinearLayout
and all parsing is done in an AsyncTask
. This is all on the emulator. Really strange... My guess is that the emulator does not actually separate threads from the UI, so the AsyncTask
and ProgressBar
spinning are fighting for CPU cycles, whereas in an actual phone it operates as intended because it is not emulated.
Edit: So, after watching TaskManager on my WinXP, the emulator starts with 7 threads running. I launch my app, run through several AsyncTask
's using AsyncTask.THREAD_POOL_EXECUTOR
and the thread count never changes from 7 (in Task Manager) during this whole process.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With