android - 使用 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);
}
解决方案
您需要添加日志才能看到真正的响应
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 错误描述
推荐阅读
- ansible - Ansible 查找格式
- postgresql - 可以使用 pglogical 将本地数据库迁移到 AWS RDS 吗?
- node.js - Socket.io“连接”事件随机触发多次
- mongodb - 如何使用 cloudrun 执行 mongodump 到 cloudstorage?
- flutter - 无法在flutter中下载Bloc Pattern的包
- python - 如何根据行中的多个条件和上面的数据填充数据框中的缺失数据
- terraform - Helm 提供程序未在 Chart 中检测到新模板
- mysql - 在优化查询方面需要帮助
- javascript - 如何在 CRUD 表中的删除按钮中具有撤消功能?
- database - 如何在 QLDB 中按降序获取记录?