从 onResponse Retrofit 返回变量

2024-04-02

我对网络服务器进行 API 调用,并在 onResponse 方法中获取 ID。

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

这是我的代码:

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

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

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) {

            }
        });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 onResponse Retrofit 返回变量 的相关文章

随机推荐