【发布时间】:2015-05-15 14:12:42
【问题描述】:
我一直在使用异步任务来访问 Web 服务器并使用结果更新控件。这有缺点,即它使异步方法特定于控件并阻止我再次使用返回的字符串。
如何从异步调用 onPostExecute 返回结果字符串?我怎么称呼它?我似乎无法让我的代码做到这一点。线程应该没有问题,因为我有一个对话框会冻结 UI,直到工作完成。
我典型的 asyncTask 代码如下
class GetDataFromServer extends AsyncTask<String, String, String>
{
* */
// Progress Dialog
private ProgressDialog qDialog;
private Context context;
private String dialogString;
private ArrayList<String[]> newLoginResult;
// JSON parser class
String url_newGame ="http://xxxxxx.php";
public myAsyncMethos(String dialogMessage, Context con)
{
this.qDialog = new ProgressDialog(con);
this.dialogString = dialogMessage;
this.context = con;
}
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute()
{
super.onPreExecute();
qDialog = new ProgressDialog(this.context);
qDialog.setMessage(this.dialogString);
qDialog.setIndeterminate(false);
qDialog.setCancelable(false);
qDialog.show();
}
@Override
protected JSONObject doInBackground(String... args)
{
//MAKE SERVER CALL and cast to JSONOBject
return jsonNewUser;
}
public void onPostExecute(JSONObject jsonString)
{
// dismiss the dialog after getting response
qDialog.dismiss();
//I WANT TO RETURN A STRING HERE BUT KEEP GETTING SYNTAX ERRORS BEFORE RUNTIME
}
}
【问题讨论】: