Getting socket.io response in android client, but can't understand how to implement the response in recycler adapter

1.5k views Asked by At

I'm working on a real-time chatting android application where I need to connect socket.io for real-time response. I followed this tutorial:https://socket.io/blog/native-socket-io-and-android/ and I've successfully implemented socket.io in my android app and getting the response in a toast.

Here is the Socket connection class:

    import android.app.Application;
    import java.net.URISyntaxException;
    import io.socket.client.IO;
    import io.socket.client.Socket;

    public class ChatApplication extends Application {

    private Socket mSocket;
      {
        try {
            mSocket = IO.socket(Constants.CHAT_SERVER_URL);
            } catch (URISyntaxException e) {
              throw new RuntimeException(e);
            }
      }

    public Socket getSocket() {
            return mSocket;
            }
    }

Here is the url of server:

    public class Constants {
        public static final String CHAT_SERVER_URL = 
        "https://pubsub.XXX.com:3000";
    }

I've connected the socket.io in this Activity and getting the response perfectly.

In this activity I've attached the recycler adapter to show the one-to-one full conversation with a friend. For viewing the full conversation I've used retrofit.

Here is the activity code link https://pastebin.com/b22ehMFE

In this activity I've connected the socket through the "username" event and call the "onNewMessage" function.

     // initialize Socket
        ChatApplication app = (ChatApplication) getApplication();
        mSocket = app.getSocket();
        mSocket.on(Socket.EVENT_CONNECT, onConnect);
        mSocket.on(username, onNewMessage);
        mSocket.connect();

I'm getting the real time server response in public Emitter.Listener onNewMessage

    public Emitter.Listener onNewMessage = new Emitter.Listener(){

        @Override
        public void call(final Object... args) {
            MsgChatActivity.this.runOnUiThread(new Runnable(){

                @Override
                public void run() {
                    //args[i] is the data received
                    JSONObject abc = (JSONObject) args[0];

                    Toast.makeText(MsgChatActivity.this, ""+ abc,
                            Toast.LENGTH_LONG).show();
                }
            });

        }
    };

Now, I need to show the response in the adapter view but I can't understand how to do it.

Here is the server responses in JSON format which I want to implement,

pnType: chat

{
  "alertId": "xxxxxxxxxxxxxx",
  "originator": {
    "id": "1",
    "username": "jon",
    "url": "\/jon",
    "full_name": "jon smith",
    "avatar": "\/uploads\/images\/1491902152.jpg",
    "cover": "\/uploads\/images\/1491902130.jpg",
    "is_active": "1",
  },
  "queId": "1503725762883",
  "content_id": "4066",
  "msg": "",
  "media": {
    "sticker": "\/defaultMedia\/stickers\/BlueCat\/3.png"
  },
  "pnType": "chat",
  "unRead": "1"
}

and pnType: chat typing

          {
           "pnType": "chatTyping",
           "originator": {
           "id": "1",
           "name": "jon"
           }
          }

Can anyone help me to implement the response in adapter? Thanks in advance.

1

There are 1 answers

0
Saeef Minhaz On BEST ANSWER

Got it. Use Model class as JSONOBject by reflection. Add this code n the Emitter.Lister response

        Gson gson= new Gson();
        socketData=gson.fromJson(abc.toString(),SocketData.class);