I'm trying to write a small code that send array from my android application by volley android to php file a that will write the array in text file here is my array
>String[] title = {
"Abundance",
"Anxiety",
"Bruxism",
"Discipline",
"Drug Addiction"
};
my volley code:
>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);
my stringRequest class 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;
}
}
my Register.php file
$value = $_POST['names'];
$fp = fopen('Log.txt', 'w');
fwrite($fp, $value);
fclose($fp);
the output of the php file in the text file 'Log.txt' that came from the array
[Ljava.lang.String;@41c509a8
So my problem the array comes wrong or I didnt declare the array! so please how can I declare it.
Thanks for all
You need to send array values , instead of reference hash code so use 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));
Output :
[Abundance, Anxiety, Bruxism, Discipline, Drug Addiction]
[Ljava.lang.String;@14eac69
Option 2.
You can create JSONARRAY
and put values into JSONARRAY
and later use jsonarray.toString()
JSONArray array = new JSONArray();
array.put("Abundance");
array.put("Anxiety");
array.put("Bruxism");
System.out.println(array.toString());
output :
["Abundance","Anxiety","Bruxism"]
params.put("names", String.valueOf(names));//i think my problem is here!
Indeed. Better try something like:
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
Or:
params.put("names0", names[0]);
params.put("names1", names[1]);
params.put("names2", names[2]);
......
Your php script would be different for the two cases.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.