parse incoming message from pubnub

I'm having a hard time with this one...

I have a data coming into a service from a pubnub connection

public void Startpubnub() {
Toast.makeText(this, "PubnubService created...", Toast.LENGTH_LONG).show();
Log.i("PUBNUB", "PubnubService created...");
try {
    pubnub.subscribe(new String[] {channel}, new Callback() {
        public void connectCallback(String channel) {

            notifyUser("CONNECT on channel:" + channel);
        }
        public void disconnectCallback(String channel) {
            notifyUser("DISCONNECT on channel:" + channel);
        }
        *public void reconnectCallback(String channel) {
            notifyUser("RECONNECT on channel:" + channel);
        }
        @**Override
        public void successCallback(String channel, Object message) {
            Log.i("tag","broadcast is sent!");
            //notifyUser(channel + " " + message.toString());
            Gson gson = new Gson();
            String jsonMessage = gson.toJson(message);
            System.out.printf("tag",jsonMessage);
            JSONObject json = null;
            try {
                json = new JSONObject(jsonMessage);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.printf("tag",json);
            String data = null;
            try {
                data = json.getString("data");
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.i("tag",data);**

        }*



        02-06 03:32:58.752: W/System.err(2690): org.json.JSONException: No value for data

02-06 03:32:58.752: W/System.err(2690): at org.json.JSONObject.get(JSONObject.java:354) 02-06 03:32:58.762: W/System.err(2690): at org.json.JSONObject.getString(JSONObject.java:510) 02-06 03:32:58.762: W/System.err(2690): at tv.loqoo.FourthScreen.FourthScreenPushService$2.successCallback(FourthScreenPushService.java:120) 02-06 03:32:58.762: W/System.err(2690): at com.pubnub.api.PubnubCore$7.handleResponse(PubnubCore.java:1483) 02-06 03:32:58.762: W/System.err(2690): at com.pubnub.api.SubscribeWorker.process(SubscribeWorker.java:105) 02-06 03:32:58.762: W/System.err(2690): at com.pubnub.api.Worker.run(RequestManager.java:81) 02-06 03:32:58.762: W/System.err(2690): at java.lang.Thread.run(Thread.java:856) 02-06 03:32:58.762: W/dalvikvm(2690): threadid=24: thread exiting with uncaught exception (group=0x418b6300) 02-06 03:32:58.762: W/System.err(2690): java.lang.NullPointerException: println needs a message 02-06 03:32:58.762: W/System.err(2690): at android.util.Log.println_native(Native Method) 02-06 03:32:58.762: W/System.err(2690): at android.util.Log.i(Log.java:159) 02-06 03:32:58.762: W/System.err(2690): at tv.loqoo.FourthScreen.FourthScreenPushService$2.successCallback(FourthScreenPushService.java:125) 02-06 03:32:58.762: W/System.err(2690): at com.pubnub.api.PubnubCore$7.handleResponse(PubnubCore.java:1483) 02-06 03:32:58.762: W/System.err(2690): at com.pubnub.api.SubscribeWorker.process(SubscribeWorker.java:105) 02-06 03:32:58.762: W/System.err(2690): at com.pubnub.api.Worker.run(RequestManager.java:81) 02-06 03:32:58.762: W/System.err(2690): at java.lang.Thread.run(Thread.java:856)

In the successCallback im trying to parse the incoming message as a jsonobject and send the extras to a broadcast reciever.

I'm having extreme trouble making any sense of how to parse...Object message.

this is the format of the incoming message:

        5 info = pubnub.publish({
        6     'channel' : 'somechannel',
        7     'message' : {
        8         'data' : 'Hello my World',
        9         'data2' : 'yup',
        10         'data3' : 'yupyup'
        11     }
        12 })

Answers


Try some thing like this:

import org.json.JSONObject;
public void callback(JSONObject data) {
    try {
        String dt = data.getString("data");
        String dt = data.getString("data1");
    } catch (Exception e) {}
}

I ended up using this... from a service which sends the json to a broadcast receiever class. it's pretty verbose. but didn't really understand how to implement @bhagat's answer....

        private void broadcastMessage(JSONObject message) { 
    String msg = null;
    try {
    msg = message.getString("action");
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
    Intent intent = new Intent(msg);
    intent.putExtra("message", message.toString());
    sendBroadcast(intent);
}

class MessageHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
        try {
            String m = msg.getData().getString("message");
            Log.i("handleMessage", m);
            JSONObject message = (JSONObject) new JSONTokener(m).nextValue();
            broadcastMessage(message);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

};
    @Override
        public void successCallback(String channel, Object message) {
            Log.i("tag","broadcast is sent!");
            notifyUser(channel + " " + message.toString());
            Log.i("afterBroadcastisSent", message.toString());
            try {
                Message m = Message.obtain();
                Bundle b = new Bundle();
                b.putString("message", message.toString());
                m.setData(b);
                mMessageHandler.sendMessage(m);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

Need Your Help

how to get the separate contact name array list and contact number array list from the device

java android

ArrayList<String> contacts_list = new ArrayList<String>();

Difference in perf and Intel PCM

performance performancecounter perf

I know both Intels PCM and perf are available, do they use the same hardware counters?? However perfs raw h/w events are not clear and Intel`s PCM provides much more functionality. Can I use PCM to