繁体   English   中英

GCM Java服务器中的错误

[英]error in GCM Java server

大家好,我有一个Java GCM服务器,当我运行它时,出现了这一行错误

Sending POST to GCM

Sending 'POST' request to URL: https://android.googleapis.com/gcm/send
Response Code: 400
java.io.IOException: Server returned HTTP response code: 400 for URL: https://android.googleapis.com/gcm/send
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at POST2GCM.post(POST2GCM.java:45)
    at App.main(App.java:11)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://android.googleapis.com/gcm/send
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
    at POST2GCM.post(POST2GCM.java:41)
    ... 1 more

在出现关于fail_on_empty_beans的错误之前,但我已经找到了解决方案。 伙计们请帮忙。

您的json字符串应采用以下格式

String postData = "{ \"registration_ids\": [ \"" + CLIENT_REG_ID + "\" ],\"delay_while_idle\": true,\"data\": {\"tickerText\":\"My Ticket\",\"contentTitle\":\"My Title\",\"message\": \"Test GCM message from GCMServer-Android\"}}";

如线程中所回答

POST from Java or JS to GCM

HttpURLConnection connection = null;
        try {
            //Create connection
            String postData = "{ \"registration_ids\": [ \"" + CLIENT_REG_ID + "\" ],\"delay_while_idle\": true,\"data\": {\"tickerText\":\"My Ticket\",\"contentTitle\":\"My Title\",\"message\": \"Test GCM message from GCMServer-Android\"}}";
            URL url = new URL("https://gcm-http.googleapis.com/gcm/send");
            connection = (HttpURLConnection)url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Authorization", "key=API_KEY");
            connection.connect();
            OutputStream os = connection.getOutputStream();
            os.write(postData.getBytes("UTF-8"));
            System.out.println(connection.getResponseCode());
            InputStream stream = (InputStream) connection.getInputStream();
            InputStreamReader isReader = new InputStreamReader(stream);
            //put output stream into a string
            BufferedReader br = new BufferedReader(isReader);
            String line;
            while((line = br.readLine()) != null){
                System.out.println(line);
            }
            os.flush();
            os.close();

        } catch(Exception e){
            System.out.println(e);
        }

在这里的createData方法中吗

public class Content implements Serializable{

    private List<String> registration_ids;
    private Map<String,String> data;

    public void addRegId(String regId){
        if(registration_ids == null)
            registration_ids = new LinkedList<String>();
        registration_ids.add(regId);
    }

    public void createData(String title, String message){
        if(data == null)
            data = new HashMap<String,String>();

        data.put("title", title);
        data.put("message", message);

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM