1

您好我正在尝试将 ArrayList 作为 POST 请求的参数从我的 Android 应用程序发送到服务器。到目前为止,我有这个代码:

HttpResponse response;

        List<NameValuePair> postParams = new ArrayList<NameValuePair>(2);
        postParams.add(new BasicNameValuePair("post[text]", text));
        postParams.add(new BasicNameValuePair("post[common_comments]", String.valueOf(commonComments));
        postParams.add(new BasicNameValuePair("post[wall_ids]", wallIds);

        UrlEncodedFormEntity encodedParams;
        try {
            encodedParams = new UrlEncodedFormEntity(postParams);
            post.setEntity(encodedParams);
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }

但 BasicNameValuePair 只接收 String 作为值。有什么方法可以发送一个 ArrayList 作为 post[wall_ids] 的值吗?

提前致谢。

4

1 回答 1

1

布埃诺迪亚斯卡拉。
我不知道你是否解决了这个问题(两个月是很多时间)......但也许它可以帮助你:

 for(String wallyID: wallyIDs)
      postParams.add(new BasicNameValuePair("extraFields[wallyIDs][]", wallyID));

如果您想使用拉丁字符(这会在服务器上忘记),使用“utf 8”格式也会很好,例如:

  HttpClient httpclient = new DefaultHttpClient(); 
  HttpPost httppost = new HttpPost(url);
  httppost.setEntity(new UrlEncodedFormEntity(postParams, HTTP.UTF_8));

我有一个关于 List 构造函数的问题......你为什么使用 2 作为它的参数?

PS:我想发表评论而不是回答,因为我不知道它是否可以按您的意愿工作。

于 2015-02-05T10:46:20.507 回答