简体   繁体   English

如何向RESTful Web服务发出HttpPost请求,以传递带有包含值数组的参数的URL?

[英]How do I make a HttpPost request to RESTful webservice to pass an URL with parameters which includes an array of values?

I am trying to make a call to a RESTful webservice to get JSON object. 我正在尝试调用RESTful网络服务以获取JSON对象。 Now I tried making the call through HttpGet and was successful. 现在,我尝试通过HttpGet进行呼叫并成功。 The URL I need to pass was pretty much like this: http://example.com/ /def.xxx?Name=save&Code=sample&OrderDetails=[{“Count”: “2”, “ID”: “1”, “Price”: “5”}]. 我需要传递的网址非常像这样: http : //example.com/ /def.xxx?Name=save&Code=sample&OrderDetails=[{“Count”:“ 2”,“ ID”:“ 1”,“价格”:“ 5”}]。 I 一世

` `

StringBuilder URL = new StringBuilder("http://example.com/def.xxx?");
URL.append("Name="+name+"&");
URL.append("Code="+code+"&");
URL.append("Details=%5b");
            int val = 0;
            for (int i = 0; i<len; i++){
                    if (val > 0)
                    {URL.append(",");
                    }
                    else
                        val = 1;                
            URL.append(.....);
URLX = URL.toString();
httpGet = new HttpGet(URLX); 
response = client1.execute(httpGet);

` `

Now, what should I do if I want to make HttpPost call instead of HttpGet call? 现在,如果我想拨打HttpPost而不是HttpGet怎么办? I tried in this way, 我以这种方式尝试过

String URL = "http://example.com/def.xxx";

    DefaultHttpClient client1 = new DefaultHttpClient();
    HttpResponse response = null;
    HttpPost httpPost = new HttpPost();
    ArrayList<NameValue> postParameters;

    postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("Name", name));
        postParameters.add(new BasicNameValuePair("Code", code));

try {
            httpPost.setEntity(new UrlEncodedFormEntity(postParameters));
            } catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }


                response = client1.execute(httpPost);

} Now I am not sure how should I add the values pairs in Details=[{“Count”: “2”, “ID”: “1”, “Price”: “5”}] in the Post call and how should I execute it to get the same JSON object as I am getting while making HttpGet call. }现在,我不确定如何在邮寄电话会议中的Details = [{“ Count”:“ 2”,“ ID”:“ 1”,“ Price”:“ 5”}]中添加值对,以及应该如何添加我执行它以获取与进行HttpGet调用时所获得的JSON对象相同的JSON对象。 Please help. 请帮忙。

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

ArrayList<NameValuePair> postParameters;

postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("Name", name));
postParameters.add(new BasicNameValuePair("Code", sample));

To construct JSONArray or JSONObject You can checkit . 构造JSONArray或JSONObject可以选中它

postParameters.add(new BasicNameValuePair("OrderDetails",jOrderdetails));

httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();

EDIT:- 编辑:-

for OrderDetailsObject you can construct it as follows.. 对于OrderDetailsObject,可以按以下方式构造它。

JSONArray jOrderdetails = new JSONArray();
for(int i=0;i<len;i++){
JSONObject childObject = new JSONObject();
childObject.put("Count",countvalue);
childObject.put("ID",IDvalue);
childObject.put("Price",Pricevalue);
jOrderdetails.put(childObject).toString();
}

in the above shown way you can construct the JSONArray and then that object you need to pass as parameter. 以上面显示的方式,您可以构造JSONArray,然后构造需要作为参数传递的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM