套接字io节点js找不到值

时间:2018-08-22 04:05:19

标签: android node.js socket.io

当我将套接字io和noee js连接到android并进行聊天时 一切都很完美,但是android中的recylceview没有显示消息为什么它s not showing? i think my code is don没有任何问题..因为我只是遵循一些示例

这是我的node.js代码

socket.on('messagedetection' , (senderName, messagec) => {
//log the message in console
console.log(senderName+"  :    "+messagec)
//create message object
let message= {"message" : messagec," senderName ": senderName
}
//send message to the cline t sirte
socket.emit('message', message);
  });
});
socket.on('disconnect', function(){
    console.log('user has left')
    socket.broadcast.emit("userdisconnect",' user has left')
 });
 });  

这是我的android代码套接字

    socket.on("message", new Emitter.Listener() {
        @Override
        public void call(final Object... args) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    JSONObject data =(JSONObject) args[0];
                    try {
                        String message = data.getString("message");
                        String nickname = data.getString("senderName");
                        Message m = new Message(nickname,message) ;
                        messageListView.add(m);
                        chatBoxAdapter = new 
   ChatBoxAdapter(messageListView);
                        chatBoxAdapter.notifyDataSetChanged();
                        recyclerView.setAdapter(chatBoxAdapter);
                    }catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    });

及其我的适配器代码

 public MyViewHolder(View view){
    super(view);
    nickname=view.findViewById(R.id.nickname);
    message=view.findViewById(R.id.message1);
}
}

//The ear tap is added to the parameter

public  ChatBoxAdapter(List<Message>MessageList){
  this.messageList=MessageList;
}

@NonNull
@Override
public ChatBoxAdapter.MyViewHolder onCreateViewHolder(@NonNull 
ViewGroup parent, int viewType) {
    View itemview =  LayoutInflater. 
  from(parent.getContext()).inflate(R.layout.there_message, 
  parent ,false);
  return new ChatBoxAdapter.MyViewHolder(itemview);
   }

  @Override
   public void onBindViewHolder(final ChatBoxAdapter.MyViewHolder 
   holder ,final int position) {
    Message m =messageList.get(position);
   holder.nickname.setText(m.getNickname());
   holder.message.setText(m.getMessage());
   }

   @Override
  public int getItemCount(){
   return messageList.size();
  }

 }

是我的错误:

  08-22 12:50:18.379 27217-27217/com.jhpark.server W/System.err: 
   org.json.JSONException: No value for senderName
    at org.json.JSONObject.get(JSONObject.java:389)
    at org.json.JSONObject.getString(JSONObject.java:550)
    at 


com.jhpark.server.Chat.ChatBoxActivity$2$1.run(ChatBoxActivity.java:73)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)

为什么senderNanme没有价值? 但是控制台登录cmd是可以的,但是我的android recycleview不起作用,只能发送消息 我该怎么办?

0 个答案:

没有答案