0

I'm trying to implement pagination in android.currently i have a service which returns jsonarray.

protected JSONArray doInBackground(String... params) {

    JSONArray jsonArray = null; 
    String result = ServerConnect.readService("http://myservice.com/resbook_redberry/mobile/GetKitchenOrder");

    try {
        jsonArray = new JSONArray(result);
    } catch (Exception e) {
        e.printStackTrace();
        try {
            jsonArray = new JSONArray(result.substring(3));
        } catch (JSONException e1) {
            e1.printStackTrace();
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }
    return jsonArray;
}

but now I need to send page number to the web service and get JSONArray as response.


Do I need to write HTTP post? what things should I change to make this happen?

1 Answer 1

3
Try this: here i am using gson library to parse json : https://code.google.com/p/google-gson/downloads/detail?name=google-gson-2.2.4-release.zip&can=1&q=

**With GET Method**

    protected SignUpResponseJson doInBackground(String... params) {

        urlString = "http://www.xxxxx.com?page=1"
        url  =  new URL(urlString);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setUseCaches(false);
        connection.setConnectTimeout(Integer.parseInt(context.getResources().getString(R.string.timeout))); //set timeout to 5 seconds
        connection.setReadTimeout(Integer.parseInt(context.getResources().getString(R.string.timeout))); //set timeout to 5 seconds

        connection.connect();

        HttpURLConnection httpConnection  =  (HttpURLConnection) connection;
        int responseCode  =  httpConnection.getResponseCode();
        //      Log.i("test-responsecode",String.valueOf(responseCode));
        if (responseCode   ==   HttpURLConnection.HTTP_OK) {
            //          Log.i("test","inside if");
            InputStream in  =  httpConnection.getInputStream();
            Gson gson  =  new Gson();
            Reader r  =  new InputStreamReader(in);

            Type DataType  =  new TypeToken<SignUpResponseJson>() {
            }.getType();

            signUpResponseJsons  =  gson.fromJson(r,DataType);
            Log.i(TAG, gson.toJson(signUpResponseJsons).toString());

        }

    }

OR With Post Method

http://fahmirahman.wordpress.com/2011/04/26/the-simplest-way-to-post-parameters-between-android-and-php/

public void postData(){  
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.yourdomain.com/post.php");  

        try {
            // Add your data
            List nameValuePairs = new ArrayList(1);
            nameValuePairs.add(new BasicNameValuePair("data1", "dataValue"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

            InputStream is = response.getEntity().getContent();

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your answer..but what i need is posting page number to the server and get the json array according to that request. Are you posting any values to server before getting the json?
yes..but unfortunately it not accepts parameters via url. is there any option to send parameters to server other than url?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.