Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 解析收到的GCM消息_Java_Android_Json_Google Cloud Messaging - Fatal编程技术网

Java 解析收到的GCM消息

Java 解析收到的GCM消息,java,android,json,google-cloud-messaging,Java,Android,Json,Google Cloud Messaging,我一直试图从收到的GCM消息中提取信息。以下是从我的服务器发送的消息: "Message(data: {profile={"id":"214","name":"Dr Who","phone_number":"214"}})" 以及手机上收到的信息: 05-24 21:00:26.083: D/GCMIntentServiceReceived Message:(3929): Received Message: Message(data: {profile={"id":"214","name":

我一直试图从收到的GCM消息中提取信息。以下是从我的服务器发送的消息:

 "Message(data: {profile={"id":"214","name":"Dr Who","phone_number":"214"}})"
以及手机上收到的信息:

05-24 21:00:26.083: D/GCMIntentServiceReceived Message:(3929): Received Message: Message(data: {profile={"id":"214","name":"Dr Who","phone_number":"214"}})
下面是我用来尝试提取键/值对的代码

@Override
    protected void onMessage(Context arg0, Intent intent) {

        String message = intent.getStringExtra("message");
        JsonParser parser = new JsonParser();
        JSONFingerprint fingerprintProfile = null;

        Log.d(TAG + "Received Message: ", "Received Message: " + message.toString());

        //System.out.println("PHONE " + intent.getExtras().getString("id")); This returns null


    }

在本例中,我试图提取“id”字段,但总是得到NULL。有人有什么想法吗?谢谢

“id”是字符串消息中封装的JSON的一部分。您应该将字符串消息转换为JSON对象,然后检索“id”

String[] parts = message.split("=");
try {
JSONObject object = new JSONObject(parts[1].substring(0, parts[1].length() - 2));
String id = object.getString("id");
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

请注意,上面的代码非常不通用。您应该尝试从服务器发送一个干净的json字符串,并跳过字符串剪切位。

谢谢您的回复!问题似乎是由使用PHP脚本发送GCM消息引起的。切换回纯Java,效果很好!