Java 如何将字符串数组作为基本名称值对作为 HTTPPOST 发送?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21131015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:48:01  来源:igfitidea点击:

How to send a String array as basic name value pair as HTTPPOST?

javaandroidjsonhttp

提问by goonerDroid

I want to send a array as name value pair as httppost.My server accepts only array values.The following is my code snippet..

我想发送一个数组作为名称值对作为 httppost。我的服务器只接受数组值。以下是我的代码片段..

public String SearchWithType(String category_name, String[] type,int page_no) {

    String url = "http://myURL";
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);

    String auth_token = Login.authentication_token;
    String key = Login.key;

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("authentication_token",
                auth_token));
        nameValuePairs.add(new BasicNameValuePair("key", key));
        nameValuePairs.add(new BasicNameValuePair("category_name",
                category_name));
        int i = 0;
        nameValuePairs.add(new BasicNameValuePair("type", type[i]));
        nameValuePairs.add(new BasicNameValuePair("page", String.valueOf(page_no)));

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        eu = EntityUtils.toString(entity).toString();

    } catch (IOException ioe) {
        String ex = ioe.toString();
        return ex;
    }

    return eu;
} 

采纳答案by goonerDroid

I got the issue. Here's how:

我得到了这个问题。就是这样:

try {
    int i = 0;

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("authentication_token", auth_token));
    nameValuePairs.add(new BasicNameValuePair("key", key));
    nameValuePairs.add(new BasicNameValuePair("category_name", category_name));
    nameValuePairs.add(new BasicNameValuePair("type", type[i]));
    nameValuePairs.add(new BasicNameValuePair("page", String.valueOf(page_no)));

    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();

    eu = EntityUtils.toString(entity).toString();
} catch (Exception e) {
    Log.e(TAG, e.toString());
}

All I had to do was initialize a loop:

我所要做的就是初始化一个循环:

for (int i = 0; i < type.length; i++) {
    nameValuePairs.add(new BasicNameValuePair("type[]",type[i]));
}

回答by Autocrab

nameValuePairs.add(new BasicNameValuePair("type", Arrays.toString(type)));

回答by learner

convert from array to string and then send using http post,again server side parse from String to array

从数组转换为字符串,然后使用 http post 发送,服务器端再次从字符串解析为数组

回答by Burak Durmu?

json_array = [{param1:"param1Value", param2:"param2Value"}] if you want send a json array with nameValuePairs you can send like this;

json_array = [{param1:"param1Value", param2:"param2Value"}] 如果你想发送一个带有 nameValuePairs 的 json 数组,你可以这样发送;

new BasicNameValuePairs("param[0][param1]","param1Value")
new BasicNameValuePairs("param[0][param2]","param2Value")