android - 应为 BEGIN_ARRAY,但得到 NEGIN_OBJECT
问题描述
我正在使用 JSON 服务器 http://api.myjson.com/bins/kp9wz 进行改造。
它说它需要一个数组并且它正在给出一个对象。
它在 toast Expected BEGIN_ARRAY 中给出了这个异常,但是 BEGIN_OBJECT
API 是
public interface Api {
String BASE_URL = "https://api.myjson.com/bins/";
@GET("kp9wz")
Call<List<Hero>> getHero();
}
json在这里
http://api.myjson.com/bins/kp9wz
职业英雄
public class Hero {
String firstname;
int age;
String mail;
public String getFirstname() {
return firstname;
}
public int getAge() {
return age;
}
public String getMail() {
return mail;
}
public Hero(String firstname, int age, String mail) {
this.firstname = firstname;
this.age = age;
this.mail = mail;
}
}
和 MainActivity 是
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Api api = retrofit.create(Api.class);
Call<List<Hero>> call = api.getHero();
call.enqueue(new Callback<List<Hero>>() {
@Override
public void onResponse(Call<List<Hero>> call, Response<List<Hero>> response) {
List<Hero> heroes = response.body();
String dis = "";
for(Hero h:heroes)
{
dis = dis + h.getFirstname();
}
TextView display = findViewById(R.id.sample);
display.setText(dis);
}
@Override
public void onFailure(Call<List<Hero>> call, Throwable t) {
Toast.makeText(getApplicationContext(), "Cool"+t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
有人可以告诉我我做错了什么吗?
解决方案
请使用[点击这里][1]
API接口
public interface Api {
String BASE_URL = "https://api.myjson.com/bins/";
@GET("kp9wz")
Call<Hero> getHero();
}
要生成您的模型,请检查下面的代码 Employee Class
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Employee {
@SerializedName("firstname")
@Expose
private String firstname;
@SerializedName("age")
@Expose
private Integer age;
@SerializedName("mail")
@Expose
private String mail;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
}
英雄等级
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class Hero {
@SerializedName("employees")
@Expose
private List<Employee> employees = null;
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
}
主要活动
void callService(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Api api = retrofit.create(Api.class);
Call<Hero> call = api.getHero();
call.enqueue(new Callback<Hero>() {
@Override
public void onResponse(Call<Hero> call, Response<Hero> response) {
Hero heroes = response.body();
String dis = "";
// TextView display = findViewById(R.id.sample);
// display.setText(dis);
Log.v("oops",heroes.getEmployees().get(0).getFirstname()+" ");
}
@Override
public void onFailure(Call<Hero> call, Throwable t) {
Toast.makeText(getApplicationContext(), "Cool"+t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
推荐阅读
- python - 需要 Python 的 Pandas 的 DataFram.Replace 帮助
- asp.net-core-mvc - 为什么在脚手架代码中发布编辑操作时,会根据模型/隐藏输入中的 ID 检查 URL 中的 ID?
- ffmpeg - GStreamer H264 流到 HTML5 网页
- javascript - React Native 在抽屉导航屏幕上显示标题和后退按钮
- autohotkey - AutohotKey 控件 + 逗号在浏览器的新窗口中打开选项卡
- r - 如何校准具有点(不是概率)作为变量的模型?
- android - 如何创建在背景中有圆圈和在顶部有图像的布局?
- android - 使用 flutter_bluetooth_serial 在两个(或更多)Flutter 应用程序之间进行通信
- r - 数据框中各列的最大组合总和
- css - 无限水平动画:如何使其平滑?