title: Server returned HTTP response code: 400 for URL: https://gcm-http.googleapis.com/gcm/send
body: <p>I searched a lot, but I didn't find a way to solve this problem below.</p>
  
  <blockquote>
    <p>Enviando POST para o GCM
    Response Code: 400
    java.io.IOException: Server returned HTTP response code: 400 for URL: <a href="https://gcm-http.googleapis.com/gcm/send" rel="nofollow">https://gcm-http.googleapis.com/gcm/send</a>
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
        at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1890)
        at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1885)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1884)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1457)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
        at br.com.farmas.util.Post2GCM.post(Post2GCM.java:46)
        at br.com.farmas.main.AppMain.main(AppMain.java:17)
    Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: <a href="https://gcm-http.googleapis.com/gcm/send" rel="nofollow">https://gcm-http.googleapis.com/gcm/send</a>
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
        at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
        at br.com.farmas.util.Post2GCM.post(Post2GCM.java:43)
        ... 1 more</p>
  </blockquote>
  
  <p>could you help me please?</p>
  
  <p>below, I show the code I am executing....</p>
  
  <p>line 43 is the following: int responseCode = connection.getResponseCode();</p>
  
  <pre><code>Enviando POST para o GCM
  Response Code: 400
  java.io.IOException: Server returned HTTP response code: 400 for URL:      https://gcm-http.googleapis.com/gcm/send
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
  at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1890)
  at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1885)
  at java.security.AccessController.doPrivileged(Native Method)
  at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1884)
  at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1457)
  at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
  at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
  at br.com.farmas.util.Post2GCM.post(Post2GCM.java:46)
  at br.com.farmas.main.AppMain.main(AppMain.java:17)
  Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://gcm-http.googleapis.com/gcm/send
  at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840)
  at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
  at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
  at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
  at br.com.farmas.util.Post2GCM.post(Post2GCM.java:43)
  ... 1 more
  </code></pre>
  
  <p>Below is the first code from where I do the call</p>
  
  <pre><code>    public static void main(String[] args) 
  {
      System.out.println("Enviando POST para o GCM");
  
      Post2GCM.post(API_KEY, criarContent());
  }
  
  private static Content criarContent() 
  {
      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  
      Content content = new Content();
      content.addRegId("eCvEu9f2LFM:APA91bEB7AnrTxP1owl_QrFTLUc35vxs2rGOCpyajU5RTsuPY427IhFF8EayEC3WThQ54ag7q6mwbKUywohGvK_q-RTH0QlglJjVqzH9b9X-gx6Nli9_RJlLRnwyV10iervfexFB3fOy");
      content.addData("titulo", "Titulo Teste");
      content.addData("message", "Message Teste");
      content.addData("data", dateFormat.format(new Date()));
      content.addData("id", "2");
  
      return content;
  }
  </code></pre>
  
  <p>Below is the second code where the exception happens. The exception happens at the following line: int responseCode = connection.getResponseCode();</p>
  
  <pre><code>    public static void post(String apiKey, Content content) 
  {
      try 
      {
          URL url = new URL("https://gcm-http.googleapis.com/gcm/send");
  
          HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  
          connection.setRequestMethod("POST");
  
          connection.setRequestProperty("Content-Type", "application/json");
          connection.setRequestProperty("Authorization", "key=" + apiKey);
  
          connection.setDoOutput(true);
  
          ObjectMapper mapper = new ObjectMapper();
  
          DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
  
          mapper.writeValue(outputStream, content);
  
          outputStream.flush();
  
          outputStream.close();
  
          int responseCode = connection.getResponseCode();
          System.out.println("Response Code: " + responseCode);
  
          BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  
          String linha = "";
          StringBuffer resultado = new StringBuffer();
          while ((linha = bufferedReader.readLine()) != null) 
          {
              resultado.append(linha);
          }
          bufferedReader.close();
  
          System.out.println(resultado);
      } 
      catch (MalformedURLException e) 
      {
          e.printStackTrace();
      } 
      catch (IOException e) 
      {
          e.printStackTrace();
      }
  }
  </code></pre>