群聊QuickBlox:由于服务器不活动,连接已关闭

时间:2018-07-15 03:53:51

标签: android quickblox connection-timeout quickblox-android

我成功加入了群聊和私人聊天,但是几分钟后,出现此错误。我尝试发送一条消息,但是它宣布为空,并且我无法与任何消息聊天:

LogCat中的错误:

W/AbstractXMPPConnection: Connection closed due to inactivity server
07-14 23:28:20.662 1532-1618/com.example.sang.chattingdemo D/QBASDK: Starting reconnect
07-14 23:28:30.662 1532-1618/com.example.sang.chattingdemo D/QBASDK: Setting socket keepAlive :true
    Setting socket timeout :60000
07-14 23:28:30.702 1532-1618/com.example.sang.chattingdemo D/QBASDK: Setting socket keepAlive :true
    Setting socket timeout :60000
07-14 23:28:31.022 65-65/? D/Genyd: Received Set Clipboard
07-14 23:28:31.022 65-65/? D/Genymotion: Received Set Clipboard
07-14 23:28:36.842 165-321/? W/genymotion_audio: out_write() limiting sleep time 113560 to 39909
07-14 23:28:36.882 165-321/? W/genymotion_audio: out_write() limiting sleep time 89274 to 39909
07-14 23:28:36.922 165-321/? W/genymotion_audio: out_write() limiting sleep time 69229 to 39909
07-14 23:28:36.962 165-321/? W/genymotion_audio: out_write() limiting sleep time 49183 to 39909
07-14 23:28:37.102 1532-1532/com.example.sang.chattingdemo D/QBASDK: Can't parse the 'date_sent' value null: Invalid double: "null"
    Can't parse the 'date_sent' value null: Invalid double: "null"
07-14 23:28:37.102 1532-1532/com.example.sang.chattingdemo E/ErrorChatMessage: null
07-14 23:28:39.394 542-588/system_process D/dalvikvm: GC_FOR_ALLOC freed 1290K, 21% free 6859K/8628K, paused 9ms, total 9ms
07-14 23:28:39.398 542-593/system_process D/MobileDataStateTracker: default: setPolicyDataEnable(enabled=true)
07-14 23:28:44.726 165-321/? W/genymotion_audio: out_write() limiting sleep time 113560 to 39909
07-14 23:28:44.766 165-321/? W/genymotion_audio: out_write() limiting sleep time 79274 to 39909
07-14 23:28:44.806 165-321/? W/genymotion_audio: out_write() limiting sleep time 59229 to 39909
07-14 23:28:44.886 165-321/? W/genymotion_audio: out_write() limiting sleep time 58366 to 39909
07-14 23:28:44.946 1532-1532/com.example.sang.chattingdemo D/QBASDK: Can't parse the 'date_sent' value null: Invalid double: "null"
    Can't parse the 'date_sent' value null: Invalid double: "null"
07-14 23:28:44.946 1532-1532/com.example.sang.chattingdemo E/ErrorChatMessage: null
07-14 23:28:45.322 1532-1532/com.example.sang.chattingdemo D/QBASDK: Can't parse the 'date_sent' value null: Invalid double: "null"
    Can't parse the 'date_sent' value null: Invalid double: "null"
07-14 23:28:45.322 1532-1532/com.example.sang.chattingdemo E/ErrorChatMessage: null
</i>

我的代码: 活动

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_message);
        initView();
        qbChatMessagesArray=new ArrayList<QBChatMessage>();
        retrieveImagesOfUsers();
        initChatDilalog();
        retrieveMessages();

        btnsendMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                QBChatMessage chatMessage = new QBChatMessage();
                chatMessage.setBody(contentMessage.getText().toString());
                chatMessage.setSenderId(QBChatService.getInstance().getUser().getId());
                chatMessage.setSaveToHistory(true);
                try {
                    qbChatDialog.sendMessage(chatMessage);
                } catch (SmackException.NotConnectedException e) {
                    e.printStackTrace();
                }
                adapter.notifyDataSetChanged();
                contentMessage.setText("");
                contentMessage.setFocusable(true);
                scroolSmooth();
            }
        });
    }


    private void retrieveImagesOfUsers() {
        QBUsers.getUsers(null).performAsync(new QBEntityCallback<ArrayList<QBUser>>() {
            @Override
            public void onSuccess(ArrayList<QBUser> qbUsers, Bundle bundle) {
                for (final QBUser user : qbUsers
                        ) {
                    if (user.getFileId()!=null) {
                        if (QBFileHolder.getInstance().getFileUserById(user.getId()) == null) {
                            QBContent.getFile(user.getFileId()).performAsync(new QBEntityCallback<QBFile>() {
                                @Override
                                public void onSuccess(QBFile qbFile, Bundle bundle) {
                                    String URL = qbFile.getPublicUrl();
                                    Picasso.with(getBaseContext()).load(URL).into(new Target() {
                                        @Override
                                        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                                            QBFileHolder.getInstance().putQBFileUser(user.getId(),bitmap);

                                        }

                                        @Override
                                        public void onBitmapFailed(Drawable errorDrawable) {

                                        }

                                        @Override
                                        public void onPrepareLoad(Drawable placeHolderDrawable) {

                                        }
                                    });
                                }

                                @Override
                                public void onError(QBResponseException e) {

                                }
                            });
                        }
                    }
                }

            }

            @Override
            public void onError(QBResponseException e) {

            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    protected void onStop() {
        super.onStop();

    }

    private void retrieveMessages() {

        QBMessageGetBuilder qbMessageGetBuilder = new QBMessageGetBuilder();
        qbMessageGetBuilder.setLimit(500);
        if(qbChatDialog!=null)
        {
            QBRestChatService.getDialogMessages(qbChatDialog,qbMessageGetBuilder).performAsync(new QBEntityCallback<ArrayList<QBChatMessage>>() {
                @Override
                public void onSuccess(ArrayList<QBChatMessage> qbChatMessages, Bundle bundle) {
                    for (QBChatMessage  qbChatMessage: qbChatMessages
                         ) {
                        qbChatMessagesArray.add(qbChatMessage);
                    }
                    LinearLayoutManager layoutManager = new LinearLayoutManager(ChatMessageActivity.this);
                    adapter = new ChatMessageAdapter(ChatMessageActivity.this,qbChatMessagesArray);
                    lvChatting.setLayoutManager(layoutManager);
                    lvChatting.setAdapter(adapter);
                    scroolSmooth();
                }

                @Override
                public void onError(QBResponseException e) {
                    Log.e("Error",e.getMessage());
                }
            });
        }
        else
            Toast.makeText(this, "null roi", Toast.LENGTH_SHORT).show();


    }
    private void scroolSmooth()
    {
        if(adapter.getItemCount()>0)
        lvChatting.smoothScrollToPosition(adapter.getItemCount()-1);
    }
    private void initChatDilalog() {
        qbChatDialog=(QBChatDialog)getIntent().getSerializableExtra(Common.DIALOG_EXTRA);
        Toast.makeText(this, ""+qbChatDialog.getDialogId(), Toast.LENGTH_SHORT).show();
        qbChatDialog.initForChat(QBChatService.getInstance());
        QBIncomingMessagesManager incomingMessagesManager = QBChatService.getInstance().getIncomingMessagesManager();
        incomingMessagesManager.addDialogMessageListener(new QBChatDialogMessageListener() {
            @Override
            public void processMessage(String s, QBChatMessage qbChatMessage, Integer integer) {

            }

            @Override
            public void processError(String s, QBChatException e, QBChatMessage qbChatMessage, Integer integer) {

            }
        });

        if (!qbChatDialog.getType().equals(QBDialogType.PRIVATE))
        {
            DiscussionHistory discussionHistory = new DiscussionHistory();
            discussionHistory.setMaxStanzas(0);
            qbChatDialog.join(discussionHistory, new QBEntityCallback() {
                @Override
                public void onSuccess(Object o, Bundle bundle) {
                    Toast.makeText(ChatMessageActivity.this, "Join Group Succesfully", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onError(QBResponseException e) {
                    Toast.makeText(ChatMessageActivity.this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        }
        qbChatDialog.addMessageListener(this);
    }

    private void initView() {
        lvChatting = (RecyclerView) findViewById(R.id.list_chat_messages);
        btnsendMessage = (ImageButton) findViewById(R.id.sendMessage);
        contentMessage =(EditText)findViewById(R.id.content_message);

    }

    @Override
    public void processMessage(String s, QBChatMessage qbChatMessage, Integer integer) {
        qbChatMessagesArray.add(qbChatMessage);
        adapter.notifyDataSetChanged();
        scroolSmooth();
    }

    @Override
    public void processError(String s, QBChatException e, QBChatMessage qbChatMessage, Integer integer) {
        Log.e("ErrorChatMessage",""+e.getMessage());
    }
}

适配器:

private static final int VIEW_TYPE_MESSAGE_SENT = 1;
    private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;

    private Context mContext;
    private List<QBChatMessage> mMessageList;
    Integer userID;


    public ChatMessageAdapter(Context context, List<QBChatMessage> messageList) {
        mContext = context;
        mMessageList = messageList;
        userID = QBChatService.getInstance().getUser().getId();


    }


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

    // Determines the appropriate ViewType according to the sender of the message.
    @Override
    public int getItemViewType(int position) {


        if (mMessageList.get(position).getSenderId().equals(userID)) {
            // If the current user is the sender of the message
            return VIEW_TYPE_MESSAGE_SENT;
        } else {
            // If some other user sent the message
            return VIEW_TYPE_MESSAGE_RECEIVED;
        }
    }

    // Inflates the appropriate layout according to the ViewType.
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;

        if (viewType == VIEW_TYPE_MESSAGE_SENT) {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_send_messages, parent, false);
            return new SentMessageHolder(view);
        } else if (viewType == VIEW_TYPE_MESSAGE_RECEIVED) {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_receive_messages, parent, false);
            return new ReceivedMessageHolder(view);
        }

        return null;
    }

    // Passes the message object to a ViewHolder so that the contents can be bound to UI.
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {


        switch (holder.getItemViewType()) {
            case VIEW_TYPE_MESSAGE_SENT:
                ((SentMessageHolder) holder).bind(mMessageList.get(position));
                break;
            case VIEW_TYPE_MESSAGE_RECEIVED:
                ((ReceivedMessageHolder) holder).bind(mMessageList.get(position));

        }
    }


    private class SentMessageHolder extends RecyclerView.ViewHolder {
        BubbleTextView bubbleTextView;

        SentMessageHolder(View itemView) {
            super(itemView);

            bubbleTextView = (BubbleTextView) itemView.findViewById(R.id.idmessend);
        }

        void bind(QBChatMessage message) {
            bubbleTextView.setText(message.getBody());
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private class ReceivedMessageHolder extends RecyclerView.ViewHolder {
        BubbleTextView bubbleTextView;
        CircleImageView userImage;

        ReceivedMessageHolder(View itemView) {
            super(itemView);
            bubbleTextView = (BubbleTextView) itemView.findViewById(R.id.idmesreceive);
            userImage = (CircleImageView) itemView.findViewById(R.id.user_image);

        }

        void bind(final QBChatMessage message) {
            bubbleTextView.setText(message.getBody());
            if(QBFileHolder.getInstance().getFileUserById(message.getSenderId())!=null)
                userImage.setImageBitmap(QBFileHolder.getInstance().getFileUserById(message.getSenderId()));

        }

        }

我不知道此错误是由于QuickBLox或我的代码引起的。那么有人可以帮助我解决这个问题吗?

0 个答案:

没有答案