改造JsonArray recyclerview Exception

时间:2017-12-31 09:48:52

标签: java android json retrofit

我试图从http://jsonplaceholder.typicode.com/users获取数据并将其显示在recyclerview中。我不需要api的所有专栏

public interface APIService {

@GET("/users")
    Call<JSONResponse>getJSON();}

public class JSONResponse {

private UserInfo[] Info;

public  UserInfo[] getInfo(){
    return Info;
}}

。     公共类UserInfo {

@SerializedName("id")
private String id;
@SerializedName("name")
private String name;
@SerializedName("city")
private String city;
@SerializedName("company")
private String company;
@SerializedName("email")
private String email;
@SerializedName("phone")
private String phone;
@SerializedName("lat")
private double lat;
@SerializedName("lng")
private double lng;
@SerializedName("website")
private String website;

//getter methods }

主要活动

private void loadJSON() {
    Call<JSONResponse>call = apiService.getJSON();
    call.enqueue(new Callback<JSONResponse>() {
        @Override   
        public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {


            JSONResponse jsonResponse = response.body();                  
            data = new ArrayList<>(Arrays.asList(jsonResponse.getInfo()));
            adapter = new DataAdapter(data);
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onFailure(Call<JSONResponse>call,Throwable t) {
            Log.d("Error", t.getMessage());
        }
    });
}

我得到了java.lang.IllegalStateException:预期BEGIN_OBJECT但是在第1行第2行路径$ BEGIN_ARRAY。

我尝试更改为Call<List<UserInfo>> getJSON();但是我得到了java.lang.IllegalStateException:期望一个字符串,但是第19行第17行路径为$ [0] .company

的BEGIN_OBJECT

1 个答案:

答案 0 :(得分:0)

使用此模型类

public class UserInfo {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("username")
@Expose
private String username;
@SerializedName("email")
@Expose
private String email;
@SerializedName("address")
@Expose
private Address address;
@SerializedName("phone")
@Expose
private String phone;
@SerializedName("website")
@Expose
private String website;
@SerializedName("company")
@Expose
private Company company;

public Integer getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

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 Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getWebsite() {
return website;
}

public void setWebsite(String website) {
this.website = website;
}

public Company getCompany() {
return company;
}

public void setCompany(Company company) {
this.company = company;
}

}

然后像这样改变界面

public interface APIService {

@GET("/users")
Call<List<UserInfo>> getJSON();
}

您的网络通话功能应该是这样的

private void loadJSON() {
Call<List<UserInfo>> call = apiService.getJSON();
call.enqueue(new Callback<List<UserInfo>>() {
    @Override   
    public void onResponse(Call<List<UserInfo>> call, Response<List<UserInfo>> response) {


        List<UserInfo> user = response.body();                  
        data = new ArrayList<>(user);
        adapter = new DataAdapter(data);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onFailure(Call<List<UserInfo>> call,Throwable t) {
        Log.d("Error", t.getMessage());
    }
});
}