This issue is been perplexing me for a while and I can't figure the issue. I have to admit I normally program in php and not Java, so I would really appreciate your help.

So my java code below makes a REST GET request, retrieves the xml and outputs it into a file. It also paginates through if the REST service has more than 100 records. The code works fine if we donít paginate. But when I do paginate it does not return the correct result.

So lets say there are 100 records in the REST service and result per page is 10. My java code is supposed to output 10 xml files, but instead only outputs 4 files.

The rest service says:

Parameters
● currentPage - the page of results to get. If not specified assume the first page.
● resultsPerPage - how many results are returned per page.

Metadata
The following meta data will be returned
● totalResults - the total number of results
● resultsPerPage - the number of results per page
● currentPage - the current page number of results
● lastPage - the page number of the last page of result

Sample xml:

<?xml version="1.0" encoding="UTF-8" ?>
<results />
<metaData>
<totalResults>100</totalResults>
<resultsPerPage>10</resultsPerPage>
<currentPage>1</currentPage>
<lastPage>10</lastPage>
</metaData>

my code

Code:
int currentPage; //THIS SHOULD BE USED FOR PAGINATION
int totalPages; //THIS WILL BE THE TOTAL NUMBER OF PAGES AS RETURNED IN THE METADATA IN THE FIRST ITERATION
String outputFolder = "/xxxx/xxxx/xxxx/";

 

public String getData(String brandSiteCode, String fromDate, String toDate, int cp) {

        currentPage = cp;

        String s="";

        String response ="";

        //need to rename filename to get rid of colons.

        String filename = "Test2" + brandSiteCode + "_" + fromDate.replace(":","-") + "_" + toDate.replace(":","-") + "_" + currentPage + ".xml";

        String metaData = "";

 
//replace : with - , since this isn't a good character to use in a filename.

  try

  {

    URL url;

    HttpURLConnection urlConn;

    InputStream dis;
      
 String myURL = "http://xxxxxx.stg.xxxxx/v1/user-records/?brandSiteCode="+brandSiteCode+"&fromDate=" + fromDate + "&currentPage=" + currentPage;

 
//logInfo("getting data for this date range: " + myURL);

    url = new URL(myURL);

 
    urlConn = (HttpURLConnection)url.openConnection();

    urlConn.setDoInput(true);

    urlConn.setRequestMethod("GET");

    urlConn.setUseCaches(false);

    urlConn.setRequestProperty("Accept","application/xml");

    //dis = new DataInputStream(urlConn.getInputStream());
 

    int status = urlConn.getResponseCode();

    System.out.println("Status code:" + status);

    //if http code 200 is not returned, there's an error.

    if (status!=200) {

        dis = new DataInputStream(urlConn.getErrorStream());

       }else {

        dis= new DataInputStream(urlConn.getInputStream());

      }

    InputStreamReader inReader = new InputStreamReader(dis, "utf-8");

    StringBuffer responseBuffer = new StringBuffer();


if (status!=200) {   

    failSession("HTTP status code wasn't 200, so must be something wrong - FAILED");

    }


    char[] buffer = new char[1024];

    int bytes;

    while ((bytes = inReader.read(buffer)) != -1) {

        responseBuffer.append(buffer, 0, bytes);

    }

        response = responseBuffer.toString();

    int metaDataIndex = response.indexOf("<metaData");

    metaData = response.substring(metaDataIndex,response.length());

    response = response.substring(0,metaDataIndex);

 

    //THE FOLLOWING FOUR LINES SHOULD WORK BUT IS UNTESTED:

    metaDataIndex = metaData.indexOf("<lastPage>")+10;

    metaData = metaData.substring(metaDataIndex,metaData.length()); //the number of pages will now be at the start of metaData

    metaDataIndex= metaData.indexOf("<");

    totalPages=Integer.parseInt(metaData.substring(0,metaDataIndex));

    FileOutputStream out = new FileOutputStream(outputFolder + filename);

    PrintStream p = new PrintStream(out);

    p.println(response);
 
    System.out.println("metadata:" + metaData);

    p.close();

    dis.close();

 
// PAGINATION

    currentPage++;

    while (currentPage<=totalPages) {

        getData(brandSiteCode,fromDate, toDate, currentPage); 
		
}
        //currentPage++;

    }

        catch (Exception e) {e.printStackTrace();}

 

       return response;

  }