Click here to Skip to main content
11,933,253 members (65,348 online)
Rate this:
Please Sign up or sign in to vote.
I am trying to make a call to a RESTful webservice to get JSON object. Now I tried making the call through HttpGet and was successful. The URL I pass through HttpGet right now is pretty much like this: /Ordering.ashx?Name=save&Code=sample&OrderDetails=[{“Count”: “2”, “ID”: “1”, “Price”: “5”}, {“Count”: “2”, “ID”: “2”, “Price”: “5”}].

And the way I am doing it:

StringBuilder mURL = new StringBuilder("");
               int val = 0;
               for (int i = 0; i<len; i++){
                       if (val > 0)
                           val = 1;
                   mURL.append("%22Count%22"+":"+ "%22" + count +"%22"+",");
                       mURL.append("%22ID%22"+":"+ "%22" + id +"%22"+",");
                       mURL.append("%22Price%22"+":"+ "%22" + price +"%22"+",");
   URL = mURL.toString();
   httpGet = new HttpGet(URL);
   response = client1.execute(httpGet);

Now, what should I do if I want to make HttpPost call instead of HttpGet call? I tried in this way,

String URL =";

        DefaultHttpClient client1 = new DefaultHttpClient();
        HttpResponse response = null;
        HttpPost httpPost = new HttpPost();
        ArrayList<NameValuePair> postParameters;
        postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("Name", name));
            postParameters.add(new BasicNameValuePair("Code", sample));
                inParameters = new ArrayList<NameValuePair>();
                 for (int i = 0; i<len; i++){
                       inParameters.add(new BasicNameValuePair("Count", count+i));                    inParameters.add(new BasicNameValuePair("ID", id+i)); }
postParameters.add(new BasicNameValuePair("OrderItemDetails", inParameters.toString()));
    try {
                httpPost.setEntity(new UrlEncodedFormEntity(postParameters, HTTP.UTF_8));
                } catch (UnsupportedEncodingException e1) {
                    // TODO Auto-generated catch block
                try {
                    response = client1.execute(httpPost);

Instead of inParameters = new ArrayList<NameValuePair>(), I also tried with Bundle b = new Bundle() and onwards. Nothing works!
Now I am not sure how should I add the values pairs in OrderDetails=[{“Count”: “2”, “ID”: “1”, “Price”: “5”}, {“Count”: “2”, “ID”: “2”, “Price”: “5”}] in the Post call and how should I execute it to get the same JSON object in as I am getting while making HttpGet call. I am also not sure if I need to add HttpHeader for this. I am working on a android project. I am a new programmer, so not very known to these issues. Please help.
Posted 25-Jun-13 8:31am
Edited 4-Jul-13 17:12pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151126.1 | Last Updated 4 Jul 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100