Home:ALL Converter>Server returned HTTP response code: 400 for URL: https://gcm-http.googleapis.com/gcm/send

Server returned HTTP response code: 400 for URL: https://gcm-http.googleapis.com/gcm/send

Ask Time:2016-06-11T06:19:07         Author:Marcel

Json Formatter

I searched a lot, but I didn't find a way to solve this problem below.

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

could you help me please?

below, I show the code I am executing....

line 43 is the following: int responseCode = connection.getResponseCode();

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

Below is the first code from where I do the call

    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;
}

Below is the second code where the exception happens. The exception happens at the following line: int responseCode = connection.getResponseCode();

    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();
    }
}

Author:Marcel,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/37757896/server-returned-http-response-code-400-for-url-https-gcm-http-googleapis-com
yy