0

I'm using RxJava2 and Retrofit with ViewPager and I faced this error io.reactivex.exceptions.OnErrorNotImplementedException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

My code below Pojo class

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

interfaces

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

}

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

NetworkManager class

public class NetworkManager implements inNetworkManager { private inServices services;

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

}

} Adapter

public class QuetoPhotoPageAdapter extends 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 Answers2

1

This error occurs mostly when there is mismatch in the Pojo which is a object but your JSON format is of array. First create a pojo while will give list of photo

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

}

After this

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();
}
  • `{"heroes":[{"name":"Spiderman","imageurl":"https:\/\/simplifiedcoding.net\/demos\/view-flipper\/images\/spiderman.jpg"},{"name":"Iron Man","imageurl":"https:\/\/simplifiedcoding.net\/demos\/view-flipper\/images\/ironman.jpg"},{"name":"Thor","imageurl":"https:\/\/simplifiedcoding.net\/demos\/view-flipper\/images\/thor.jpg"},{"name":"Wolverine","imageurl":"https:\/\/simplifiedcoding.net\/demos\/view-flipper\/images\/wolverine.jpeg"}]}` – Yunus Rajabiy Jul 10 '18 at 11:57
  • check every field which is case sensitive i.e. you have written imageUrl which should be imageurl. or you can use @SerializedName("imageurl") private String imageUrl; – Sushil Chaudhary Jul 10 '18 at 12:00
  • if it worked correct the answer any error feel to ask again? – Sushil Chaudhary Jul 16 '18 at 04:28
0

You shoud chande your Pojo due to your JSON

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

So your interfcae will look like this

public interface inServices {
    @GET("heroes.php")
    Observable<HeroResponse> loadHeroes();
}
Anton A.
  • 1,718
  • 15
  • 37