Search code examples
androidjsonurihttpurlconnectionandroidhttpclient

Why can't I read next page of json data from website into json buffer?


I m doing book finder android app, it fetches data from website of ebooks, displays 10 books per page, when I press next button it should display next 10 books.

http://it-ebooks-api.info/v1/search/java&type=title

The above url gives me a json array of books on the first page, it looks like this:

{"Error":"0","Time":0.0043,"Total":"327","Page":1,"Books":[{"ID":1542146786,"Title":"Java Phrasebook","Description":"Java Phrasebook gives you the code phrases............................... and so on

this url - http://it-ebooks-api.info/v1/search/java&type=title&page=5

should give me next chunk of json data on page 5

I do build valid url but my httpUrlconnection method that reads using GET request method always gives me only first page - even if I form url for page 5.

This is the method that establishes URL connection and stores into buffer from given URL:

private String MakeConnectionAndStoreBufferData(String validUrl){
HttpURLConnection urlConnection = null;
BufferedReader reader = null;


    URL url = new URL(validUrl); 
    // even if this url has &page=5 at the end
    // it still fetches data for page = 0

    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.connect();

    InputStream inputStream = urlConnection.getInputStream();

    StringBuffer buffer = new StringBuffer();

    reader = new BufferedReader(new InputStreamReader(inputStream));

    String line;
    while((line = reader.readLine()) != null) {
        buffer.append(line + "\n");
    }

    return buffer.toString();

EDIT: at Rami, here I show how i actually build my URL addresses - I do check them after I build them, they look as expected:

public class ProcessData extends AsyncTask<String, Void, String> {
private String createValidURL(String query, int page_counter) {
    Log.d(LOG_TAG, "create Valid url......");

    String BASE_URL = "http://it-ebooks-api.info/v1/search";

    String resultUrl = null ;
    if (page_counter == 0){

        destinationURL = Uri.parse(BASE_URL).buildUpon()
                .appendPath(query)
                .appendQueryParameter("type", "title")
                .build();

    resultUrl = destinationURL.toString();


}
   else if(page_counter > 0){

        destinationURL = Uri.parse(BASE_URL).buildUpon()
                .appendPath(query)
                .appendQueryParameter( "type", "title")
                .appendQueryParameter("page",  Integer.toString(page_counter) )
                .build();

        resultUrl = destinationURL.toString();

    }
    Log.d(LOG_TAG, "before we return url, it is :" + resultUrl);
    return resultUrl ;
}


public String doInBackground(String ...params) {

    String validUrl = createValidURL(params[0], 5);//5 is page counter        
    String result_buffer = MakeConnectionAndStoreBufferData(validUrl);
    return result_buffer ;
    }
    }

This is mainactivity.java, I do search for books with title "java":

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    // create valid URL, connect to it-ebooks website, read buffer, parse buffer to return complete list of books

    ProcessData data = new ProcessData();
    data.execute("java");//this calls doInBackground in processData class!
    }

Solution

  • http://it-ebooks-api.info/v1/search/java?type=title&page=5
    

    After careful observation i found the mistake - it was about ? sign after word java.

    Basically you can write ....java?type=title&pa and it will still load the first page! the website is "forgiving" and will return the first page by default if the query is illformed - if page is missing, incomplete etc

    I could not find it other way! the only problem is the java ? -question sign, anyways, nobody could help me here, i m glad i found the bug myself!

    I should make sure my url has replaced ? with &.

    p.s. be careful about creating urls: append query parameters may make almost correct URL.