Hi, I’m a beginner developer of android app. I do this work for a University Exam. I read more documentation but I have a problem with show a progress dialog in my activity while the asynktask download a Json String from a server that then I have to put in a listview.
In my UI thread I call the Asynk task, but the thread continue to work and I can’t use the result of the httpGet(that works fine).. I understand this using a Log.i(…)
Why the UI thread dosn’t stop and attend the result?? What I do Wrong?
Please help me.
package my.pack;
import java.util.concurrent.ExecutionException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
public class TestDialogActivity extends Activity
{
ProgressDialog dialog;
String url = "My URL";
String result= "init";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DownloadJsonDataTask task = (DownloadJsonDataTask) new DownloadJsonDataTask(result).
execute(url);
try {
String ris = task.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("GET",result);
}
public String requestInfoFromServer() {
String request = null;
HttpConnection http = HttpConnection.getInstance();
http.setHttpClient(url);
request = http.executeRequest();
return request;
}
private class DownloadJsonDataTask extends AsyncTask<String, Integer, String>
{
String Result;
protected void onPreExecute()
{
dialog = new ProgressDialog(TestDialogActivity.this);
dialog.setTitle("Download");
dialog.setMessage("Please wait...");
dialog.setIndeterminate(true);
dialog.show();
}
public DownloadJsonDataTask(String response) {
this.Result=response;
}
protected String doInBackground(String... urls) {
String urldisplay = urls[0];
Log.i("STRING URL:", urldisplay);
String result = requestInfoFromServer();
return Result;
}
protected void onPostExecute(String result) {
this.Result = result;
dialog.dismiss();
}
}
}
ahh I see the problem in the
doInBackground(String... urls)function you have declared two different strings with similar names now since variable names are case sensitive it is perfectly legal to have one string namedResultand the other namedresultas R and r are seen as two different characters so thus both have unique names. how ever while this is valid syntax; this is prone to logic errors. And I believe this is where you ran into problems. you did not assign any value toResultonly toresultby the end of the function call which again is valid syntax and doesn’t point null variable as when you declaredResultit did put in a default value of an empty string. so it will compile and not throw a null pointer error because the variable is not null, even though there is is no string data in it the pointer is still pointing at a valid spot in the memory so as far as the compiler is concerned everything is good, it’s not supposed to check on the contents of the string just pass it on. meanwhile theresultvariable which you do assign all the data to just before the end of the call gets loaded up with all the data you want. gets completely ignored until the end of the call as there are no further commands dealing with it.and then at the end of the call it gets garbage collected and the data is never passed on as it wasn’t told to pass that variable only theResultonemake sense?