Good day. I use AsyncTask and use the POST method to send a HashMap<String, String> to the server without problems, but what about the int[] array? How to send an array correctly?
1 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 send 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 an array
$list = $_POST['intlist']; $obj = json_decode($list); |