繁体   English   中英

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

[英]Using Retrofit getting response with http code 200 but empty body

我是第一次使用 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);
}

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

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 错误描述

我有同样的问题,在 okhttp 上我看到了我的 JSON 响应。 这似乎是 GSON 解析的问题,但无法修复。

调试时会填充响应,但主体除外

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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