RxJava,Retrofit Error :Expected BEGIN_ARRAY but wa

2019-05-07 05:44发布

问题:

I am using Retrofit and RxJava Library and trying to parse this url:

My code is as below.

APIInterface.java

public interface APIService {
    @GET("/bins/tdze5")
    Observable<List<Student>> getdata();
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

  private RecyclerView recyclerView;
  private List<Student> list;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.recyclerview);

    getData();
  }

  private void getData() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://api.myjson.com")
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build();
    APIService apiService = retrofit.create(APIService.class);

    Observable<List<Student>> observable = apiService.getdata().subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread());

    observable.subscribe(new Observer<List<Student>>() {
      @Override
      public void onCompleted() {
      }

      @Override
      public void onError(Throwable e) {
        Log.d("error",e.toString());
      }

      @Override
      public void onNext(List<Student> students) {
        list = new ArrayList<>();
        Log.d("response size", String.valueOf(students.size()));
        for(int i = 0; i < students.size(); i++){
          Student student = new Student();
          student.setId(students.get(i).getId());
          student.setName(students.get(i).getName());
          list.add(student);
        }

        RecyclerAdapter recyclerAdapter = new RecyclerAdapter(list);
        RecyclerView.LayoutManager recyce = new LinearLayoutManager(MainActivity.this);
        recyclerView.setLayoutManager(recyce);
        recyclerView.setItemAnimator( new DefaultItemAnimator());
        recyclerView.setAdapter(recyclerAdapter);
      }
    });
  }
}

My Model Class is as below

Student.java

public class Student {

private String id;

private String name;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
} }

I am getting

D/error: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ in Log

I have searched everywhere about this error but I couldn't find anything about RxJava and Retrofit.

回答1:

Add this one more class and use it like below.

public class ResponseClass {

    @SerializedName("students")
    @Expose
    private List<Student> students = null;

    public List<Student> getStudents() {
    return students;
    }

    public void setStudents(List<Student> students) {
    this.students = students;
    }

    }

APIInterface.java

public interface APIService {
    @GET("/bins/tdze5")
    Observable<ResponseClass> getdata();
}

MainActivity.java

private void getData() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://api.myjson.com")
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build();
    APIService apiService = retrofit.create(APIService.class);

    Observable<ResponseClass> observable = apiService.getdata().subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread());

    observable.subscribe(new Observer<ResponseClass>() {
      @Override
      public void onCompleted() {
      }

      @Override
      public void onError(Throwable e) {
        Log.d("error",e.toString());
      }

      @Override
      public void onNext(ResponseClass response) {
        list = new ArrayList<>();
        List<Student> students = response.getStudents();
        Log.d("response size", String.valueOf(students.size()));
        for(int i = 0; i < students.size(); i++){
          Student student = new Student();
          student.setId(students.get(i).getId());
          student.setName(students.get(i).getName());
          list.add(student);
        }

        RecyclerAdapter recyclerAdapter = new RecyclerAdapter(list);
        RecyclerView.LayoutManager recyce = new LinearLayoutManager(MainActivity.this);
        recyclerView.setLayoutManager(recyce);
        recyclerView.setItemAnimator( new DefaultItemAnimator());
        recyclerView.setAdapter(recyclerAdapter);
      }
    });
  }