本文介绍了HTTP POST阵列PARAMS的Java / Android版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中构建一个HTTP POST查询我可以用一个叫做简单的方法:http_build_query将返回基于传递给函数在阵列上以下内容:

简单数组:

 阵列

    [0] =>富
    [1] =>酒吧
    [2] =>巴兹
    [3] =>繁荣
    [牛] =>牛奶
    [PHP] =>超文本处理器

返回:

<$p$p><$c$c>flags_0=foo&flags_1=bar&flags_2=baz&flags_3=boom&cow=milk&php=hypertext+processor

有点更复杂的数组:

 阵列

[用户] =&GT;排列
    (
        [名] =&GT;鲍勃·史密斯
        [年龄] =&GT; 47
        [性爱] =&GT;中号
        [DOB] =&GT; 1956年5月12日
    )[消遣] =&GT;排列
    (
        [0] =&GT;高尔夫球
        [1] =&GT;歌剧
        [2] =&GT;扑克
        [3] =&GT;敲击
    )[儿童] =&GT;排列
    (
        [鲍比] =&GT;排列
            (
                [年龄] =&GT; 12
                [性爱] =&GT;中号
            )        [出击] =&GT;排列
            (
                [年龄] =&GT; 8
                [性爱] =&GT; F
            )    )[0] =&GT; CEO

返回:

<$p$p><$c$c>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的后者实体格式?我已尝试以下没有任何运气:

 列表&LT;&的NameValuePair GT; namevaluepairs中=新的ArrayList&LT;&的NameValuePair GT;(3);
nameValuePairs.add(新BasicNameValuePair(用户,NULL));
nameValuePairs.add(新BasicNameValuePair(名字,管理员));
nameValuePairs.add(新BasicNameValuePair(姓氏,管理员));
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));

希望有人知道如何做到这一点:)
亲切的问候,
莫滕

编辑:

基本上我需要的是产品在Java中对应的PHP的:

  $ PARAMS =阵列('用户'=&GT;阵列(
    '名字'=&GT; 鲍勃·史密斯,
    '姓氏'=&GT; 强生
));

这是JSON格式相同的请求:

  {用户:{名字:鲍勃·史密斯,姓氏:强生}}

我只需要在Java相当于应用程序/ x-WWW的形式urlen codeD格式)

BTW。非常感谢回答须藤code,真的appriciate它!


解决方案

我知道了。这里的样本code:

 字符串数据= EntityUtils.toString(新UrlEn codedFormEntity(namevaluepairs中,UTF-8));
        数据=放大器; + data.replaceAll(%5B0%5D,[0]);
        StringEntity本身=新StringEntity(数据,UTF-8);
        se.setContentType(应用程序/ x-WWW的形式urlen codeD);
        se.setContentEncoding(UTF-8);
        httppost.setEntity(SE);

一个有点讨厌,但它的工作。您可能想要得到它更通用的一点 - 这是只有1个元素的数组

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!

解决方案

I've got it. Here's the sample code:

        String data = EntityUtils.toString(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
        data = "&" + data.replaceAll("%5B0%5D", "[0]");
        StringEntity se = new StringEntity(data, "UTF-8");
        se.setContentType("application/x-www-form-urlencoded");
        se.setContentEncoding("UTF-8");
        httppost.setEntity(se);

A bit nasty, but it's working. You may want to get it a bit more generic - this is for 1-element array only.

这篇关于HTTP POST阵列PARAMS的Java / Android版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 17:28