当前位置: 首页 > 知识库问答 >
问题:

改装JsonArray recyclerview异常

瞿宏儒
2023-03-14
@GET("/users")
    Call<JSONResponse>getJSON();}
private UserInfo[] Info;

public  UserInfo[] getInfo(){
    return Info;
}}
@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。

我尝试更改为调用 >getJSON(); ,但得到java.lang.IllegalStateException:应为字符串,但在第19行第17列路径$[0].Company处为BEGIN_OBJECT

共有1个答案

班承恩
2023-03-14

使用此模型类

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());
    }
});
}
 类似资料:
  • 我想打一个改装电话,我得到了马尔福梅杰松例外。 这是我的电话: 这是我的界面: 调用队列进入onFailure方法,错误为"com.google.gson.stream.MalformedJsonExc0019:使用JsonReader.setLenient(true)在第1行第2列path$接受格式错误的JSON"。 我做了如下更改,以设置宽松: 然后,我有一个不同的错误:“com.google

  • 但是,如果web服务器没有回复任何东西(成功登录),那么通过改型,我会得到一个异常,因为是空的,web服务器回复的是一个简单的空字符串消息,而不是字符串数组。如果我将更改为,它就可以工作,但我不能,因为如果登录失败,它必须是一个字符串数组。

  • 我不熟悉GSON和改装,这是我的输出模型(仅供参考结构), 这是我的输出 这是我的改装代码 应为BEGIN_对象,但为BEGIN_数组“ 我尝试过用List替换RootObject

  • 我正在使用Retrofit 2.4.0向服务器发送请求。但有时服务器会阻止我的请求,如果它与另一个请求的时间戳以毫秒为单位相似。我需要一次发送一个请求: 请求A已发送 是否可以使用Reformation和OkHttp库创建这样的队列?

  • 问题内容: 我正在尝试通过android应用程序中的改造连接到Rest服务。我正在得到回应。但是,当服务有一些错误响应时,就会发生转换异常,现在我想根据响应主体执行一些操作。但是我得到的响应主体为NULL。但是改造日志中有一条错误消息。为什么会这样。 码: 在这里,我得到的。不知道为什么 我是否需要使用rest适配器进行设置,以便将响应传递到改造错误对象。 问题答案: 试试这个代码: 与 Retr

  • 我正在用Kotlin PHP MySql开发一个应用程序。 如果有人知道为什么我在调用createUser函数时会出现此异常“com.google.gson.stream.MalformedJsonException:使用JsonReader.setLenient(true)在第1行第1列路径接受格式错误的JSON”,我将不胜感激。 当我使用相同的api调用getUser函数时,我没有得到异常。