title: How can I send more than one String through a POST or JSON? Can I send a JSON array through another JSON object?
body: <p>I am trying to implement push notifications for android. I want to send the same message to more than one phone. Currently it works to send to one phone. I want to get the registration keys from one mysql database and send to all those phones one message.</p>
  
  <p>The problem is I am confused by json and arrays and POST. Can I send a JSON array through another JSON object? How can I put in  <code>params.add(new BasicNameValuePair("regId", regkeys));</code> more than just one registration key? I tried to make regkeys an array but it didn't work. I know it has to be somehow in the form </p>
  
  <p><code>{   "message":"some message here",
      "regIds": [
          "dsdfsfet366767547",
          "63567356366reygh",
          "sgdgtwetwetsdgsdg",
          "sdgsdgsdgwet24t"
      ]
  }</code> </p>
  
  <p>but I don't know how to create it.</p>
  
  <p>Php code</p>
  
  <pre><code>     $regId = $_POST["regId"];
       $message = $_POST["message"];
  ...
  $registatoin_ids = array($regId);
      $message = array("price" =&gt; $message);
       $result = $gcm-&gt;send_notification($registatoin_ids, $message);
  ...
       public function send_notification($registatoin_ids, $message) {...}
  </code></pre>
  
  <p>Android code   </p>
  
  <pre><code> String regkeys = regkey.getText().toString();
   String message = messg.getText().toString();
  </code></pre>
  
  <p>...</p>
  
  <pre><code>List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt; ();
  params.add(new BasicNameValuePair("regId", regkeys));
  params.add(new BasicNameValuePair("message", message));
  
  JSONObject json = parser2.makeHttpRequest(url, params); 
  </code></pre>
  
  <p>...     </p>
  
  <pre><code> public class jParser2 {
  
  static InputStream is = null;
  static JSONObject jObject = null;
  static String json = "";
  
  public jParser2(){
  
  }
  
  public JSONObject makeHttpRequest(String url, List&lt;NameValuePair&gt; params){
  
  try {
      DefaultHttpClient httpClient = new DefaultHttpClient();
      HttpPost httpPost = new HttpPost(url);
  
      httpPost.setEntity(new UrlEncodedFormEntity(params));
  
      HttpResponse httpResponse = httpClient.execute(httpPost);
      HttpEntity httpEntity = httpResponse.getEntity();
      is = httpEntity.getContent();
  
  } catch (UnsupportedEncodingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  } catch (ClientProtocolException e) {
      e.printStackTrace();
  } catch (IOException e) {
      e.printStackTrace();
  }
  try {
      BufferedReader reader = new BufferedReader (new InputStreamReader (is, "iso-8859-1"), 8);
  
      StringBuilder sb = new StringBuilder();
      String line = null;
      while ((line = reader.readLine())!=null){
          sb.append(line+"\n");
      }
      is.close();
      json = sb.toString();
  } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();        
  }   
  try {
          jObject = new JSONObject(json);
  } catch (JSONException e) {
          // TODO Auto-generated catch block
      e.printStackTrace();
  }   
  
      return jObject;
  }
    }
  </code></pre>
  
  <p>My server to GCM code.</p>
  
  <pre><code>public function send_notification($registatoin_ids, $message) {
  // include config
  include_once './config.php';
  
      // Set POST variables
      $url = 'https://android.googleapis.com/gcm/send';
  
      $fields = array(
          'registration_ids' =&gt; $registatoin_ids,
          'data' =&gt; $message,
      );
  
      $headers = array(
          'Authorization: key=' . GOOGLE_API_KEY,
          '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);
  
      // Disabling SSL Certificate support temporarly
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  
      curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
  
      // Execute post
      $result = curl_exec($ch);
      if ($result === FALSE) {
          die('Curl failed: ' . curl_error($ch));
      }
  
      // Close connection
      curl_close($ch);
      echo $result;
  }
  </code></pre>