Retrieve Data from pubnub Android

436 Views Asked by At

i never worked with pubnub earlier , so just want to get some guideline from you all , as my requirement is somewhat like , i need to make group chat application where i will be passing data from my application to Server using RestFull Apis and The server will be pushing the data to Pubnub and i will be retrieving data from pubnub to display data inside application.

Flow is something like :-

  1. Passing Data from App to the Server using RESTful API.

  2. Retrieving data from Server to App using PubNub Data Network.

I Goggled about pubnub and got to know how i can integrate that in my application as i will be needing publish_key and subscribe_key to initialize the Pubnub SDK inside my application.

 Pubnub pubnub = new Pubnub("Your Publish Key Here", "Your Subscribe Key Here");

I have also studied about the Subscriber and Publisher , As per my understanding should i have to subscribe the channel which my Server will build at pubnub in order to retrieve the data from Pubnub and have to use the function as stated bellow :-

    pubnub.history("channel_name", 100, new Callback() {
    public void successCallback(String channel, Object message) {
        System.out.println(message);
    }
});

is this just what i need to do inside the code part of my application ,Please provide me some guideline , if anyone work with this type of requirement , that would be very help-full as i do not have much time to spent on R&D.

Thanks

2

There are 2 best solutions below

1
On

1. Passing Data from App to the Server using RESTful API.

If you want to send data from your client app to your server app you can just POST it to your server directly using traditional web application techniques.

Then your server can publish messages to a PubNub channel that your client apps are subscribed to.

2. Retrieving data from Server to App using PubNub Data Network.

You can use pubnub-android-Lolli-chat application that makes use of Many PubNub features.

git repo https://github.com/GleasonK/pubnub-android-lolli-chat

http://kevingleason.me/pubnub-android-lolli-chat/

Retrieve data from PubNub using flowing code:

pubnub.history(this.channel,100,false,new Callback() {
    @Override
    public void successCallback(String channel, final Object message) {
        try {
            JSONArray json = (JSONArray) message;
            Log.d("History", json.toString());
            final JSONArray messages = json.getJSONArray(0);
            final List<ChatMessage> chatMsgs = new ArrayList<ChatMessage>();
            for (int i = 0; i < messages.length(); i++) {
                try {
                    if (!messages.getJSONObject(i).has("data")) continue;
                    JSONObject jsonMsg = messages.getJSONObject(i).getJSONObject("data");
                    String name = jsonMsg.getString(Constants.JSON_USER);
                    String msg = jsonMsg.getString(Constants.JSON_MSG);
                    long time = jsonMsg.getLong(Constants.JSON_TIME);
                    ChatMessage chatMsg = new ChatMessage(name, msg, time);
                    chatMsgs.add(chatMsg);
                } catch (JSONException e) { // Handle errors silently
                    e.printStackTrace();
                }
            }

            MainActivityPubnub.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                  //  Toast.makeText(MainActivityPubnub.this,"RUNNIN",Toast.LENGTH_SHORT).show();
                    mChatAdapter.setMessages(chatMsgs);
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void errorCallback(String channel, PubnubError error) {
        Log.d("History", error.toString());
    }
});
0
On

If you want to send data from your client app to your server app you can just POST it to your server directly using traditional web application techniques.

Then your server can publish messages to a PubNub channel that your client apps are subscribed to.

There is no requirement to use the history API for what you describe. Use history to get messages that were missed at the time they were published. For example, the client app was offline at the time.

And it appears you are working with the PubNub Java SDK v3.x. Please use the latest PubNub 4.x SDK.