简体   繁体   中英

How to save the value of a variable inside a Retrofit call?

I am trying to retrieve the value of a certain field from the header using a Retrofit call to be used to be sent back to server. I am successful in getting the value inside the try block and send it back immediately in the try block too. But when I try the same outside the call instance, the value of abc (which is where I assigned the value of the response header) is lost. I have already declared the String abc as a global variable. How do I save the value of the string?

public class MainActivity extends AppCompatActivity {
    private static final String LOG_TAG = "MainActivityClass";
    String abc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);

        Call<List<TrendingModel>> call = apiService.getAllTodos();
        call.enqueue(new Callback<List<TrendingModel>>() {
            @Override
            public void onResponse(Call<List<TrendingModel>> call, Response<List<TrendingModel>> response) {
                try {

                    List<TrendingModel> todoModels = response.body(); // WHERE WE GET THE RESPONSE BODY
                    abc = response.headers().get("Tanand"); // WHERE WE GET THE RESPONSE HEADER AND ASSIGN IT TO abc, WHICH WE DECLARED GLOBALLY

                    ApiClient.getClient(abc).create(ApiInterface.class); // PASSING THE abc VARIABLE TO THE GETCLIENT(TOKEN) METHOD WITHIN
                                    // THE SAME TRY BLOCK WHICH WORKDS

                } catch (Exception e) {
                    Log.d("onResponse", "There is an error");
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<List<TrendingModel>> call, Throwable t) {
                Log.d("onFailure", t.toString());
            }
        });

        ApiClient.getClient(abc).create(ApiInterface.class); // VALUE OF abc IS NOT PERSISTED HERE (abc IS NULL) ALTHOUGH WE DECLARED IT GLOBALLY
    }
}

Try to call method inside OnResponse method .

because onResponse() method runs in background until the data is fetched.

If you want to access the data of response then call your method inside it .

And outside all statements are called before the response data finished which is why it doesn't give you actual data . As a Good Practice , use the below method.

Just create a method inside the class and call your all statements inside it.

Now call your method inside onResponse method .

You can use a setter method within the onResponse method of Retrofit. This answer explains how you can go about it https://stackoverflow.com/a/63060520/10123715

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM