Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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: http://abc.com/ /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:
`

Java
StringBuilder mURL = new StringBuilder("http://abc.com/Ordering.ashx?");
   mURL.append("Name="+name+"&");
   mURL.append("Code="+sample+"&");
   mURL.append("OrderDetails=%5b");
               int val = 0;
               for (int i = 0; i<len; i++){
                       if (val > 0)
                       {mURL.append(",");
                       }
                       else
                           val = 1;
               mURL.append("%7b");
                   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,

Java
String URL = http://abc.com/Ordering.ashx";

        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
                    e1.printStackTrace();
                }

                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
Updated 4-Jul-13 16:12pm
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900