[英]Retrofit 2.0: getting response code 200 but not getting the desired data
Retrofit 2.0的一个非常令人失望的特性是它并没有准确地说明解析响应失败的地方。 因此,在邮递员遇到同一个机构的请求时,我得到一个登录响应:
{
"result": "success",
"response_code": 200,
"data": {
"id": "1",
"display_name": "admin",
"email": "payal@teckmovers.com",
"username": "admin",
"access_token": "8daa8e02ca432e51ae90912fbf63eeea"
}
}
但是当我在Retrofit中使用完全相同的主体点击完全相同的请求时,我得到了一个非常特殊的响应:{protocol = http / 1.1,code = 200,message = OK,url = http://192.168.0.52/evidya / wp-api / v1 / user / login }。 现在我已经解决了上述问题的其他相关问题,但没有一个问题对我有用。 请帮忙。 我的代码:
改造API接口:
public interface eVidyaApi {
@FormUrlEncoded
@POST("user/login")
Call<LoginResponse> loginUser(
@HeaderMap Map<String, String> headers,
@Field("email") String email,
@Field("password") String password
);
}
登录功能:
public void login() {
Log.d(TAG, "Login");
if (!validate()) {
onLoginFailed();
return;
}
final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this, R.style.MyDialogTheme);
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Authenticating...");
progressDialog.show();
String email = _emailText.getText().toString();
String password = _passwordText.getText().toString();
Log.d(TAG, "login: "+email+" "+password);
// TODO: Implement your own authentication logic here.
Call<LoginResponse> loginResponseCall = evidya.loginUser(Common.getHeaders(), email, password);
loginResponseCall.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
progressDialog.dismiss();
if(!response.isSuccessful()){
Toast.makeText(LoginActivity.this, ""+response.message(), Toast.LENGTH_SHORT).show();
Log.d(TAG, "onResponse: fail "+response.code());
return;
}
Log.d(TAG, "onResponse: success"+response.code()+" "+response);
if(response.body()!=null){
String content="";
// _loginButton.setEnabled(false);
LoginResponse loginResponse = response.body();
content += "code:"+ response.code();
content += "token:"+ loginResponse.getData().getAccessToken();
content += "result"+ loginResponse.getResult();
content += "result"+ loginResponse.getData().getDisplayName();
// onLoginSuccess();
Log.d(TAG, "onResponse: login res"+content);
} else {
Toast.makeText(LoginActivity.this, "Invalid response from server", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
progressDialog.dismiss();
Toast.makeText(LoginActivity.this, "Cannot fetch request", Toast.LENGTH_SHORT).show();
}
});
}
LoginResponse.java
package com.example.evidya.Retrofit.Model.LoginModel;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class LoginResponse {
@SerializedName("result")
@Expose
private String result;
@SerializedName("response_code")
@Expose
private Integer responseCode;
@SerializedName("data")
@Expose
private Data data;
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public Integer getResponseCode() {
return responseCode;
}
public void setResponseCode(Integer responseCode) {
this.responseCode = responseCode;
}
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
}
Data.java
package com.example.evidya.Retrofit.Model.LoginModel;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Data {
@SerializedName("id")
@Expose
private String id;
@SerializedName("display_name")
@Expose
private String displayName;
@SerializedName("email")
@Expose
private String email;
@SerializedName("username")
@Expose
private String username;
@SerializedName("access_token")
@Expose
private String accessToken;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
}
我的日志记录(ok hhttp),点击登录按钮时出现错误的详细信息:
我的日志记录(ok hhttp),点击带有CORRECT详细信息的登录按钮:
基本上问题是我使用的是Log.d(TAG, "onResponse: success"+response.code()+" "+response);
检查响应回调中的响应。 虽然我应该做的就是不要卡在那里并检查loginResponse对象的值(来自LoginResponse loginResponse = response.body();
)。 因为response.body实际上以对象形式存储响应。 这就是改造中的工作方式。
根据您的日志,API正确调用。 它也回应。 但问题是你的后端API验证失败了。 在Web服务上添加日志并进行检查。 从应用程序方面来说,它工作正常。 这不是Retrofit的问题。
使用下面的内容更新onResponse()并运行应用程序。 然后测试并告诉我你得到的消息。
if(response.body()!=null){
LoginResponse loginResponse = response.body();
String content="";
if (response.body().getResponseCode()==200){
content+= loginResponse.getData().getAccessToken();
content+= loginResponse.getData().getDisplayName();
content+= loginResponse.getData().getEmail();
content+= loginResponse.getData().getId();
content+= loginResponse.getData().getUsername();
}else{
content+=loginResponse.getData().getMsg();
}
Log.d(TAG, "onResponse: login res"+content);
} else {
Toast.makeText(LoginActivity.this, "Invalid response from server", Toast.LENGTH_SHORT).show();
}
下面是Data.java中的代码
@SerializedName("msg")
@Expose
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.