How to handle pagination with retrofit?
<p>I have an API response that I need to show on my listView. At the beginning It starts off with 15 items, and I want to add more as the user scrolls down and reaches the end of the list.
What would be the best strategy to page through the API automatically with Retrofit, so that all available data is downloaded by default? Unfortunately I am a beginner and this is as far as I can get. Please help! Any help would be appreciated.</p>

<p>These are the parameters from the response I need to use:</p>

<p><div class="snippet" data-lang="js" data-hide="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>"total": 181,
    "per_page": "15",
    "current_page": 1,
    "last_page": 13,
    "next_page_url": "URL",
    "prev_page_url": null,
    "from": 1,
    "to": 15,</code></pre>
</div>
</div>
</p>

<p>MainActivity:</p>

<p><div class="snippet" data-lang="js" data-hide="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://nmc.no-ip.org:8888/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();


        StreamingOfferService service = retrofit.create(StreamingOfferService.class);
        Call&lt;ResponseResult&gt; response = service.getStreamingOffers();


        response.enqueue(new Callback&lt;ResponseResult&gt;() {
            @Override
            public void onResponse(Call&lt;ResponseResult&gt; result, Response&lt;ResponseResult&gt; response) {
                createList(response);
            }

            @Override
            public void onFailure(Call&lt;ResponseResult&gt; result, Throwable t) {

            }


        });

    }

    public void createList(final Response&lt;ResponseResult&gt; response)

    {

        final ListView listview = (ListView) findViewById(R.id.listview);
        final StreamingAdapter adapter = new StreamingAdapter(this,R.layout.listofusers,response);
        listview.setAdapter(adapter);
        listview.setOnScrollListener(new AbsListView.OnScrollListener() {


            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {


            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                ResponseResult r = new ResponseResult();
                int currentPageNumber = r.getCurrentPage();
                boolean loading = true;
                String next_page_url = r.getNextPageUrl();

                if (listview.getLastVisiblePosition() == listview.getAdapter().getCount() - 1
                        &amp;&amp; listview.getChildAt(listview.getChildCount() - 1).getBottom() &lt;= listview.getHeight()) {
                    // r.getNextPageUrl();
                   // currentPageNumber++;

                    loading = true;
                    Retrofit retrofit = new Retrofit.Builder()
                            .baseUrl(r.getNextPageUrl())  // this is not working
                            .addConverterFactory(GsonConverterFactory.create())
                            .build();

                    StreamingOfferService service = retrofit.create(StreamingOfferService.class);
                    Call&lt;ResponseResult&gt; response = service.getStreamingOffers();

                    response.enqueue(new Callback&lt;ResponseResult&gt;() {
                        @Override
                        public void onResponse(Call&lt;ResponseResult&gt; result, Response&lt;ResponseResult&gt; response) {

                            // StreamingAdapter adapter = new StreamingAdapter(this,R.layout.listofusers, response);.
                        }

                        @Override
                        public void onFailure(Call&lt;ResponseResult&gt; result, Throwable t) {

                        }
                    });
                }}
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();


        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}</code></pre>
</div>
</div>
</p>

<p>The Adapter:</p>

<p><div class="snippet" data-lang="js" data-hide="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>public class StreamingAdapter extends BaseAdapter {

    private Context context;
    private int resource;
    private LayoutInflater inflater;
    private List&lt;StreamingOffer&gt; streamingOfferList;





    public StreamingAdapter(Context context, int resource, Response&lt;ResponseResult&gt; response) {
        this.context=context;
        this.resource=resource;
        this.inflater=LayoutInflater.from(context);
        streamingOfferList = response.body().getData();

    }

    void addToList(List&lt;ResponseResult&gt; updatedlist)
    {
        this.streamingOfferList.addAll(updatedlist);    // how do I add the new response here
        this.notifyDataSetChanged();
    }


    public class Holder extends User {

        private TextView full_name;
        private TextView location;
        private TextView price;
        private TextView currency_symbol;
        private TextView total_streamed;

        private  ImageView offer;
        private  ImageView profile;
        private  ImageView iconUser;
        private  ImageView iconStar;
        private  ImageView iconStar1;
        private  ImageView iconStar2;
        private  ImageView iconStar3;
        private  ImageView iconStar4;

    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Holder holder = new Holder();
        View itemView = convertView;

        StreamingOffer streamingOffer = getItem(position);
        User u = streamingOffer.getUser();

        if(itemView==null) {
            itemView = inflater.inflate(resource, parent, false);

            holder.full_name = (TextView) itemView.findViewById(R.id.full_name);
            holder.full_name.setText(u.getFullName());

            holder.location = (TextView) itemView.findViewById(R.id.location);
            holder.location.setText(streamingOffer.getLocation());


            holder.price = (TextView) itemView.findViewById(R.id.price);
            holder.currency_symbol = (TextView) itemView.findViewById(R.id.currency_symbol);

            if(streamingOffer.getIs_free()==0)
            {
                holder.price.setText(streamingOffer.getPrice());
                holder.currency_symbol.setText(streamingOffer.getCurrencySymbol());
            }
            else
                holder.price.setText("FREE");


            holder.total_streamed = (TextView) itemView.findViewById(R.id.total_streamed);
            holder.total_streamed.setText(u.getTotalStreamed());

            holder.offer = (ImageView) itemView.findViewById(R.id.offer);
            holder.profile = (ImageView) itemView.findViewById(R.id.profile);
            holder.iconUser = (ImageView) itemView.findViewById(R.id.iconUser);
            holder.iconStar = (ImageView) itemView.findViewById(R.id.iconStar);
            holder.iconStar1 = (ImageView) itemView.findViewById(R.id.iconStar1);
            holder.iconStar2 = (ImageView) itemView.findViewById(R.id.iconStar2);
            holder.iconStar3 = (ImageView) itemView.findViewById(R.id.iconStar3);
            holder.iconStar4 = (ImageView) itemView.findViewById(R.id.iconStar4);




            Images im = new Images();
            final GsonBuilder builder = new GsonBuilder();
            final Gson gson = builder.create();
            String jsonRepresentation = gson.toJson(im);

            if(jsonRepresentation.contains("75x75")){
                Picasso.with(context).load(im.getImage_75x75()).into(holder.profile);

            }


           //Picasso.with(context).load(jsonRepresentation).into(holder.offer);
            Picasso.with(context).load(im.getImage_154x154()).into(holder.profile);
            Picasso.with(context).load(im.getImage_154x154()).into(holder.offer);


            Picasso.with(context)
                    .load(R.drawable.icon_red_star).into(holder.iconStar);
            Picasso.with(context)
                    .load(R.drawable.half_red_star).into(holder.iconStar1);
            Picasso.with(context)
                    .load(R.drawable.icon_black_star).into(holder.iconStar2);
            Picasso.with(context)
                    .load(R.drawable.icon_black_star).into(holder.iconStar3);
            Picasso.with(context)
                    .load(R.drawable.icon_black_star).into(holder.iconStar4);

           // Picasso.with(context)
           //         .load("http://nmc.no-ip.org:8888/assets/images/offer-2.png").into(holder.offer);
           // Picasso.with(context)
             //       .load("http://nmc.no-ip.org:8888/assets/images/profile-1.png").into(holder.profile);


            if(u.getIs_online()==1) {
                Picasso.with(context)
                        .load(R.drawable.icon_user_online).into(holder.iconUser);
            }
            else
                Picasso.with(context)
                        .load(R.drawable.icon_user_offline).into(holder.iconUser);}




            if(u.getRating()!=null) {
                if (u.getRating() == "1.0" &amp;&amp; u.getRating() == "1.5") {
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar);
                    Picasso.with(context)
                            .load(R.drawable.icon_black_star).into(holder.iconStar1);
                    Picasso.with(context)
                            .load(R.drawable.icon_black_star).into(holder.iconStar2);
                    Picasso.with(context)
                            .load(R.drawable.icon_black_star).into(holder.iconStar3);
                    Picasso.with(context)
                            .load(R.drawable.icon_black_star).into(holder.iconStar4);

                }

                if (u.getRating() == "2.0" &amp;&amp; u.getRating() == "2.5") {
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar);
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar1);
                    Picasso.with(context)
                            .load(R.drawable.icon_black_star).into(holder.iconStar2);
                    Picasso.with(context)
                            .load(R.drawable.icon_black_star).into(holder.iconStar3);
                    Picasso.with(context)
                            .load(R.drawable.icon_black_star).into(holder.iconStar4);

                }
                if (u.getRating() == "3.0" &amp;&amp; u.getRating() == "3.5") {
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar);
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar1);
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar2);
                    Picasso.with(context)
                            .load(R.drawable.icon_black_star).into(holder.iconStar3);
                    Picasso.with(context)
                            .load(R.drawable.icon_black_star).into(holder.iconStar4);

                }
                if (u.getRating() == "4.0" &amp;&amp; u.getRating() == "4.5") {
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar);
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar1);
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar2);
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar3);
                    Picasso.with(context)
                            .load(R.drawable.icon_black_star).into(holder.iconStar4);

                }
                if (u.getRating() == "5.0") {
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar);
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar1);
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar2);
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar3);
                    Picasso.with(context)
                            .load(R.drawable.icon_red_star).into(holder.iconStar4);

                }
            }

        itemView.setTag(holder);
        return itemView;
    }


    @Override
    public int getCount() {

        return streamingOfferList.size();
    }

    @Override
    public StreamingOffer getItem(int position) {
        return streamingOfferList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }



}</code></pre>
</div>
</div>
</p>