繁体   English   中英

解析收到的GCM消息

[英]Parse received GCM message

我一直在尝试从收到的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'是封装在String消息中的JSON的一部分。 您应该将String消息转换为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-string并跳过字符串切割位。

暂无
暂无

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

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