使用Retrofit使用http代码200但正文为空的情况下获得响应

时间:2019-03-14 15:34:09

标签: android android-volley retrofit2

我第一次使用Retrofit,在登录验证之后,我运行我的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'

我已将截击呼叫转换为仅将用户名和密码发送到服务器(没有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);
}

1 个答案:

答案 0 :(得分:0)

您需要添加日志记录才能看到真实的响应

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