在Android客户端中获取socket.io响应,但不了解如何在回收站适配器中实现响应 [英] Getting socket.io response in android client, but can't understand how to implement the response in recycler adapter

查看:110
本文介绍了在Android客户端中获取socket.io响应,但不了解如何在回收站适配器中实现响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个实时聊天的android应用程序,我需要连接socket.io以获得实时响应.我遵循了本教程: https://socket.io/blog/native- socket-io-and-android/,并且我已经在我的android应用中成功实现了socket.io,并在烤面包中获得了响应.

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.

这是Socket连接类:

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

这是服务器的网址:

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

我已经在本活动中连接了socket.io,并获得了完美的响应.

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.

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

在此活动中,我已通过"username"事件连接了套接字,并调用了"onNewMessage"函数.

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

我正在公共Emitter.Listener onNewMessage中获得实时服务器响应

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.

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

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

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:聊天输入

and pnType: chat typing

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

有人可以帮助我在适配器中实现响应吗?预先感谢.

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

推荐答案

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

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

这篇关于在Android客户端中获取socket.io响应,但不了解如何在回收站适配器中实现响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆