Android RxJava2改造和ViewPager

时间:2018-07-10 11:47:29

标签: android retrofit

我正在将RxJava2和Retrofit与ViewPager一起使用,但遇到此错误 io.reactivex.exceptions.OnErrorNotImplementedException:应为BEGIN_ARRAY,但在第1行第2列路径$ BEGIN_OBJECT中

我的下面的代码 Pojo课

public class Photo {
    private String name;
    private String imageUrl;

    public Photo() {
    }

    public String getName() {
        return name;
    }

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

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }
}

界面

public interface inNetworkManager {
Observable<Photo[]> loadPhotos();

}

public interface inServices {
    @GET("heroes.php")
    Observable<Photo[]> loadPhotos();
}

NetworkManager类

NetworkManager实现inNetworkManager {     私人inServices服务;

public NetworkManager() {
    initData();
}

private void initData() {
    OkHttpClient client = new OkHttpClient.Builder()
            .build();
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(CommonsKt.getBASE_URL())
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build();
    services = retrofit.create(inServices.class);
}

@Override
public Observable<Photo[]> loadPhotos() {
    return services.loadPhotos()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread());

}

} 适配器

公共类QuetoPhotoPageAdapter扩展了PagerAdapter {

private ArrayList<Photo> list;
private LayoutInflater inflater;
private Context context;
private AppCompatImageView imQueto;

public QuetoPhotoPageAdapter(ArrayList arrayList, Context context) {
    this.list = arrayList;
    this.context = context;
    inflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    return list.size();
}

public void addPhotos(Photo[] photos) {
    list.clear();
    notifyDataSetChanged();
    Collections.addAll(list, photos);
    notifyDataSetChanged();
}

@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
    View view = inflater.inflate(R.layout.item_view, container, false);
    imQueto = view.findViewById(R.id.imQuote);
    Picasso.get()
            .load(String.valueOf(list.get(position)))
            .into(imQueto);

    container.addView(view, 0);
    return view;
}

@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
    return view.equals(object);
}

}

MainActivity

public class MainActivity extends BaseActivity {
private ArrayList<Photo> list;
private QuetoPhotoPageAdapter adapter;


@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    addNet();
    init();
}
/*new Consumer<Photo[]>() {
                @Override
                public void accept(Photo[] photos) throws Exception {
                    adapter.addPhotos(photos);
                }
            }*/

private void addNet() {
    inNetworkManager manger = ((App) getApplication()).getManager();
    Disposable d = manger.loadPhotos()
            .subscribe(photos -> adapter.addPhotos(photos));
    addDispose(d);

}
 private void init() {
        adapter = new QuetoPhotoPageAdapter(list, MainActivity.this);
    }

2 个答案:

答案 0 :(得分:1)

此错误主要是在作为对象的Pojo中存在不匹配但您的JSON格式为数组的情况下发生的。首先创建一个pojo,同时会提供照片列表

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("heroes")
@Expose
private List<Hero> heroes = null;

public List<Hero> getHeroes() {
return heroes;
}

public void setHeroes(List<Hero> heroes) {
this.heroes = heroes;
}

}

此后

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Hero {

@SerializedName("name")
@Expose
private String name;
@SerializedName("imageurl")
@Expose
private String imageurl;

public String getName() {
return name;
}

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

public String getImageurl() {
return imageurl;
}

public void setImageurl(String imageurl) {
this.imageurl = imageurl;
}

}
And 


  public interface inServices {
    @GET("heroes.php")
    Observable<Example> loadHeroes();
}

答案 1 :(得分:0)

由于您的JSON,您应该颂扬Pojo

public class Hero {
   @SerializedName("name")
   @Expose
   private String name;
   @SerializedName("imageurl")
   @Expose
   private String imageurl;
}

public class HeroResponse {
   @SerializedName("heroes")
   @Expose
   private ArrayList<Hero> heroes = new ArrayList<>();
}

所以您的界面看起来像这样

public interface inServices {
    @GET("heroes.php")
    Observable<HeroResponse> loadHeroes();
}