Click here to Skip to main content
Rate this: bad
good
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: 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:
`
 
 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,
 
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 25-Jun-13 8:31am
Edited 4-Jul-13 17:12pm
(no name)41.5K
v3

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



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 4 Jul 2013
Copyright © CodeProject, 1999-2014
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