Question:
Good afternoon. I am using AsyncTask
and using the POST
method I send a pair of HashMap<String, String>
to the server without any problem, but what about the int[]
array? How to send an array correctly?
Answer:
I think Json will help you
List<Integer> productIds = ...
JSONArray x = new JSONArray();
for(Integer productId : productIds){
x.put(productId);
}
or if int[]
JSONArray x = new JSONArray();
for(int k=0; k < size; k++){
x.put(productId[k]);
}
then we send it to the server. There are many methods, one option
try {
HttpClient client = new DefaultHttpClient();
String postURL = "http://somepostaddress.com";
HttpPost post = new HttpPost(postURL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("intlist", x.toString()));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
}
well, on the server, you can then transfer it back to the array
$list = $_POST['intlist'];
$obj = json_decode($list);