【问题标题】:How to send JSONArray string to php server using multipart entity in android如何使用 android 中的多部分实体将 JSONArray 字符串发送到 php 服务器
【发布时间】:2014-08-08 07:23:10
【问题描述】:

我无法使用 android 中的多部分实体将 JSONArray 字符串发送到 php 服务器。我尝试了以下方法,但它不起作用:

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,BOUNDARY,Charset.defaultCharset());
entity.addPart("invite_friend", new StringBody(friendsArray));

在 PHP 服务器端应该是这样的:

'invite_friend' => array
    (
        0 => '800'
        1 => '794'
    )

有什么办法,请提出建议。

【问题讨论】:

  • 它不是JSONArray
  • "friendsArray" 在字符串值对中包含类似 ["800","794"]:"invite_friend":["800","794"]。如何使用 Multipart 发送此“["800","794"]" 数据。
  • 它是StringArray...不是JSONArray..

标签: php android multipartentity


【解决方案1】:

你可以这样做。

// Prepare Category Array
for (String mFrndsID : friendsArray) {
    reqEntity.addPart("invite_friend[]", new StringBody(mFrndsID));
}

只需在其中的循环中添加带有数组标记和 paas 值的 []。 这里的invite_friend 是数组标签。您可以使用循环在此标签中传递您的值。它将作为数组发布在服务器上。

更多详情请参考此答案

How to send the string array of values in one key word using post method to the server

这可能对你有帮助

【讨论】:

    【解决方案2】:

    尝试使用此代码将多个值对发送到服务器端脚本。

    String strUrl = "http://****/****.php";
    url = new URL(strUrl);
    
    HttpURLConnection connection = (HttpURLConnection) url
            .openConnection();
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
            connection.getOutputStream());
    
    outputStreamWriter.write("user_names_giver=" +user_names_giver + "&tcredit="+tcredit+"&user_name_receiver="+user_name_receiver);                
    outputStreamWriter.flush();
    outputStreamWriter.close();
    
    InputStream iStream = connection.getInputStream();
    BufferedReader reader = new BufferedReader(new
    InputStreamReader(iStream));
    
    StringBuffer sb = new StringBuffer();
    
    String line = "";
    
    while( (line = reader.readLine()) != null)
    {
        sb.append(line);
    }
    
    reader.close();
    iStream.close();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-03
      相关资源
      最近更新 更多