【问题标题】:Send array by volley android to php通过凌空android将数组发送到php
【发布时间】:2017-01-13 11:11:42
【问题描述】:

我正在尝试编写一个小代码,通过 volley android 将我的 android 应用程序中的数组发送到 php 文件,这将在文本文件中写入数组,这是我的数组

>String[] title = {
                    "Abundance",
                    "Anxiety",
                    "Bruxism",
                    "Discipline",
                    "Drug Addiction"
            };

我的截击码:

>UploadRequest registerRequest = new UploadRequest(title, responseListener);
            RequestQueue volleyQueue = Volley.newRequestQueue(MainActivity.this);
            DiskBasedCache cache = new DiskBasedCache(getCacheDir(), 64 * 2048 * 2048);
            volleyQueue = new RequestQueue(cache, new BasicNetwork(new HurlStack()));
            volleyQueue.start();
            volleyQueue.add(registerRequest);

我的 stringRequest 类 UploadRequest.java

public class UploadRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL ="http://192.168.2.115/app/Register.php";
private Map<String, String> params;

public UploadRequest(String[] names, Response.Listener<String> listener) {
    super(Method.POST, REGISTER_REQUEST_URL, listener, null);
    params = new HashMap<>();
    
    params.put("names", String.valueOf(names));//i think my problem is here!

}

@Override
public Map<String, String> getParams() {
    return params;
}

}

我的 Register.php 文件

$value = $_POST['names'];

$fp = fopen('Log.txt', 'w');

fwrite($fp, $value);

fclose($fp);

来自数组的文本文件'Log.txt'中php文件的输出

[Ljava.lang.String;@41c509a8

所以我的问题是数组出错或者我没有声明数组!那么请问我该如何声明呢。

谢谢大家

【问题讨论】:

    标签: java android json


    【解决方案1】:

    您需要发送数组值,而不是参考哈希码,因此请使用Arrays.toString

     params.put("names", Arrays.toString(names));
    

        String[] title = {
                "Abundance",
                "Anxiety",
                "Bruxism",
                "Discipline",
                "Drug Addiction"
        };
        System.out.println(Arrays.toString(title));
        System.out.println(String.valueOf(title));
    

    输出:

    [Abundance, Anxiety, Bruxism, Discipline, Drug Addiction]
    [Ljava.lang.String;@14eac69
    

    选项 2。

    您可以创建JSONARRAY并将值放入JSONARRAY,然后使用jsonarray.toString()

        JSONArray array = new JSONArray();
        array.put("Abundance");
        array.put("Anxiety");
        array.put("Bruxism");
        System.out.println(array.toString());
    

    输出:

    ["Abundance","Anxiety","Bruxism"]
    

    【讨论】:

    • @Steve.r 我很高兴,我能帮上忙,编码愉快:)
    • 现在我正在尝试通过单独的行在 php 上获取此数组 ["Abundance","Anxiety","Bruxism"],将其一一插入到 mysql 中,看起来很难一点点。
    • 我已经接受了,再次感谢并希望找到php解决方案。
    【解决方案2】:
    params.put("names", String.valueOf(names));//i think my problem is here!
    

    确实如此。最好尝试以下方法:

    params.put("names[0]", names[0]);
    params.put("names[1]", names[1]);
    params.put("names[2]", names[2]);
    .... of course you would make a loop for it
    

    或者:

    params.put("names0", names[0]);
    params.put("names1", names[1]);
    params.put("names2", names[2]);
    ......
    

    对于这两种情况,您的 php 脚本会有所不同。

    【讨论】:

      猜你喜欢
      • 2018-08-16
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      • 2017-09-20
      • 1970-01-01
      • 1970-01-01
      • 2022-06-17
      相关资源
      最近更新 更多