从 onResponse Retrofit 返回变量 [英] Return variable from onResponse Retrofit

查看:47
本文介绍了从 onResponse Retrofit 返回变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对网络服务器进行 API 调用,然后在 onResponse 方法中取回 ID.

I do a API call to a webserver and I get the ID back in the method onResponse.

现在我想保存这个ID并在doLogin方法的返回中返回这个ID.如何在 return 语句中获取该变量 ID?

Now I want to save this ID and return this id in the return of the method doLogin. How can I get that variable ID in the return statement?

这是我的代码:

public class LoginController {

    public static String doLogin(String loginMail, String loginPassword) {

        //Logging Retrofit
        final HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("###URLTOAPICALL###")
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        APIService service = retrofit.create(APIService.class);
        Call<JsonElement> call = service.doLogin(loginMail, loginPassword);

        call.enqueue(new Callback<JsonElement>() {
            @Override
            public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {

                if (response != null) {
                    JSONObject obj = null;

                    try {
                        obj = new JSONObject(response.body().toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    JSONObject setup = null;
                    try {
                        setup = obj.getJSONObject("setup");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    if(setup != null) {
                        try {
                            Setup stp = new Setup();
                            stp.setUserId(setup.getInt("id"));

                            //I WANT HERE TO SAVE MY ID

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                }
            }

            @Override
            public void onFailure(Call<JsonElement> call, Throwable t) {
                Log.v("ERROR", t+"");
            }


        });

        return "I WANT RETURN THAT ID HERE";
    }
}

推荐答案

由于改造是异步的,不要从方法返回而是使用接口回调.

As retrofit is asynchronous don't return from method instead use interface callbacks.

public class LoginController {

    public interface LoginCallbacks{
        void onLogin(String id);
        void onLoginFailed(Throwable error);
    }

    public static void doLogin(String loginMail, String loginPassword, final LoginCallbacks loginCallbacks) {

        //Logging Retrofit
        final HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("###URLTOAPICALL###")
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        APIService service = retrofit.create(APIService.class);
        Call<JsonElement> call = service.doLogin(loginMail, loginPassword);

        call.enqueue(new Callback<JsonElement>() {
            @Override
            public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {

                if (response != null) {
                    JSONObject obj = null;

                    try {
                        obj = new JSONObject(response.body().toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    JSONObject setup = null;
                    try {
                        setup = obj.getJSONObject("setup");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    if(setup != null) {
                        try {
                            Setup stp = new Setup();
                            stp.setUserId(setup.getInt("id"));

                            //I WANT HERE TO SAVE MY ID
                            if (loginCallbacks != null)
                                loginCallbacks.onLogin(setup.getInt("id"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                }
            }

            @Override
            public void onFailure(Call<JsonElement> call, Throwable t) {
                Log.v("ERROR", t+"");
                if (loginCallbacks != null)
                    loginCallbacks.onLoginFailed(t);
            }


        });
    }
}

调用方法:

doLogin("email", "password", new LoginCallbacks() {
            @Override
            public void onLogin(String id) {

            }

            @Override
            public void onLoginFailed(Throwable error) {

            }
        });

这篇关于从 onResponse Retrofit 返回变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆