Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用接口在AsyncTask onPostExecute中返回Android值_Java_Android_Android Asynctask_Android Ksoap2 - Fatal编程技术网

Java 使用接口在AsyncTask onPostExecute中返回Android值

Java 使用接口在AsyncTask onPostExecute中返回Android值,java,android,android-asynctask,android-ksoap2,Java,Android,Android Asynctask,Android Ksoap2,在下面的代码中,我想使用接口从AsyncTask返回值。但我得到了错误的值,无法从onPostExecute返回正确的值 我用我的代码开发了这个教程。我不能正确地使用它 接口: public interface AsyncResponse { void processFinish(String output); } KsoapMain类: public class WSDLHelper implements AsyncResponse{ public SoapObject r

在下面的代码中,我想使用接口从
AsyncTask
返回值。但我得到了错误的值,无法从
onPostExecute
返回正确的值

我用我的代码开发了这个教程。我不能正确地使用它

接口:

public interface AsyncResponse {
    void processFinish(String output);
}
Ksoap
Main类:

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);
    }

}
公共类WSDLHelper实现异步响应{
公共对象请求;
私有字符串mainsult;
公共字符串调用(SoapObject rq){
ProcessTask p=新的ProcessTask(rq);
String tmp=String.valueOf(p.execute());
p、 委托=此;
返回主结果;
}
@凌驾
公共void processFinish(字符串输出){
这个.main结果=输出;
}
}
类ProcessTask扩展了AsyncTask{
公共异步响应委托=null;
SOAPObjectReq1;
私有字符串结果;
公共ProcessTask(SoapObject rq){
req1=rq;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的Void doInBackground(Void…参数){
SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
信封.setOutputSoapObject(this.req1);
AndroidHttpTransport=新的AndroidHttpTransport(Strings.URL\u TSMS);
transport.debug=true;
试一试{
transport.call(Strings.URL\u TSMS+this.req1.getName(),信封);
this.result=envelope.getResponse().toString();
}捕获(IOEX异常){
Log.e(“,例如getMessage());
}捕获(XmlPullParserException-ex){
Log.e(“,例如getMessage());
}
if(result.equals(String.valueOf(Integers.CODE\u USER\u PASS\u FALSE))){
试一试{
抛出新的TException(PublicErrorList.USERNAME\u PASSWORD\u ERROR);
}捕获(特克斯){
e、 printStackTrace();
}
}
Log.e(“----------++++++++--------------”,this.result);
返回null;
}
受保护的void onPostExecute(字符串结果){
/*super.onPostExecute(结果)*/
delegate.processFinish(this.result);
}
}

请帮助我解决此问题

您的结果将始终为null,因为您在
doInBackground()
方法中返回null。在
doInBackground()
中返回的值将传递给onPostExecute()。将
AsyncTask
更改为
AsyncTask
,并返回结果。这将调用带有正确结果的
onPostExecute(字符串结果)


也许这个链接可以帮你一点忙:

那不行。您正在创建和执行AsyncTask(异步!),然后在它尚未收到结果时调用ReturnMainResult(同步!)。解决方案是删除冗余类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);
}

}

更改
doInBackground()
onPostExecute()
的返回类型,然后使用
return
语句返回对象谢谢。如何在
WSDLHelper
中开发
call
方法?我在
ProcessTask p=newprocesstask(rq)中的
Contsructor
中得到错误我要将其更改为
公共字符串调用(SoapObject rq){ProcessTask p=newprocesstask(rq,this);/*String tmp=String.valueOf(p.execute());p.delegate=this;*/return main result;}
但这不正确。哦,实际上,我还没有看到你的“p.delegate=this”。这在很多方面都是错误的。。。您还没有理解接口的概念。您正在执行一个异步任务,并希望立即返回其结果。它是异步的,所以这是不可能的!我会更新我的答案。对不起,先生。我正在为您开发更新代码。但这是错误的,返回
NullPointerException
。删除WSDLHelper。不要返回“Mainresult”,它是空的,因为ProcessTask异步返回其结果!在processFinish()内处理结果。如果你不能理解这一点,你必须阅读——否则没人能帮你。对不起。在发送主题之前,我阅读了该链接,但我在整个项目中使用了
call
方法表单
WSDLHelper
,我必须返回结果才能使用其他方法,我无法删除
WSDLHelper
。使用
processFinish
获取输出后,我是否可以从中返回结果。例如:
LoginFields loginSoapParser=tsms.SoapParser(wsdlHelper.call(tsms.SoapObjectRequest))请帮帮我谢谢
new ProcessTask(rq, new AsyncResponse() {
    @Override
    public void processFinish(String output) {
        // do whatever you want with the result
    }
}).execute();