AiVision
AiVision

Reputation: 4233

How to pass data from onSuccess to other class?

I am developing one app and it is based on json. I want to pass data form one class to other class which is received in onSuccess method. when i request for data then data will be receive properly but when i call that data to other class the null pointer error will occur. my method is

        @Override
        protected Void doInBackground(Void... params1) {

            RequestParams params;
            params = new RequestParams();
            params.add("username", str_emailId);
            params.add("password", str_password);
            params.add("action", action);

            String str_result=constant.login(params);

            Log.d("Result::--",str_result);

            return null;
        }

and my login method is

public String login( RequestParams params1) {

        client = new AsyncHttpClient();
        client.post(Constant.check_login_user, params1, new TextHttpResponseHandler() {

            @Override
            public void onSuccess(int i, Header[] headers, String response) {

                str_result = response;
                Log.d("Str_Result: ", str_result);

           }

            @Override
            public void onFailure(int statusCode, Header[] headers, String response, Throwable e) {
                if (statusCode == 401) {
                    jsonString[0] = response;
                    Log.d("onFailure: ", jsonString[0]);
                }
            }

        });

        return str_result;
    }

i got the data in on Success method and it will print in log ok but when return the str_result variable it will return null and i print the log in doInBackground() method it will return null pointer error i don't know how? please any one can help me that how to print that data thanks in advance.

Upvotes: 1

Views: 2117

Answers (2)

Alican Temel
Alican Temel

Reputation: 1318

You can post your models to all alive fragments or activites with EventBus.

Link: https://github.com/greenrobot/EventBus

Upvotes: 1

Drag0
Drag0

Reputation: 8928

Your POST method is Asynchronous function. For simplicity, imagine it starts doing it's work in the background, while rest of application continues doing whatever it's doing. When POST method is finished, it will call onSuccess() method or onFailure() method, depending whether POST method succeeded or failed. You see, you call the POST method and return result right away even though POST method didn't finish. You can imagine your login method doing something like this.

  1. result = null
  2. call login()
  3. return result
  4. login is finished and calls onSuccess()

So you see, you return result before login method finished executing.

How to fix it?

Class that receives String result should implement function like: public void setResult(String result) {...}

and your login method should call it in onSuccess() method like this:

@Override
public void onSuccess(int i, Header[] headers, String response) {
  // Your login method finished, now you can send result to receiving object
  str_result = response;
  receivingClassObject.setResult(str_result);
}

Upvotes: 0

Related Questions