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

冷暖自知 提交于 2020-01-05 02:09:32

问题


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:


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);


来源:https://stackoverflow.com/questions/45892399/getting-socket-io-response-in-android-client-but-cant-understand-how-to-implem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!