I am using a JSON server http://api.myjson.com/bins/kp9wz for retrofit.
It says that it expects an array and it is giving an object.
It is giving this exception in the toast Expected BEGIN_ARRAY but was BEGIN_OBJECT
Api is
public interface Api {
String BASE_URL = "https://api.myjson.com/bins/";
@GET("kp9wz")
Call<List<Hero>> getHero();
}
json is here
http://api.myjson.com/bins/kp9wz
class hero
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;
}
}
and MainActivity is
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();
}
});
}
}
Can someone just tell me what I am doing wrong?