Java/Android 的 HTTP POST 数组参数
在 PHP 中构建 HTTP POST 查询时,我可以使用一个名为 http_build_query 的简单方法,它将根据传递给函数的数组返回以下内容:
简单数组:
Array
(
[0] => foo
[1] => bar
[2] => baz
[3] => boom
[cow] => milk
[php] => hypertext processor
)
返回:
flags_0=foo&flags_1=bar&flags_2=baz&flags_3=boom&cow=milk&php=hypertext+processor
< strong>稍微复杂一点的数组:
Array
(
[user] => Array
(
[name] => Bob Smith
[age] => 47
[sex] => M
[dob] => 5/12/1956
)
[pastimes] => Array
(
[0] => golf
[1] => opera
[2] => poker
[3] => rap
)
[children] => Array
(
[bobby] => Array
(
[age] => 12
[sex] => M
)
[sally] => Array
(
[age] => 8
[sex] => F
)
)
[0] => CEO
)
返回:
user%5Bname%5D=Bob+Smith&user%5Bage%5D=47&user%5Bsex%5D=M&user%5Bdob%5D=5%2F12%2F1956&pastimes%5B0%5D=golf&pastimes%5B1%5D=opera&pastimes%5B2%5D=poker&pastimes%5B3%5D=rap&children%5Bbobby%5D%5Bage%5D=12&children%5Bbobby%5D%5Bsex%5D=M&children%5Bsally%5D%5Bage%5D=8&children%5Bsally%5D%5Bsex%5D=F&flags_0=CEO
我要问的是,有没有办法在Java/Android中创建后一种实体格式?我尝试了以下方法,但没有任何运气:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("user", null));
nameValuePairs.add(new BasicNameValuePair("firstname", "admin"));
nameValuePairs.add(new BasicNameValuePair("lastname", "admin"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
希望有人知道如何实现这一目标:) 亲切的问候, Morten
编辑:
基本上我需要的是生成该 PHP 的 Java 等效项:
$params = array('user' => array(
'firstname' => 'Bob Smith',
'lastname' => 'Johnson'
));
这与 JSON 格式的请求相同:
{"user":{"firstname":"Bob Smith","lastname":"Johnson"}}
我只需要 application/x-www-form-urlencoded 格式的 Java 等效项;)
顺便说一句。非常感谢您回答 sudocode,非常感谢!
When building a HTTP POST Query in PHP I can use a simple method called: http_build_query which will return the following based on the array passed to function:
Simple array:
Array
(
[0] => foo
[1] => bar
[2] => baz
[3] => boom
[cow] => milk
[php] => hypertext processor
)
Returns:
flags_0=foo&flags_1=bar&flags_2=baz&flags_3=boom&cow=milk&php=hypertext+processor
A Bit more complex array:
Array
(
[user] => Array
(
[name] => Bob Smith
[age] => 47
[sex] => M
[dob] => 5/12/1956
)
[pastimes] => Array
(
[0] => golf
[1] => opera
[2] => poker
[3] => rap
)
[children] => Array
(
[bobby] => Array
(
[age] => 12
[sex] => M
)
[sally] => Array
(
[age] => 8
[sex] => F
)
)
[0] => CEO
)
Returns:
user%5Bname%5D=Bob+Smith&user%5Bage%5D=47&user%5Bsex%5D=M&user%5Bdob%5D=5%2F12%2F1956&pastimes%5B0%5D=golf&pastimes%5B1%5D=opera&pastimes%5B2%5D=poker&pastimes%5B3%5D=rap&children%5Bbobby%5D%5Bage%5D=12&children%5Bbobby%5D%5Bsex%5D=M&children%5Bsally%5D%5Bage%5D=8&children%5Bsally%5D%5Bsex%5D=F&flags_0=CEO
What I'm asking is, is there any way to create the latter entity format in Java/Android? I've tried the following without any luck:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("user", null));
nameValuePairs.add(new BasicNameValuePair("firstname", "admin"));
nameValuePairs.add(new BasicNameValuePair("lastname", "admin"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Hopefully someone know how to achieve this :)
Kind regards,
Morten
EDIT:
Basically what i need is to to product the Java equivalent of this PHP:
$params = array('user' => array(
'firstname' => 'Bob Smith',
'lastname' => 'Johnson'
));
And this is the same request in JSON format:
{"user":{"firstname":"Bob Smith","lastname":"Johnson"}}
I just need the Java equivalent in application/x-www-form-urlencoded format ;)
BTW. Thanks alot for answering sudocode, really appriciate it!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我明白了。这是示例代码:
有点令人讨厌,但它可以工作。您可能想让它更通用一些 - 这仅适用于 1 元素数组。
I've got it. Here's the sample code:
A bit nasty, but it's working. You may want to get it a bit more generic - this is for 1-element array only.
我认为您的代码是正确的,但您对它应该产生的输出的期望是不正确的。我认为您不应该期望参数名称包含在方括号中(urlencoded:%5B 和%5D)。
如果您需要,那么您需要将括号添加到您的 Java 代码中,例如
I think your code is correct, but your expectation of what output it should produce is incorrect. I don't think you should expect your parameter names to be surrounded in square brackets (urlencoded: %5B and %5D).
If you need that, then you will need to add the brackets to your Java code, e.g.