![](/img/trans.png)
[英]Send json array to add multiple records in mysql to php script using volley in android
[英]Send array by volley android to php
我正在嘗試編寫一個小代碼,通過 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);
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;
}
}
$value = $_POST['names'];
$fp = fopen('Log.txt', 'w');
fwrite($fp, $value);
fclose($fp);
[Ljava.lang.String;@41c509a8
所以我的問題是數組出錯了或者我沒有聲明數組! 所以請我如何聲明它。
謝謝大家
您需要發送數組值,而不是參考哈希碼,因此請使用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"]
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 腳本會有所不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.