Parsing syntax from android

I use GCM for push notification on a mobile phone using this example , and the notification is pressed. But I could not parse the package received from the intent. This is the package I received from the magazine Bundle[{message={"valid":"bbb","deal":"its working","address":"some","name":"aaa"}, android.support.content.wakelockid=1, collapse_key=do_not_collapse, from=198162727742}] , and I converted it to a string and then tried to convert to a json object using this code json = new JSONObject(message); but no luck. can someone tell me what i am doing wrong here. thanks in advance

+7
json android bundle
source share
5 answers

In the method that received the intent, do the following:

 protected void onMessage(Context context, Intent intent) { //log the message in JSON format Log.i(TAG, "Received message >> " + intent.getExtras().toString()); //Retrieve message and extra String message = intent.getExtras().getString("message"); } 
+9
source share

Real JSON object:

 {"valid":"bbb","deal":"its working","address":"some","name":"aaa"} 

The rest is not JSON.

Try to parse this line and it will work =)

So:

 json = new JSONObject( bundle.getString("message") ); 
+2
source share

You must use the built-in Bundle functions to get the data (e.g. getString ). Try the following code:

 String jsonStr = bundle.getString("message"); json = new JSONObject(jsonStr); 
0
source share

You must accept a JSON value against the message key and convert it to a string

-one
source share

try it...

 ...... @Override protected void onHandleIntent(Intent intent) { Bundle extras = intent.getExtras(); GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); String messageType = gcm.getMessageType(intent); if (!extras.isEmpty()) { if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR .equals(messageType)) { sendNotification("Send error: " + extras.toString()); } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED .equals(messageType)) { sendNotification("Deleted messages on server: " + extras.toString()); } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE .equals(messageType)) { Log.i("Notification", "Completed work @ " + SystemClock.elapsedRealtime()); Log.d(CabChainGlobal.APPTAG, "Message received: " + extras.toString()); sendNotification("" + extras.get("message")); Log.i("Notification Received", "Received: " + extras.toString()); } } GcmBroadcastReceiver.completeWakefulIntent(intent); } 

.....

-one
source share

All Articles