改造2.0:获取响应码200,但未获取所需数据

时间:2019-04-02 05:30:55

标签: java android retrofit retrofit2

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} 。现在,我已经解决了其他与上述问题相关的问题,但是没有一个对我有用。请帮忙。我的代码:

Retrofit 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;
    }

}

我的日志记录(确定hhttp),当单击登录按钮并输入错误的详细信息时:

enter image description here

我的日志记录(确定为hhttp),当单击具有正确详细信息的登录按钮时:

enter image description here 解决方案:

基本上,问题在于我正在使用Log.d(TAG, "onResponse: success"+response.code()+" "+response);来检查onresponse回调中的响应。我应该做的就是不要卡在那儿,而是检查loginResponse对象的值(来自LoginResponse loginResponse = response.body();)。因为response.body实际上以对象形式存储响应。事情就是这样进行改造的。

1 个答案:

答案 0 :(得分:4)

enter image description here

根据您的日志,API会正确调用。它也会回应。但问题是您的后端的API身份验证失败。在您的Web服务上添加日志并检查。从应用程序方面来看,它运行良好。这不是改造的问题。

使用下面的内容更新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;
        }