我一直在使用异步任务来命中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
}
}
答案 0 :(得分:4)
我个人会在你的课程中添加一个回调函数,然后运行onPostExecute
后,关闭你对主类的监听器的回调。
class GetDataFromServer extends AsyncTask<String, String,JSONObject>
{
// Progress Dialog
private ProgressDialog qDialog;
private Context context;
private String dialogString;
private ArrayList<String[]> newLoginResult;
private InformComplete myCallback;
public GetDataFromServer(String dialogMessage, Context con,InformComplete callback)
{
this.qDialog = new ProgressDialog(con);
this.dialogString = dialogMessage;
this.context = con;
this.myCallback=callback;
}
@Override
protected void onPreExecute()
{
// set up your dialog
}
@Override
protected JSONObject doInBackground(String... args)
{
JSONObject jsonNewUser=new JSONObject();
return jsonNewUser;
}
public void onPostExecute(JSONObject jsonString)
{
qDialog.dismiss();
myCallback.PostData(jsonString);
}
public interface InformComplete
{
public void PostData(JSONObject result);
}
}
然后从你的通话班,你会有这样的事情......
private void callTheAsyncThing
{
GetDataFromServer gds=new GetDataFromServer("please wait", this, letMeKnow);
gds.execute(params);
}
private InformComplete letMeKnow=new InformComplete()
{
public void PostData(JSONObject result)
{
// we now have the data in the calling class
}
};
答案 1 :(得分:1)
您无法在AsynTask的方法中返回值,因为它曾用于返回void元素。因此,您可以实例global var foe示例,并将值设置为它。喜欢... `
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string s1="abc";
vector<string> ss;
ss.push_back("abc");
ss.push_back("vsj");
for (size_t i = 0; i < ss.size(); ++i) {
if (ss.at(i) == s1) {
cout<<"One of the string equal to s1";
}
}
return 0;
}
然后使用它。有一种方法可以归还某些东西。顺便说一句,你无法改变回报。
答案 2 :(得分:0)
onPostExecute的方法不是Programmer调用,而是由AsyncTask实例调用。如果要从异步任务返回结果,可以将参数发送到GetDataFromServer,并在onPostExecute方法中,给他新值,然后,您可以使用Handler发送消息。
答案 3 :(得分:-1)
当您尝试在onPostExecute方法中获取字符串时,在doInBackground方法中返回JSONObject。
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
}