1

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();
    }
}
Marcel
  • 421
  • 2
  • 8
  • 18
  • Can you share your code where you're making this call ? – s7vr Jun 10 '16 at 22:35
  • 400 means a malformed request. which means something your sending isnt right. – Tomer Shemesh Jun 10 '16 at 22:36
  • can you try changing the content type to "application/json;charset=UTF-8" ? – s7vr Jun 10 '16 at 22:53
  • @ShivV I changed that line to connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); but the problem still happens. – Marcel Jun 10 '16 at 23:00
  • Here are couple of posts which I think will help you. http://stackoverflow.com/questions/11912990/objectmapper-not-handling-utf-8-properly and http://stackoverflow.com/questions/30352133/server-side-gcm-returns-error-400?rq=1 – s7vr Jun 10 '16 at 23:19
  • @ShivV I checked your suggestions but I didn't figure out the problem. – Marcel Jun 11 '16 at 03:17
  • Can you add the suggestions that you tried as an update to the post ? – s7vr Jun 11 '16 at 03:24
  • @ShivV One of the things I tried was the whole post method of your first suggestion. – Marcel Jun 11 '16 at 03:34
  • Your second suggestion is an older version of gcm and it doesn't sound good enough to be literally used because of it. But it post method sounds literally with mine. – Marcel Jun 11 '16 at 03:35
  • @ShivV I didn't use a PROJECT_NUMBER as it mentions. I generated the google-services.json file. In Google Cloud Platform I have the server key and the android key. The android key is in google-services.json. The server key I use in API_KEY in the method Post2GCM.post(API_KEY, criarContent()); – Marcel Jun 11 '16 at 03:39
  • @ShivV I got the solution. Thank you for your patience and I'm sorry by my errors. I love these forum and I love your interest to help people. – Marcel Jun 11 '16 at 03:59

0 Answers0