【问题标题】:Sending array from android to php. $_POST shows Array() string将数组从 android 发送到 php。 $_POST 显示 Array() 字符串
【发布时间】:2014-09-27 18:17:47
【问题描述】:

来自android的调用如下

CallWebServiceTask task = new CallWebServiceTask(6, this,
                "Fetching Data From Server");
        int key = 0;
        for(int i = 0; i < localIds.size(); i++) {
           key = localIds.keyAt(i);
           // get the object by the key.
           task.addNameValuePair("ids", ""+key);
        }

        task.execute(url);

这会为键ids添加一个id列表。

当我这样做时,在我的 php 中,print_r($_POST); 它只返回 Array() 作为字符串。

如何,我如何将php中的数组值提取到一个数组中。谢谢。

【问题讨论】:

    标签: php android arrays


    【解决方案1】:

    只需将您的数组编码为JSONArray 并将其发布到您的PHP 服务器,这样使用起来更容易。

    CallWebServiceTask task = new CallWebServiceTask(6, this,
                    "Fetching Data From Server");
    task.addNameValuePair("ids", new JSONArray(localIds));
    task.execute(url);
    

    【讨论】:

    • 究竟如何?我应该不使用名称值对吗?
    • 是的,使用它,摆脱循环,因为您正在覆盖帖子键
    • 所以你想让我发送 {id1 : 1, id2 : 2, id3 : 3} 而不是 {ids : 1,2,3}?
    • 取决于或喜欢这个 { "ids" : [ 1, 2, 3] } ,但最重要的是以 JSON 格式发送
    • 但是顺便说一句,我已经做到了,它应该是一个 json 对吧?并且 json 应该看起来与您评论中提到的完全一样。还是我在这里弄错了。
    【解决方案2】:
    CallWebServiceTask task = new CallWebServiceTask(6, this,
                    "Fetching Data From Server");
            int key = 0;
            for(int i = 0; i < localIds.size(); i++) {
               key = localIds.keyAt(i);
               // get the object by the key.
               task.addNameValuePair("ids"+i, ""+key);
            }
    
            task.execute(url);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多