php - 使用 Android Volley 将数组发布到 PHP

标签 php android mysql android-volley

这就是我使用 Volley 发布值的方式:

@Override
protected Map<String, String> getParams() {

   JSONObject jsonObjectMembers=new JSONObject();
   for (int i=0; i<arr_added_userids.size(); i++) {
       try {
            jsonObjectMembers.put("params_"+i,arr_added_userids.get(i));
       } catch (JSONException e) {
         e.printStackTrace();
       }
   }

   Map<String, String> params = new HashMap<String, String>();
   params.put("host", session_userid);
   params.put("params",jsonObjectMembers.toString());
   return params;
}

这是我尝试过的:

foreach($_POST['params'] as $key => $value)
{
  try {
        $stmt = $conn->prepare("INSERT INTO CREWMEMBERS (CREWID, MEMBER) VALUES
        (:crewid, :member)");
            $query_params = array( 
                ':crewid' => $crewid,
                ':member' => $value     
            );  
            $stmt->execute($query_params); 
            $response["success"] = 1;
        } catch(PDOException $e) {
        //echo 'ERROR: ' . $e->getMessage();
        $response["success"] = 3;
    }
}

我总是得到 org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

当我通过电子邮件将 $_POST['params'] 发送给自己以查看其外观时,我得到了

{"params_0":"000000000284","params_1":"000000000229","params_2":"000000000081"}

如何分别处理这些元素?

最佳答案

我刚遇到同样的问题。我的解决方案:

Map<String, String> params = new HashMap<String, String>();
   params.put("params[0]", "a");
   params.put("params[1]", "b");
   params.put("params[2]", "c");

关于php - 使用 Android Volley 将数组发布到 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29460536/

相关文章:

php - mysql特殊日期输出格式

php - 将信息从 javascript 发送到 php 文件

iphone - 如何使用C语言向数据库中插入数据?

php - 如何修复以获取 MySql 中的特定数据

php - 为什么在 PHP 中使用 "lock"文件而不是仅仅计算进程数?

php - 我如何使用 belongsto laravel 进行搜索?

android - Android Support Library的CardView在不同设备上的不同结果

android:从 ListView 中删除项目无法正常工作

android - 将 flutter 从 2.5 升级到 2.8 后出错

php - 在Mysql中连接三个表以生成报表