I am trying to send an array from Android to PHP. The array is a basic key-value pair (string, string) which consists of all the contacts from my device phonebook (name, phone_number). Here's the code snippet that does this:
HttpClient httpClient = new DefaultHttpClient();
// post header
HttpPost httpPost = new HttpPost(postReceiverUrl);
int i=0;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
for(i=0;i<item.size();i++) {
nameValuePairs.add(new BasicNameValuePair(item.get(i).getName(), item.get(i).getNumber()));
}
Log.v("Count", ""+i);
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// execute HTTP post request
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String responseStr = EntityUtils.toString(resEntity).trim();
Log.v("TAG", "Response: " + responseStr);
}
} catch (IOException e) {
e.printStackTrace();
}
On PHP end, the code is meant to accept the data in POST and echo it out. The code goes:
<?php
// Run script only if the dump array is received
if($_POST){
echo "server received: ".count($_POST);
// Read comma-separated text file
$arr = 0;
foreach($_POST as $names[$arr] => $numbers[$arr]) {
$numbers[$arr] = preg_replace('/[^0-9]/','',$numbers[$arr]);
$arr += 1;
}
for($i=0;$i<$arr;++$i) {
echo $i.". ".$numbers[$i]."-------".$names[$i]."\n";}
}
?>
This works perfect for smaller phonebooks. However, while running this on a phonebook with exactly 100 phone number entries, the echo dump shows only 87. What gives? I even tried using GET and REQUEST instead of POST but results remain the same.