gpt4 book ai didi

android - 改造抛出错误预期为 BEGIN_ARRAY 但为 BEGIN_OBJECT

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:09 29 4
gpt4 key购买 nike

您好,我是 Retrofit 库的新手,我在解析某些 json 时遇到问题。我已经查看了 Stackoverflow 上的其他一些解决方案,但对我的问题不太满意。我试图让一个简单的网络服务工作。任何建议将不胜感激..

Json文件

{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}

请求方式

public void requestEmployeeData(String uri){
RestAdapter adapter = new RestAdapter.Builder().setEndpoint(ENDPOINT).build();
EmployeesAPI employeesAPI =adapter.create(EmployeesAPI.class);
employeesAPI.getEmployees(new Callback<List<Employees.employeesclass>>() {
@Override
public void success(List<Employees.employeesclass> employees, Response response) {
List<String> names = new ArrayList<String>();
Log.v("nas", "The Employees Webservice Success" + response);
}

@Override
public void failure(RetrofitError retrofitError) {
Log.v("nas", "The Employees Webservice Failed " + retrofitError);
}
});
}

员工.java

public class Employees {
// List<String> listOfStrings = new ArrayList<String>();
@SerializedName(value="employees")
public List<Employees> employees;

public void setEmployees(List<Employees> employees) {
this.employees = employees;

}

public static class employeesclass {
String firstName;
String lastName;


@Override
public String toString() {
return (firstName + " " + lastName);

}
}

}

员工API.java

public interface EmployeesAPI {
@GET("/get_names.json")
public void getEmployees(Callback<List<Employees.employeesclass>> response);
}

我得到的错误是

The Employees Webservice Failed retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

我知道错误是说数据是一个对象,它应该以数组开头,但我无法解决。谢谢

最佳答案

您正在混合使用 Employeesemployeesclass 类。尝试改用它:

public class Employees {

@SerializedName(value="employees")
public List<employeesclass> employees;

public void setEmployees(List<employeesclass> employees) {
this.employees = employees;

}

// employeesclass definition

}

然后相应地改变你的接口(interface)和requestEmployeeData方法

public interface EmployeesAPI {
@GET("/get_names.json")
public void getEmployees(Callback<Employees> response);
}

关于android - 改造抛出错误预期为 BEGIN_ARRAY 但为 BEGIN_OBJECT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26266240/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com