Android 应为BEGIN_数组,但得到NEGIN_对象

Android 应为BEGIN_数组,但得到NEGIN_对象,android,json,retrofit,Android,Json,Retrofit,我使用的是JSON服务器 用于改装 它表示它需要一个数组,并给出一个对象 它在toast预期的BEGIN\u数组中给出此异常,但它是BEGIN\u对象 Api是 public interface Api { String BASE_URL = "https://api.myjson.com/bins/"; @GET("kp9wz") Call<List<Hero>> getHero(); } 阶级英雄 public class Hero

我使用的是JSON服务器 用于改装

它表示它需要一个数组,并给出一个对象

它在toast预期的BEGIN\u数组中给出此异常,但它是BEGIN\u对象

Api是

public interface Api {

    String BASE_URL = "https://api.myjson.com/bins/";

    @GET("kp9wz")
    Call<List<Hero>> getHero();


}
阶级英雄

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;
    }
}
主要活动是

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();
            }
        });
    }

}
public类MainActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
改装改装=新改装.Builder()
.baseUrl(Api.BASE\u URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Api=改装.create(Api.class);
Call Call=api.getHero();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
列出英雄=response.body();
字符串dis=“”;
为(英雄h:英雄)
{
dis=dis+h.getFirstname();
}
TextView显示=findViewById(R.id.sample);
display.setText(dis);
}
@凌驾
失败时公共无效(调用调用,可丢弃的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();

}
英雄级

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;
    }
}
import com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
导入java.util.List;
公众阶级英雄{
@序列化名称(“员工”)
@暴露
私有列表员工=null;
公开名单{
返回员工;
}
公共雇员(列出雇员){
这是。雇员=雇员;
}
}
main活动

 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();
                }
            });
        }
void callService(){
改装改装=新改装.Builder()
.baseUrl(Api.BASE\u URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Api=改装.create(Api.class);
Call Call=api.getHero();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
英雄=反应。身体();
字符串dis=“”;
//TextView显示=findViewById(R.id.sample);
//display.setText(dis);
Log.v(“oops”,heroes.getEmployees().get(0.getFirstname()+”);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(getApplicationContext(),“Cool”+t.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}

您的代码需要顶级数组,但实际上JSON包含顶级对象。您期望的数组是此顶级对象中属性
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();
                }
            });
        }