首页 > 解决方案 > 应为 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();
            }
        });
    }

}

有人可以告诉我我做错了什么吗?

标签: androidjsonretrofit

解决方案


请使用[点击这里][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();
                }
            });
        }

推荐阅读