簡體   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