Reputation: 551
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();
}
}
}
Upvotes: 0
Views: 1899
Reputation: 14571
In your doInBackground() routine, save the downloaded result in the member variable Result. Then, in onPostExecute() you will need to read Result and update your UI.
eg:
private void updateUI(String jsondata)
{
foo = do.something.to.derive.some.data.from.jsondata.
TextView tv = findViewById(R.id.textview1);
tv.setText (foo);
}
private class DownloadJsonDataTask extends AsyncTask<String, Integer, String>
{
String Result = null;
...
protected String doInBackground(String... urls)
{
Result = requestInfoFromServer();
}
protected void onPostExecute(String result)
{
dialog.dismiss();
updateUI(Result);
}
}
Upvotes: 0
Reputation: 28418
In addition to Samir Mangroliya.
task.get();
is a blocking call for the thread the method is called from. So using it on the main UI thread you're blocking it which is bad because downloading smth may take some seconds. So the system detects the UI thread is blocked for such a long term and terminates the app with ANR (application not responding) popup. To fix this move result processing to onPostExecute() of your AsyncTask (at least the result processing should be initiated at this point, you can call some host Activity's method here passing the result).
Upvotes: 1
Reputation: 1849
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 named Result
and the other named result
as 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 to Result
only to result
by the end of the function call which again is valid syntax and doesn't point null variable as when you declared Result
it 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 the result
variable 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 the Result
one
make sense?
Upvotes: 0