我正在使用JSON服务器http://api.myjson.com/bins/kp9wz 进行翻新。
它表示希望有一个数组,并且正在提供一个对象。
它在预期的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();
}
});
}
}
有人可以告诉我我在做什么错吗?
答案 0 :(得分:-1)
请使用[单击此处] [1]
API接口
public interface Api {
String BASE_URL = "https://api.myjson.com/bins/";
@GET("kp9wz")
Call<Hero> getHero();
}
生成模型,请参见以下代码 员工类别
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();
}
});
}