首页 > 解决方案 > 使用 Retrofit 获得 http 代码 200 的响应但正文为空

问题描述

我是第一次使用 Retrofit,在登录验证后我运行我的改造 api 调用函数loginApiCall来验证用户。即使我的验证错误(电子邮件和密码错误),我也得到了响应 200,但得到了此检查的真实结果作为response.isSuccessful()响应。

private void loginApiCall() {
    Call<LoginResponse> call = RetrofitClient
            .getInstance().getApi().userLogin("*******@gmail.com", "****");
    call.enqueue(new Callback<LoginResponse>() {
        @Override
        public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
            if (response.isSuccessful())
                Toast.makeText(LoginScreen.this, "Success", Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(LoginScreen.this, "no Success", Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onFailure(Call<LoginResponse> call, Throwable t) {
            Toast.makeText(LoginScreen.this, "Failed", Toast.LENGTH_SHORT).show();

        }
    });
}

在这里为我的电话改造设置

public class RetrofitClient {

private static RetrofitClient mInstance;
private Retrofit retrofit;
private RetrofitClient() {
    OkHttpClient okHttpClient = new OkHttpClient.Builder().build();

    retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(okHttpClient)
            .build();
}

public static synchronized RetrofitClient getInstance() {
    if (mInstance == null) {
        mInstance = new RetrofitClient();
    }
    return mInstance;
}

public QareebApiInterface getApi() {
    return retrofit.create(QareebApiInterface.class);
}

这是 POST 的接口

public interface QareebApiInterface {
@FormUrlEncoded
@POST(WebServiceConstant.END_POINT_NEW_USER)
Call<LoginResponse> userLogin(
        @Field("email") String email,
        @Field("password") String password
);
}

这是与改造相关的 gradle 文件

implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'

我已将 volley call 转换为仅将用户名和密码发送到服务器的改造(没有 Auth)。因为我没有发送或丢失任何东西,所以有任何身份验证问题吗?但如果缺少为什么响应是 200?

以下是我用改装代替的凌空呼叫(工作正常)。

public void  loginApiCall(final String email, final String password) {

    RequestQueue queue = Volley.newRequestQueue(this);
    StringRequest postRequest = new StringRequest(Request.Method.POST, BASE_URL,
            new com.android.volley.Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.d(TAG, "onResponse: "+response);
                }
            },
            new com.android.volley.Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }
    ) {
        @Override
        protected Map<String, String> getParams() {

            Map<String, String> params = new HashMap<String, String>();
            params.put("email", email);
            params.put("password", password);
            return params;
        }
    };
    queue.add(postRequest);
}

标签: androidandroid-volleyretrofit2

解决方案


您需要添加日志才能看到真正的响应

implementation 'com.squareup.okhttp3:logging-interceptor:3.12.1'

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new 
OkHttpClient.Builder().addInterceptor(interceptor).build();

它可以是 200 错误描述


推荐阅读