openimsdk / open-im-sdk-android

IM Android
https://openim.io
MIT License
225 stars 161 forks source link

After clicking to send a message, the server responds, but the response cannot be parsed. I hope you can check to see where the issue lies. #26

Closed buyude closed 2 years ago

buyude commented 2 years ago

package io.openim.example;

import android.os.Bundle; import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

import io.openim.android.sdk.OpenIMClient; import io.openim.android.sdk.listener.InitSDKListener; import io.openim.android.sdk.listener.OnAdvanceMsgListener; import io.openim.android.sdk.listener.OnBase; import io.openim.android.sdk.listener.OnConversationListener; import io.openim.android.sdk.listener.OnFriendshipListener; import io.openim.android.sdk.listener.OnGroupListener; import io.openim.android.sdk.listener.OnMsgSendCallback; import io.openim.android.sdk.manager.MessageManager; import io.openim.android.sdk.models.ConversationInfo; import io.openim.android.sdk.models.GroupInfo; import io.openim.android.sdk.models.GroupMembersInfo; import io.openim.android.sdk.models.HaveReadInfo; import io.openim.android.sdk.models.Message; import io.openim.android.sdk.models.UserInfo; import io.openim.android.sdk.utils.JsonUtil;

public class MainActivity extends AppCompatActivity implements InitSDKListener, OnAdvanceMsgListener, OnConversationListener, OnFriendshipListener, OnGroupListener {

/// api地址
final static String IP_API = "http://192.168.2.129:10800";
/// websocket地址
final static String IP_WS = "ws://192.168.2.129:10800";
Message msg ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String path = getExternalCacheDir().getAbsolutePath();
    OpenIMClient client = OpenIMClient.getInstance();
    client.initSDK(IP_API, IP_WS, path, this);
    msg =  client.messageManager.createTextMessage("hello world");
    msg.setSendID("3569842");
    msg.setPlatformID(2);
    msg.setRecvID("36429543");
    msg.setClientMsgID("sadd");
    msg.setSessionType(1);
    msg.setSenderNickName("asdads");
    System.out.println("=======send=================" + JsonUtil.toString(msg));

    client.messageManager.sendMessage(new OnMsgSendCallback() {
        @Override
        public void onError(long code, String error) {

        }

        @Override
        public void onProgress(long progress) {

        }

        @Override
        public void onSuccess(String s) {

        }
    }, msg, "36429530", "", false);
    client.messageManager.addAdvancedMsgListener(this);
    client.groupManager.setOnGroupListener(this);
    client.conversationManager.setOnConversationListener(this);
    client.friendshipManager.setOnFriendshipListener(this);
    // uid: 13918588195, token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
}

@Override
public void onConnectFailed(long code, String error) {
    System.out.println("=======onConnectFailed=================");
}

@Override
public void onConnectSuccess() {
    System.out.println("===========onConnectSuccess=============");
}

@Override
public void onConnecting() {
    System.out.println("==========onConnecting==============");
}

@Override
public void onKickedOffline() {
    System.out.println("==========onKickedOffline==============");
}

@Override
public void onSelfInfoUpdated(UserInfo info) {
    System.out.println("==========onSelfInfoUpdated==============");
}

@Override
public void onUserTokenExpired() {
    System.out.println("=========onUserTokenExpired===============");
}

@Override
public void onRecvNewMessage(Message msg) {

    System.out.println("=========onRecvNewMessage===============");
}

@Override
public void onRecvC2CReadReceipt(List<HaveReadInfo> list) {
    System.out.println("========onRecvC2CReadReceipt================");
}

@Override
public void onRecvMessageRevoked(String msgId) {
    System.out.println("=======onRecvMessageRevoked=================");
}

@Override
public void onConversationChanged(List<ConversationInfo> list) {
    System.out.println("========onConversationChanged================");
}

@Override
public void onNewConversation(List<ConversationInfo> list) {
    System.out.println("==========onNewConversation==============");
}

@Override
public void onSyncServerFailed() {
    System.out.println("========onSyncServerFailed================");
}

@Override
public void onSyncServerFinish() {
    System.out.println("=========onSyncServerFinish===============");
}

@Override
public void onSyncServerStart() {
    System.out.println("=========onSyncServerStart===============");
}

@Override
public void onTotalUnreadMessageCountChanged(int i) {
    System.out.println("==========onTotalUnreadMessageCountChanged==============");
}

@Override
public void onBlackListAdd(UserInfo u) {
    System.out.println("==========onBlackListAdd==============");
}

@Override
public void onBlackListDeleted(UserInfo u) {
    System.out.println("=======onBlackListDeleted=================");
}

@Override
public void onFriendApplicationListAccept(UserInfo u) {
    System.out.println("=========onFriendApplicationListAccept===============");
}

@Override
public void onFriendApplicationListAdded(UserInfo u) {
    System.out.println("==========onFriendApplicationListAdded==============");
}

@Override
public void onFriendApplicationListDeleted(UserInfo u) {
    System.out.println("======onFriendApplicationListDeleted==================");
}

@Override
public void onFriendApplicationListReject(UserInfo u) {
    System.out.println("=======onFriendApplicationListReject=================");
}

@Override
public void onFriendInfoChanged(UserInfo u) {
    System.out.println("=====onFriendInfoChanged===================");
}

@Override
public void onFriendListAdded(UserInfo u) {
    System.out.println("========onFriendListAdded================");
}

@Override
public void onFriendListDeleted(UserInfo u) {
    System.out.println("========onFriendListDeleted================");
}

@Override
public void onMemberEnter(String groupId, List<GroupMembersInfo> list) {
    System.out.println("========onMemberEnter================");
}

@Override
public void onMemberLeave(String groupId, GroupMembersInfo info) {
    System.out.println("=========onMemberLeave===============");
}

@Override
public void onMemberInvited(String groupId, GroupMembersInfo opUser, List<GroupMembersInfo> list) {
    System.out.println("=========onMemberInvited===============");
}

@Override
public void onMemberKicked(String groupId, GroupMembersInfo opUser, List<GroupMembersInfo> list) {
    System.out.println("=========onMemberKicked===============");
}

@Override
public void onGroupCreated(String groupId) {
    System.out.println("==========onGroupCreated==============");
}

@Override
public void onGroupInfoChanged(String groupId, GroupInfo info) {
    System.out.println("===========onGroupInfoChanged=============");
}

@Override
public void onReceiveJoinApplication(String groupId, GroupMembersInfo info, String opReason) {
    System.out.println("===========onReceiveJoinApplication=============");
}

@Override
public void onApplicationProcessed(String groupId, GroupMembersInfo opUser, int agreeOrReject, String opReason) {
    System.out.println("============onApplicationProcessed============");
}

public void onGetConversation(View view) {
    OpenIMClient.getInstance().conversationManager.getAllConversationList(new OnBase<List<ConversationInfo>>() {
        @Override
        public void onError(long code, String error) {
        }

        @Override
        public void onSuccess(List<ConversationInfo> data) {
            System.out.println("onGetConversation:" + JsonUtil.toString(data));
        }
    });
}

public void onLogin(View view) {
    OpenIMClient.getInstance().login(new OnBase<String>() {
        @Override
        public void onError(long code, String error) {

        }
        @Override
        public void onSuccess(String data) {
            System.out.println("登录成功!");

        }
    }, "36429530", "12");
}

public void onGetMessageHistory(View view) {
    OpenIMClient.getInstance().messageManager.getHistoryMessageList(new OnBase<List<Message>>() {
        @Override
        public void onError(long code, String error) {

        }

        @Override
        public void onSuccess(List<Message> data) {
            System.out.println("消息数:" + data.size());
        }
    }, "0b55c23c7fac2b34", "", null, 20);
}

public void onSend(View view) {
    OpenIMClient.getInstance().messageManager.sendMessage(new OnMsgSendCallback() {
        @Override
        public void onError(long code, String error) {
            System.out.println("发送失败!"+code+error);
        }

        @Override
        public void onProgress(long progress) {

        }

        @Override
        public void onSuccess(String s) {
            System.out.println("发送成功!");
        }
    }, msg, "36429544", "", false);
}

}

服务端看到的 image

hrxiang commented 2 years ago

你看文档了吗????

1,初始化 2,设置监听器(会话监听,消息监听,好友关系监听) 3,登录 4,发送消息

消息体是sdk给你创建好了的,请别更改里面的内容。

buyude commented 2 years ago

文档看了,监听器设置了,登录成功了,但是服务端解析不了呀,消息体,我使用了crate方法,然后又set了几个参数,这样写是有什么问题吗?希望可以看一下代码,然后指出我的问题,please

hrxiang commented 2 years ago

文档看了,监听器设置了,登录成功了,但是服务端解析不了呀,消息体,我使用了crate方法,然后又set了几个参数,这样写是有什么问题吗?希望可以看一下代码,然后指出我的问题,please

初始化之后就发送消息??,你登录了吗,。监听器需要在登录之前设置。

hrxiang commented 2 years ago

如果你目前测试服务器是否正常,可以下载我们官网的demo app,app是可以直接修改服务器地址的