RxJava,Retrofit Error :Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

匿名 (未验证) 提交于 2019-12-03 00:59:01

问题:

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!