当前位置: 首页 > 知识库问答 >
问题:

RxJava,改装错误:应为BEGIN\u数组,但在第1行第2列路径处为BEGIN\u对象$

翟高明
2023-03-14

我正在使用Retrofit和RxJava库并尝试解析此url:

我的代码如下。

APIInterface.java

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

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

我的模型课如下

大学生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;
} }

我正在

D/错误:java。lang.IllegalStateException:应为BEGIN\u数组,但在日志的第1行第2列路径$处为BEGIN\u对象

我到处都在搜索这个错误,但找不到关于RxJava和改型的任何信息。

共有1个答案

国兴贤
2023-03-14

添加一个类,并按如下方式使用它。

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

主要活动。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);
      }
    });
  }
 类似资料: