java.lang.IllegalStateException:预期BEGIN_ARRAY但是BEGIN_OBJECT -ANDROID RETROFIT2

时间:2017-11-11 07:43:42

标签: android retrofit retrofit2

我已经多次在stackoverflow中看到过这个问题了。但那些答案并没有解决我的问题,我是新手改造。我用我的登录界面改造。我发送用户名,密码然后回复将是数组中的两个令牌。当我尝试登录时,记录显示java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT

的猫

请求

POST:form-urlencorded

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(AllConstants.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();


  public void getUser(String username,String password){

        WebserviceAPI apiService =retrofit.create(WebserviceAPI.class);
        Call<UserResponse> call = apiService.getUsers("signin",username,password);
        call.enqueue(new Callback<UserResponse>() {
            @Override
            public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
                UserResponse result = response.body();
                Log.d("res",""+result.getData());

            }

            @Override
            public void onFailure(Call<UserResponse> call, Throwable t) {
                Log.d("res",""+t.getMessage());

            }
        });
    }

模型类

public class User {

    String id;
    String username;
    String email;
    String access_token;
    String refresh_token;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAccess_token() {
        return access_token;
    }

    public void setAccess_token(String access_token) {
        this.access_token = access_token;
    }

    public String getRefresh_token() {
        return refresh_token;
    }

    public void setRefresh_token(String refresh_token) {
        this.refresh_token = refresh_token;
    }

}

UserResponse.java

public class UserResponse {
    List<User> data;

    public List<User> getData() {
        return data;
    }

    public void setData(List<User> data) {
        this.data = data;
    }

}

接口

public interface WebserviceAPI {
    @FormUrlEncoded
    @POST("auth")
    Call<UserResponse> getUsers(@Field("module_action") String signin ,@Field("username") String username,@Field("password") String password);
}

来自服务器端的响应:PHP

$response = ['status' => true,
            'message' => "Successfully logged in",
            'data' => [
              'access_token' => $accessToken,
              'refresh_token' => $refreshToken
            ],
          ];
            $this->returnJson($response, 200);

当我在Postman中运行时,响应如下

enter image description here

--------更新-----------

某些回复仅包含statusmessage

回应例如:

{
    "status": false,
    "message": "Inactive User"
}

然后我想得到message

我已编辑UserResponse,如下所示,并尝试获取message。然后显示java.lang.NullPointerException at com.android.app.myapp.Login$3.onResponse(Login.java:92)

 public class UserResponse {
        @SerializedName("status")
        @Expose
        private String status;

        @SerializedName("message")
        @Expose
        private String message;

        User data;

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public User getData() {
            return data;
        }

        public void setData(User data) {
            this.data = data;
        }

    }

public void getUser(String username,String password){

        WebserviceAPI apiService =retrofit.create(WebserviceAPI.class);
        Call<UserResponse> call = apiService.getUsers("signin",username,password);
        call.enqueue(new Callback<UserResponse>() {
            @Override
            public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
                UserResponse result = response.body();
                //User data = result.getData();
                Log.d("userresponse",""+result.getMessage());

            }

            @Override
            public void onFailure(Call<UserResponse> call, Throwable t) {
                Log.d("res",""+t.getMessage());

            }
        });
    }

2 个答案:

答案 0 :(得分:1)

在Postman响应中,数据字段将作为JSON对象出现,而不是作为JSON数组出现。如果是这种情况,并且您在UserResponse模型中使用“数据”字段作为列表,则无法映射它。这是我认为的问题。要解决此问题,您可以使用以下UserResponse数据模型:

public class UserResponse {
    User data;

    public User getData() {
        return data;
    }

    public void setData(User data) {
        this.data = data;
    }

}

答案 1 :(得分:0)

错误说明全部为Expected BEGIN_ARRAY but was BEGIN_OBJECT -ANDROID RETROFIT2 ,即收到的响应或JSON是您尝试以阵列形式访问它的对象。

将您的服务器代码更改为

$response[] = array('status' => true,
            'message' => "Successfully logged in",
            'data' => [
              'access_token' => $accessToken,
              'refresh_token' => $refreshToken
            ],
          );

echo json_encode($response);

预期回应是

[{"status":true,"message":"Successfully logged in","data":{"access_token":null,"refresh_token":null}}]

这是一个数组