【问题标题】:Sending request to GCM results in a JSON_PARSIN_ERROR向 GCM 发送请求会导致 JSON_PARSIN_ERROR
【发布时间】:2015-01-29 16:01:50
【问题描述】:

我需要编写一个向 iOS 和 Android 设备发送推送通知的服务器。

我可以成功地向 iOS 发送通知。幸运的是,实现这部分很轻松。

我在任务中最简单的部分遇到了问题:Android。

我配置了 GCM,Android 设备成功注册到服务器。

当我尝试向设备发送推送时,我收到以下错误:

STATUS: 400
JSON_PARSING_ERROR: Unexpected token END OF FILE at position 0.

这里是发送请求的代码。

AndroidPush push = new AndroidPush();
push.getRegids().add(token);
push.getData().setMessage(message);
push.getData().setMsgcnt(String.format("%d", (badge + 1)));

URL u = new URL(androidEndpoint);
HttpsURLConnection conn = null;
OutputStream os = null;
OutputStreamWriter osw = null;
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
try{

    conn = (HttpsURLConnection)u.openConnection();

    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", androidContentType);
    conn.setRequestProperty("Authorization", androidAuthorization);

    conn.connect();

    os = conn.getOutputStream();
    osw = new OutputStreamWriter(os);   

    System.out.println(push.toJSON());
    osw.write(push.toJSON());

    int status = conn.getResponseCode();
    System.err.println("STATUS: "+status);
    if(status == 200){
        is = conn.getInputStream();
    } else {
        is = conn.getErrorStream();
    }
    isr = new InputStreamReader(is);
    br = new BufferedReader(isr); 
    StringBuilder sb = new StringBuilder();
    String read = null;
    do {
        read = br.readLine();
        if(read != null)
            sb.append(read);
    } while (read != null);

    if(status != 200){
        System.err.println(sb.toString());
    } else {
        System.out.println(sb.toString());
    }

} catch(IOException ex) {
    ex.printStackTrace();
    throw ex;
} finally {
    if(isr != null)
        isr.close();
    if(is != null)
        is.close();
    if(osw != null)
        osw.close();
    if(os != null)
        os.close();
    if(conn != null)
        conn.disconnect();
}

地点:

androidEndpoint = "https://android.googleapis.com/gcm/send";
androidContentType = "application/json";
androidAuthorization = "key=<mykey>";

并且 push.toJSON() 方法的输出是以下字符串:

{
    "registration_ids" : ["APA91bEmD8T9NxQj07uhbTAsD1GTWvT7L_no1SXP70YWaQGPX6VO73pdAOa53PN-hyAyy-3erItWxLDb8W1aQ2nh3np0NttJ5g66w2-142d4bXTCsmrF34-J7rWw4IUObutQznaml59XdfweiEGKzv1Otp3quffUEA"],
    "data" : {
        "message":"Push di prova",
        "msgcnt":"13"
     }
}

我看不出有什么不对...我错过了什么?

感谢您的帮助,

【问题讨论】:

  • 你找到答案了吗?

标签: java android push-notification push google-cloud-messaging


【解决方案1】:

使用 php 开发解决方案时,当消息中包含元音变音符号(例如 äöü)时,我总是收到相同的错误。 没有消息通过并到达接收者。

最终解决方案是utf8_encode( 'your text here äöä' ) 您的外发字符串(消息、标题等)。

花费我至少几个小时来检测“错误”。

【讨论】:

    【解决方案2】:

    是不是你少了下面粗体显示的','

    {
        "registration_ids" : ["APA91bEmD8T9NxQj07uhbTAsD1GTWvT7L_no1SXP70YWaQGPX6VO73pdAOa53PN-hyAyy-3erItWxLDb8W1aQ2nh3np0NttJ5g66w2-142d4bXTCsmrF34-J7rWw4IUObutQznaml59XdfweiEGKzv1Otp3quffUEA"],
        "data" : {
            "message":"Push di prova",
            "msgcnt":"13"**,**
         }**,**
    }
    

    参考此链接中给出的消息:http://developer.android.com/google/gcm/adv.html#payload

    【讨论】:

    • 但从技术上讲...您的 JSON 无效,不是吗?我的意思是:据我所知,最后一个字段的值后面不应该有任何逗号..
    猜你喜欢
    • 2017-12-08
    • 2015-03-20
    • 2015-09-06
    • 2021-02-10
    • 1970-01-01
    • 2018-01-02
    • 2020-02-04
    • 2021-06-04
    • 1970-01-01
    相关资源
    最近更新 更多