Nitish Dash Nitish Dash - 1 year ago 123
JSON Question

How to change the RequestInterface dynamically while parsing limited JSON using Retrofit?

I am trying to incorporate the endless recyclerview by populating it with data fetched from a json source. Now, I am using the Wordpress JSON API.

The problem is that, my JSON is very large. Also, instead of loading all the data in the RecyclerView at once, I want the user to scroll down in order to fetch new data.

To get limited JSON, I am appending the &offset=10 tag. But, the problem is that every time the user scrolls down, I need to change the @GET url...

But my Interface looks like this:

import retrofit2.Call;
import retrofit2.http.GET;

public interface RequestInterface {

Call<JSONResponse> getJSON();

So, when I pass the request code from my MainActivity:

public void getFeed() {

Call<JSONResponse> listCall = mManager.getFlowerService().getAllFlowers();
listCall.enqueue(new Callback<JSONResponse>() {

public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
if (response.isSuccess()) {
flowerList = new ArrayList<>(Arrays.asList(response.body().getPosts()));

for (int i = 0; i < flowerList.size(); i++) {
ModelClass flower = flowerList.get(i);

SaveIntoDatabase task = new SaveIntoDatabase();

} else {
int sc = response.code();
switch (sc) {
case 400:
Log.e("Error 400", "Bad Request");
case 404:
Log.e("Error 404", "Not Found");
Log.e("Error", "Generic Error");

public void onFailure(Call<JSONResponse> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();



How can I change the offset value by passing an integer to the interface?

Answer Source

Pass the offset value as a parameter to your method:

public interface RequestInterface {

    Call<JSONResponse> getJSON(@Query("offset") int offset);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download