title: Passing String array to PHP as POST
body: <p>I am trying to pass a string array to a PHP script as POST data but am unsure of what to do.</p>
  
  <p>Here is my code for executing PHP scripts so far:</p>
  
  <p>Where I am trying to pass the array:</p>
  
  <pre><code>nameValuePairs.add(new BasicNameValuePair("message",message));
  String [] devices = {device1,device2,device3};
  nameValuePairs.add(new BasicNameValuePair("devices", devices));// &lt;-- Can't pass String[] to BasicNameValuePair
  callPHPScript("notify_devices", nameValuePairs);
  </code></pre>
  
  <p>Call PHP script:</p>
  
  <pre><code>public String callPHPScript(String scriptName, List&lt;NameValuePair&gt; parameters) {
      HttpClient client = new DefaultHttpClient();
      HttpPost post = new HttpPost("http://localhost/" + scriptName);
      String line = "";
      StringBuilder stringBuilder = new StringBuilder();
      try {
          post.setEntity(new UrlEncodedFormEntity(parameters));
  
          HttpResponse response = client.execute(post);
          if (response.getStatusLine().getStatusCode() != 200)
          {
              System.out.println("DB: Error executing script !");
          }
          else {
              BufferedReader rd = new BufferedReader(new InputStreamReader(
                  response.getEntity().getContent()));
              line = "";
              while ((line = rd.readLine()) != null) {
                  stringBuilder.append(line);
              }
          }
  
      } catch (IOException e) {
          e.printStackTrace();
      }
      System.out.println("DB: Result: " + stringBuilder.toString());
      return stringBuilder.toString();
  }
  </code></pre>
  
  <p>And the PHP script in question:</p>
  
  <pre><code>&lt;?php
  include('tools.php');
  // Replace with real BROWSER API key from Google APIs
  $apiKey = "123456";
  
  // Replace with real client registration IDs 
  $registrationIDs = array($_POST[devices]); &lt;-- Where I want to pass array to script
  
  // Message to be sent
  $message = $_POST['message'];
  
  // Set POST variables
  $url = 'https://android.googleapis.com/gcm/send';
  
  $fields = array(
                  'registration_ids'  =&gt; $registrationIDs,
                  'data'              =&gt; array( "message" =&gt; $message ),
                  );
  
  $headers = array( 
                      'Authorization: key=' . $apiKey,
                      'Content-Type: application/json'
                  );
  
  // Open connection
  $ch = curl_init();
  
  // Set the url, number of POST vars, POST data
  curl_setopt( $ch, CURLOPT_URL, $url );
  
  curl_setopt( $ch, CURLOPT_POST, true );
  curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  
  curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
  
  // Execute post
  $result = curl_exec($ch);
  
  // Close connection
  curl_close($ch);
  
  print_as_json($result);
  ?&gt;
  </code></pre>
  
  <p>Any ideas? Thanks !</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I am trying the following but still no joy:</p>
  
  <pre><code>public void notifyDevices(Message message) {
  
      List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;();
      List&lt;String&gt; deviceIDsList = new ArrayList&lt;String&gt;();
      String [] deviceIDArray;
  
      //Get devices to notify
      List&lt;JSONDeviceProfile&gt; deviceList = getDevicesToNotify();
  
      for(JSONDeviceProfile device : deviceList) {
          deviceIDsList.add(device.getDeviceId());
      }
  
      //Array of device IDs
      deviceIDArray = deviceIDsList.toArray(new String[deviceIDsList.size()]);
      for(String deviceID : deviceIDArray) {
  
          nameValuePairs.add(new BasicNameValuePair("devices[]", deviceID));
  
      }
  
      //Call script
      callPHPScript("GCM.php", nameValuePairs);
  }
  </code></pre>
  
  <p>This is all the "Error reporting" I have...</p>
  
  <pre><code>        HttpResponse response = client.execute(post);
          if (response.getStatusLine().getStatusCode() != 200)
          {
              System.out.println("DB: Error executing script !");
          }
  </code></pre>