在下面的代码中,我希望使用接口从AsyncTask
返回值。但是我得到了错误的值,我无法从onPostExecute
返回正确的值。
我用我的代码开发了这个link教程。我无法正确使用它。
接口:
public interface AsyncResponse {
void processFinish(String output);
}
Ksoap
主要课程:
public class WSDLHelper implements AsyncResponse{
public SoapObject request;
private String Mainresult;
public String call(SoapObject rq){
ProcessTask p =new ProcessTask(rq);
String tmp = String.valueOf(p.execute());
p.delegate = this;
return Mainresult;
}
@Override
public void processFinish(String output) {
this.Mainresult = output;
}
}
class ProcessTask extends AsyncTask<Void, Void, Void > {
public AsyncResponse delegate=null;
SoapObject req1;
private String result;
public ProcessTask(SoapObject rq) {
req1 = rq;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(this.req1);
AndroidHttpTransport transport = new AndroidHttpTransport(Strings.URL_TSMS);
transport.debug = true;
try {
transport.call(Strings.URL_TSMS + this.req1.getName(), envelope);
this.result = envelope.getResponse().toString();
} catch (IOException ex) {
Log.e("" , ex.getMessage());
} catch (XmlPullParserException ex) {
Log.e("" , ex.getMessage());
}
if (result.equals(String.valueOf(Integers.CODE_USER_PASS_FALSE))) {
try {
throw new TException(PublicErrorList.USERNAME_PASSWORD_ERROR);
} catch (TException e) {
e.printStackTrace();
}
}
Log.e("------------++++++++++++++++-----------", this.result);
return null;
}
protected void onPostExecute(String result) {
/* super.onPostExecute(result);*/
delegate.processFinish(this.result);
}
}
请帮我解决这个问题
答案 0 :(得分:2)
那不行。您正在创建并执行AsyncTask(异步!),然后在尚未收到结果时调用返回Mainresult(同步!)。解决方案是删除冗余类WSDLHelper并直接访问ProcessTask
除此之外,您正在错误地使用AsyncTask(将结果保存在类变量中而不是将其作为参数传递)。这是完整版:
public class ProcessTask extends AsyncTask<Void, Void, String> {
public AsyncResponse delegate=null;
SoapObject req1;
public ProcessTask(SoapObject rq, AsyncResponse delegate) {
req1 = rq;
this.delegate = delegate;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(this.req1);
AndroidHttpTransport transport = new AndroidHttpTransport(Strings.URL_TSMS);
transport.debug = true;
String result = null;
try {
transport.call(Strings.URL_TSMS + this.req1.getName(), envelope);
result = envelope.getResponse().toString();
} catch (IOException ex) {
Log.e("" , ex.getMessage());
} catch (XmlPullParserException ex) {
Log.e("" , ex.getMessage());
}
if (result != null && result.equals(String.valueOf(Integers.CODE_USER_PASS_FALSE))) {
try {
throw new TException(PublicErrorList.USERNAME_PASSWORD_ERROR);
} catch (TException e) {
e.printStackTrace();
}
}
Log.e("------------++++++++++++++++-----------", result);
return result;
}
protected void onPostExecute(String result) {
/* super.onPostExecute(result);*/
delegate.processFinish(result);
}
}
现在您将从外部执行ProcessTask,这将确保您异步接收结果:
new ProcessTask(rq, new AsyncResponse() {
@Override
public void processFinish(String output) {
// do whatever you want with the result
}
}).execute();
答案 1 :(得分:0)
您的结果将始终为null,因为您在doInBackground()
方法中返回null。您在doInBackground()
中返回的值将传递给onPostExecute()。将您的AsyncTask<Void, Void, Void>
更改为AsyncTask<Void, Void, String>
,然后返回结果。这将调用onPostExecute(String result)
并获得正确的结果。
也许这个链接可能对您有所帮助:http://bon-app-etit.blogspot.be/2012/12/using-asynctask.html