android - 通过 Java 连接 GCM 时获取 JSON 异常

标签 android push-notification google-cloud-messaging

我正在尝试通过 JAVA 代码连接到 GCM。

对于 application/x-www-form-urlencoded;charset=UTF-8 它工作正常, 但是当我使用 Content-Type: application/json 我得到 JSON_PARSING_ERROR: Unexpected character (r) at position 0

我的代码是:

     List<NameValuePair> formparams = new ArrayList<NameValuePair>();

                formparams.add(new BasicNameValuePair("registration_id", 
GCM_ID));
                formparams.add(new BasicNameValuePair("data.message", 
"this is data mesg"));
                // UrlEncodedFormEntity entity = new 
UrlEncodedFormEntity(formparams,
                // "UTF-8");


                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(REQUEST_URL);

                // ADDING DATA IN TO HEADER OF URL

                httpPost.setHeader("Authorization",
                        "key=AIzaSyBmy5WfZMuO-BGMaWEtVhI1f-2gqyN-1h0");
                /*httpPost.setHeader("Content-Type",
                        "application/x-www-form-urlencoded;charset=UTF-
8");*/
                httpPost.setHeader("Content-Type",
                        " application/json");                // not   
working


                try {
                    httpPost.setEntity(new 
UrlEncodedFormEntity(formparams, "utf-8"));

                    // RETRIEVING RESPONSE FROM URL
                    System.out.println("1");
                    HttpResponse response= httpclient.execute(httpPost);
                    System.out.println("2");
                    str = 
inputStreamToString(response.getEntity().getContent()).toString();
                    System.out.println("3");
                    System.out.println(str+" "+response);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

解析响应的代码是:

private StringBuilder inputStreamToString(InputStream is) {
    String line = "";
    StringBuilder total = new StringBuilder();
    // Wrap a BufferedReader around the InputStream
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    // Read response until the end
    try {
        while ((line = rd.readLine()) != null) {
            total.append(line);
        }
    } catch (IOException e) {

        Log.e("inputStreamToString", "during convert");
        e.printStackTrace();
    }
    // Return full string
    return total;
        }

最佳答案

当然不行。您正在发送 application/x-www-form-urlencoded 内容,同时将内容类型指定为 application/jsonformparams 不是 JSON 字符串。

JSON 字符串应该如下所示:

{
  "registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."],
  "data" : { 
    "message" : "this is data mesg"
  },
}

尝试使用 StringEntity 而不是 UrlEncodedFormEntity:

假设 jsonString 包含一个 正确格式的字符串:

httpPost.setEntity(new StringEntity(jsonString, ContentType.APPLICATION_JSON));

关于android - 通过 Java 连接 GCM 时获取 JSON 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19420762/

相关文章:

android - ECDH 与 Android Key Store 中的 key

php - 将 MySQL 结果集转换为 JSON 数组以使用 Google GCM API 时出现的问题

android - 从 C2DM 迁移到 GCM

android - GCM 注册设备接收两种类型的通知

android - 如何在android中显示存储中的音频文件的缩略图

Android Studio -> 如何在没有 BUILD 的情况下进行 CLEAN?

Android Matrix multiplyMM 详解

android - Onesignar 从通知 android 获取标签?

ios - 推送通知操作未在 Swift 中显示?

android - 在后台或应用程序关闭时未收到 Firebase 通知