在HTTP POST参数设置 [英] Setting parameters in HTTP POST

查看:1101
本文介绍了在HTTP POST参数设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让使用JSON调用服务器HttpClient的API。在code sinppet如下图所示。

I am trying make a JSON call to a server using HttpClient API. The code sinppet is shown below.

HttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpPost(URLString);
HttpResponse response = httpClient.execute(httpPost);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);  
nameValuePairs.add(new BasicNameValuePair("method", "completeUserLogin")); 
String[] params = new String[]{"100408"};
response = httpClient.execute(httpPost);

我要PARAMS添加到namevaluepairs中。 BasicNameValuePair类不允许您添加阵列。有什么想法?

I want to add params to nameValuePairs. BasicNameValuePair class does not allow you to add arrays. Any thoughts?

在此先感谢!

推荐答案

如果你要发送JSON格式的数据,那么你不应该张贴这样的PARAMS。相反,创建一个JSONObject把这些值在JSON对象,并从JSON对象得到一个字符串,然后创建一个StringEntity,而这个实体设置为HttpPost对象。

If you are posting data in json format then you should not post params like this. Instead create a JSONObject put these values in that json object, and get a string from that json object and then create a StringEntity, and set this Entity to HttpPost object.

有关请求创建的JSONObject:

Creating JSONObject for the Request:

JSONObject json=new JSONObject();
json.put("method", "completeUserLogin");
JSONArray arr= new JSONArray();
arr.put("100408");
json.put("params", arr);

String params=json.toString();

这篇关于在HTTP POST参数设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆