我一直在使用异步任务来访问Web服务器并使用结果更新控件。这样做有缺点,即使异步方法特定于控件,也会阻止我再次使用返回的字符串。
如何从onPostExecute中返回异步调用的结果字符串?我该如何调用它?我似乎无法让我的代码实现这一点。由于我有一个对话框可以冻结UI直到工作完成,因此应该不会出现线程问题。
我的典型异步任务代码如下:
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
}
}