-1

in this below code i want to pass data from processFinish method to use that in outside class but i can not fix problem and i can not return value from processFinish in call function.

public class WSDLHelper  implements AsyncResponse{
    public SoapObject request;

    private String MainResult;

    public String call(SoapObject rq){

        new ProcessTask(rq, this).execute();
        return MainResult;
    }

    @Override
    public void processFinish(String output) {
        MainResult = output;
    }
}

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 String 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.equals(String.valueOf(Integers.CODE_USER_PASS_FALSE))) {
            try {
                throw new TException(PublicErrorList.USERNAME_PASSWORD_ERROR);
            } catch (TException e) {
                e.printStackTrace();
            }
        }

        return result;
    }

    protected void onPostExecute(String result) {
        delegate.processFinish(result);
    }
}

please help me to develop call method to return output value to outside class. thanks

Mahdi Pishguy
  • 109
  • 1
  • 7
  • Is the delegate null on onPostExecute? Because if not then it's working as intended. – Pedro Oliveira Sep 10 '14 at 08:52
  • @PedroOliveira yes thats null. how to resolve that? – Mahdi Pishguy Sep 10 '14 at 08:58
  • [Same exact question answered here](http://stackoverflow.com/a/25759586/2964379). Mahdi, please understand that *return MainResult;* **runs before** *MainResult = output;* and therefore call(...) will always return null, no matter what you do. – 0101100101 Sep 10 '14 at 13:36
  • And both questions are actually reposts of [this one](http://stackoverflow.com/questions/25758236/android-asynctask-dont-return-correct-result). – 0101100101 Sep 10 '14 at 14:29

2 Answers2

0

There you are implementing AsyncResponce. so Replace

new ProcessTask(rq, new AsyncResponse() {
            @Override
            public void processFinish(String output) {
                MainResult = output;
            }
        }).execute();

By

new ProcessTask(rq, this).execute();

Then your class overrided method processFinish(String output) will call.

And use your value at processFinish() rather than value retured by call( ) method.

Devendra
  • 19
  • 1
  • 1
  • 7
0

ProcessTask is an async task, so when u invoke callmethod, u will get null when return MainResult, becz it has not been set by processFinishmethod yet.

Euporie
  • 1,896
  • 1
  • 21
  • 23