RetroFit:java.lang.IllegalStateException:预期BEGIN_OBJECT但在第1行第2行路径$ BEGIN_ARRAY $

时间:2016-02-19 19:53:56

标签: java android

我目前正在使用RetroFit,并且是该工具的新手。当我运行我的应用程序时,我遇到标题中所述的问题。它没有崩溃,但它不返回我需要的信息。我附上了我的代码。非常感谢您的帮助。

public class MainActivity extends AppCompatActivity {
    @Bind(R.id.textView_userid) TextView textView_userid;
    @Bind(R.id.textView_email) TextView textView_email;
    @Bind(R.id.textView_password) TextView textView_password;
    @Bind(R.id.textView_firstName) TextView textView_firstname;
    @Bind(R.id.textView_lastname) TextView textView_lastname;
    @Bind(R.id.refresh_button) Button refresh_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

    }

    @OnClick(R.id.refresh_button)
    public void onClick_Refresh(){


        UserApi.Factory.getInstance().getUsers().enqueue(new Callback<User>() {
            @Override
            public void onResponse(Response<User> response, Retrofit retrofit) {
                textView_userid.setText(response.body().getUserId());
                textView_email.setText(response.body().getEmail());
                textView_firstname.setText(response.body().getFirstName());
                textView_lastname.setText(response.body().getLastName());
                Log.e("Info", response.body().toString());
                Log.v("Get", response.body().toString());
            }

            @Override
            public void onFailure(Throwable t) {
                Log.e("failed", t.getMessage());

            }
        });
    }
    @Override
    protected void onResume(){
        super.onResume();
        onClick_Refresh();
    }
}


public interface UserApi {
    String baseUrl = "http://someUrl/";

    @GET("users")
    Call<User> getUsers();

    class Factory{
        private static UserApi service;
        public static UserApi getInstance(){
            if(service == null){
                Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(baseUrl).build();
                service = retrofit.create(UserApi.class);
                return service;
            }else{
                return service;
            }
        }
    }
}

@Generated( “org.jsonschema2pojo”) 公共类用户{

@SerializedName("userId")
@Expose
private int userId;
@SerializedName("email")
@Expose
private String email;
@SerializedName("firstName")
@Expose
private String firstName;
@SerializedName("lastName")
@Expose
private String lastName;

/**
 * 
 * @return
 *     The userId
 */
public int getUserId() {
    return userId;
}

/**
 * 
 * @param userId
 *     The userId
 */
public void setUserId(int userId) {
    this.userId = userId;
}

/**
 * 
 * @return
 *     The email
 */
public String getEmail() {
    return email;
}

/**
 * 
 * @param email
 *     The email
 */
public void setEmail(String email) {
    this.email = email;
}

/**
 * 
 * @return
 *     The firstName
 */
public String getFirstName() {
    return firstName;
}

/**
 * 
 * @param firstName
 *     The firstName
 */
public void setFirstName(String firstName) {
    this.firstName = firstName;
}

/**
 * 
 * @return
 *     The lastName
 */
public String getLastName() {
    return lastName;
}

/**
 * 
 * @param lastName
 *     The lastName
 */
public void setLastName(String lastName) {
    this.lastName = lastName;
}

}

[{"userId":1,"email":"asd@asd.asd","password":"abc123","firstName":"bob","lastName":"joe"}]

3 个答案:

答案 0 :(得分:0)

您提供的JSON片段显示您正在接收数组而不是对象尝试更改以下代码:

UserApi.Factory.getInstance().getUsers().enqueue(new Callback<User>() {
            @Override
            public void onResponse(Response<User> response, Retrofit retrofit) {

            }
            @Override
            public void onFailure(Throwable t) {
                Log.e("failed", t.getMessage());

            }
        });

对于这样的事情:

UserApi.Factory.getInstance().getUsers().enqueue(new Callback<List<User>>() {
            @Override
            public void onResponse(Response<List<User>> response, Retrofit retrofit) {
                    List<User> users = response.body;
                    // Do something with the rest
                }
            @Override
            public void onFailure(Throwable t) {
                    Log.e("failed", t.getMessage());

            }
        });

答案 1 :(得分:0)

首先,将 onResponse 的正文更改为

 @Override
        public void onResponse(Response<List<User>> response, Retrofit retrofit) {
                List<User> users = response.body;

            }

其次,对于确定jsonObject或jsonArray的位置,您可以使用此网站 you can get equivalent POJO for any Json response.

答案 2 :(得分:0)

谢谢你们的帮助。我对界面和主要活动进行了一些更改。结果如下。

    UserApi.Factory.getInstance().getUsers().enqueue(new Callback<List<User>>() {
        @Override
        public void onResponse(Response<List<User>> response, Retrofit retrofit) {
            for(User user: response.body()){
            Log.v("Endpoint Response: ", user.toString());
                textView_userid.setText(String.valueOf(user.getUserId()));
                textView_email.setText(user.getEmail());
                textView_firstname.setText(user.getFirstName());
                textView_lastname.setText(user.getLastName());
            }

        }

        @Override
        public void onFailure(Throwable t) {
            Log.e("failed", t.getMessage());

        }
    });


@GET("users")
Call<List<User>> getUsers();

class Factory{
    private static UserApi service;
    public static UserApi getInstance(){
        if(service == null){
            Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(baseUrl).build();
            service = retrofit.create(UserApi.class);
            return service;
        }else{
            return service;
        }
    }
}