0

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?

Zoe
  • 23,712
  • 16
  • 99
  • 132
  • Your code expects an array at the top level, but actually the JSON contains a top level object. The array you are expecting is the value of the property `employees` in this top level object. – Henry Sep 15 '19 at 08:31
  • 1
    Duplicate question, please search in this platform before asking a question – Zafer Celaloglu Sep 15 '19 at 13:43

1 Answers1

0

Please use [click here][1]

API interface

public interface Api {
    String BASE_URL = "https://api.myjson.com/bins/";

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

}

to generate your models , chcek below code 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;
    }

}

Hero class

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

Mainactivity

 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();
                }
            });
        }
Emad Seliem
  • 563
  • 1
  • 2
  • 5