简体   繁体   中英

How to fetch JSONArray using retrofit2 and RXJava2

I am using Retrofit2 and RxJava2 for fetching data from the server.I have response in below format:

 {
   "product_data": [
    {
        "product_name": "Pi-Pi jod kar ye Notebook li hai",
        "product_price": "249.00",
        "category_id": "9",
        "product_id": "125",
        "product_photo": "1574935198_1.jpg"
    },
    {
        "product_name": "Tere Jesa year kaha kaha esa yarana",
        "product_price": "249.00",
        "category_id": "9",
        "product_id": "128",
         "product_photo": "1574935455_1.jpg"
     }
    ]
   } 

I am getting exception below:

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $.

ApiService.class

public interface ApiService {

@GET("general_cat_api")
Observable<List<HomeGeneralModel>> getData();

}

RetrofitClient.class

public class RetrofitClient {

private static Retrofit retrofit = null;

public static Retrofit getInstance(){


    OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .connectTimeout(22, TimeUnit.SECONDS)
            .readTimeout(22, TimeUnit.SECONDS)
            .writeTimeout(22, TimeUnit.SECONDS)
            .build();

    if(retrofit == null)
        retrofit = new Retrofit.Builder()
                .baseUrl("http://aamku.com/api/")
                .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setLenient().create()))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(okHttpClient)
                .build();

    return retrofit;

}

private RetrofitClient(){

  }
}

Now I am using retrofit some thing like below to fetch data:

private void getData(){

    Retrofit retrofit = RetrofitClient.getInstance();
    ApiService myApi = retrofit.create(ApiService.class);

    myApi.getData().subscribeOn(Schedulers.io())
                   .observeOn(AndroidSchedulers.mainThread())
                   .subscribe(new Observer<List<MenuModel>>() {
                       @Override
                       public void onSubscribe(Disposable d) {

                       }

                       @Override
                       public void onNext(List<MenuModel> menuModels) {

                           if(menuModels.size() > 0){

                               menuProg.setVisibility(View.INVISIBLE);
                               list.addAll(menuModels);

                               adapter = new MenuAdapter(list,MainActivity.this);
                               menuRecycler.setAdapter(adapter);
                           }
                       }

                       @Override
                       public void onError(Throwable e) {

                           menuProg.setVisibility(View.INVISIBLE);
                           Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
                       }

                       @Override
                       public void onComplete() {

                       }
                   });
}

Someone please let me know why I am getting this exception.Any help would be appreciated.

THANKS

Your response format is wrong here

Observable<List<HomeGeneralModel>> getData();

Since you are getting a json object in response, you don't need the List. Use HomeGeneralModel directly in observable.

Your list of product_data should be inside of HomeGeneral Model

It should be like this

  Observable<HomeGeneralModel> getData();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM