【问题标题】:Getting socket.io response in android client, but can't understand how to implement the response in recycler adapter在android客户端中获取socket.io响应,但无法理解如何在回收器适配器中实现响应
【发布时间】:2017-08-26 06:00:46
【问题描述】:

我正在开发一个实时聊天 android 应用程序,我需要连接 socket.io 以进行实时响应。我按照本教程进行操作:https://socket.io/blog/native-socket-io-and-android/ 并且我已经在我的 android 应用程序中成功实现了 socket.io 并获得了响应。

这里是Socket连接类:

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

这是服务器的url:

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

我已经在这个 Activity 中连接了 socket.io 并且得到了完美的响应。

在此活动中,我附加了回收器适配器以显示与朋友的一对一完整对话。为了查看完整的对话,我使用了改造。

这是活动代码链接 https://pastebin.com/b22ehMFE

在这个活动中,我通过“用户名”事件连接了套接字并调用了“onNewMessage”函数。

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

我在 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();
                }
            });

        }
    };

现在,我需要在适配器视图中显示响应,但我不明白该怎么做。

这是我想要实现的 JSON 格式的服务器响应,

pnType:聊天

{
  "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"
}

和 pnType:聊天输入

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

谁能帮我在适配器中实现响应?提前致谢。

【问题讨论】:

    标签: android android-recyclerview retrofit2 livechat socket.io-1.0


    【解决方案1】:

    知道了。通过反射将模型类用作 JSONOBject。在 Emitter.Lister 响应中添加此代码

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

    【讨论】:

      猜你喜欢
      • 2019-12-18
      • 2021-03-18
      • 2021-02-11
      • 2012-02-21
      • 1970-01-01
      • 1970-01-01
      • 2015-05-11
      • 2023-03-03
      • 1970-01-01
      相关资源
      最近更新 更多