0

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.

Vinit Shandilya
  • 1,505
  • 5
  • 22
  • 41

1 Answers1

0

It looks like issues is in form content type, which is application/x-www-form-urlencoded by default. Here is more information on form content types:

The content type "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text containing non-ASCII characters. The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data.

Take a look here on how to use multipart/form-data instead: Post multipart request with Android SDK

Community
  • 1
  • 1
andrew
  • 6,363
  • 1
  • 20
  • 19
  • I've already tried changing the default http header like this: httpPost.setHeader("Content-Type", "multipart/form-data"); After this, the server doesn't respond at all! Also, I'm not sending any form data. My program constructs an array of some basic KVPs and sends it to the PHP server. What content type do I have to use for sending such data? – Vinit Shandilya May 03 '15 at 11:14
  • 1
    It is obvious that server do not respond. Now your request is malformed, it is not enough just to change content type header to make it work as `multipart/form-data`. There must be boundary for such type of request and `HttpPost` do not add them. So either add them as described in w3 with custom request body or take a look to answer I have linked. If you switch to `multipart/form-data` I think everything will work properly. – andrew May 03 '15 at 13:48