Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse a nested json with retrofit 2.0

i have this json's and i want to use retrofit for parsing them.

{
  "status": "true",
  "data": [
{
  "id": "1",
  "title": "Hi :)",
  "text": "<p>121212</p>",
  "cat_id": "1",
  "username": "admin",
  "coin": "0",
  "datetime": "1451508880",
  "isShow": "1"
},
{
  "id": "3",
  "title": " Hi :)",
  "text": "Hi :)",
  "cat_id": "2",
  "username": "Hi :)",
  "coin": "20",
  "datetime": "1451508880",
  "isShow": "1"
},
{
  "id": "4",
  "title": "a",
  "text": "someText",
  "cat_id": "1",
  "username": "admin",
  "coin": "10",
  "datetime": "1451982292",
  "isShow": "1"
}
  ]
}

when that json is in array mode my code work. but my question is how parse nested json like above sample?

here my javaClass: (statusClass)

public class retroStatus {

@SerializedName("status")
private String status;

@SerializedName("data")
private ArrayList<retroPost> data;

  //getters And Setters
}

(retroPost Class)

public class retroPost {

@SerializedName("id")
private int id;

@SerializedName("title")
private String title;

@SerializedName("text")
private String text;

@SerializedName("cat_id")
private int cat_id;

@SerializedName("datetime")
private long datetime;


 //getters And Setters
}

(getPostInterface)

public interface getPost {
@GET("get/posts/all")
Call<List<retroStatus>> getPost();
}

(and mainCode)

  Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://mywebsite.it/api/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    getPost postService = retrofit.create(getPost.class);
    Call<List<retroStatus>> call = postService.getPost();
    Callback<List<retroStatus>> callback = new Callback<List<retroStatus>>() {
        @Override
        public void onResponse(Response<List<retroStatus>> response, Retrofit retrofit) {
            if (response.isSuccess()) {
                for (retroStatus status : response.body()) {
                    Log.e("test", "nowStatusIs: " + status.getStatus());
                }
            } else {
                Log.e("test", "Failed");
            }


        }

        @Override
        public void onFailure(Throwable t) {
            Log.e("test", "onFailure");
        }
    };
    call.enqueue(callback);

now when run the app, onFailure happend but if main json just in onejsonArray and use just retroPost for parsing that thats work's very nice... what i mistake ?
sorry for bad english...

like image 751
javadroid Avatar asked Jan 05 '16 21:01

javadroid


2 Answers

Root node of your json is not array, but you declared it as List<responseStatus> at interface getPost. So you need to use responseStatus instead current one. And use gettter to access data

@SerializedName("data")
private ArrayList<retroPost> data;

which is array

like image 103
gio Avatar answered Nov 14 '22 15:11

gio


I see many people face this issue so I post my own way with Retrofit here is what I've done it's so simple and clean :

create ServiceHelper Class :

public class ServiceHelper {

private static final String ENDPOINT = "http://test.com";

private static OkHttpClient httpClient = new OkHttpClient();
private static ServiceHelper instance = new ServiceHelper();
private IPlusService service;


private ServiceHelper() {

    Retrofit retrofit = createAdapter().build();
    service = retrofit.create(IPlusService.class);
}

public static ServiceHelper getInstance() {
    return instance;
}

private Retrofit.Builder createAdapter() {

    httpClient.setReadTimeout(60, TimeUnit.SECONDS);
    httpClient.setConnectTimeout(60, TimeUnit.SECONDS);
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    httpClient.interceptors().add(interceptor);

    return new Retrofit.Builder()
            .baseUrl(ENDPOINT)
            .client(httpClient)
            .addConverterFactory(GsonConverterFactory.create());
}

public Call<List<CategoryModel>> getAllCategory() {
    return service.getAllCategory();
}

}

Then create your Service class in my Case it's IPlusService

    public interface IPlusService {

    @GET("/api/category")
    Call<List<CategoryModel>> getAllCategory();
}

Now in your Fragment/Activity class you call your own method with something like this :

ServiceHelper.getInstance().getAllCategory().enqueue(new Callback<List<CategoryModel>>() {
        @Override
        public void onResponse(Response<List<CategoryModel>> response, Retrofit retrofit) {
            processResponse(response);
        }

        @Override
        public void onFailure(Throwable t) {
            processResponse(null);
        }
    });

Also add following dependency to your gradle :

dependencies {

      compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
    compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
    compile 'com.squareup.okhttp:logging-interceptor:2.6.0'

}
like image 3
Amir Avatar answered Nov 14 '22 14:11

Amir