嗨,我是一名安卓应用程序的初学者开发者。我为大学考试做这项工作。我阅读了更多文档,但在我的活动中显示进度对话框时遇到问题,而 asynktask 从服务器下载 Json 字符串,然后我必须将其放入列表视图中。在我的 UI 线程中,我调用了 Asynk 任务,但线程继续工作,我无法使用 httpGet 的结果(工作正常)。我使用 Log.i(...) 理解这一点 为什么 UI线程不会停止并参加结果?我做错了什么?请帮我。
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();
}
}
}