你的浏览器禁用了JavaScript, 请开启后刷新浏览器获得更好的体验!
搜索问题、话题或人...
输入关键字进行搜索
搜索:
问题
文章
开源项目
教程
FAQ
· · ·
活动
帮助
话题
周刊
登录
注册
环信_Android
想要聊天数据自己服务器加载,不是从本地存储,和从环信服务器获取。
自己服务器已经走环信的接口获取到了聊天的数据,但是现在是获取到数据不清楚环信的聊天数据结构。
没有找到相关结果
已邀请:
与内容相关的链接
提交
4 个回复
geri_yang
您好,您从您自己的服务器加载聊天数据,和环信就没有关系了,根据您服务器的结构解析然后加载就可以了
有你便是晴天
我们是后台走你们的拉取消息的接口。然后服务器返给我们的。但是我不知道怎么给添加给环信的聊天adapter里面。
geri_yang
您好,您可以在客户端创建对应的message对象,然后把数据设置进去,调用importMessage把您服务器的消息导入到本地db里,然后再加载,如果要是不想用SDK本地存储的消息展示,那这边及都需要您那边自己处理了,展示和环信就没有关系了
geri_yang
您好,下面的您参考下吧
/**
* 导入多条消息
*/
private void importMessages() {
String msgJson = "[{\n"
+ " \"from\": \"lz1\",\n"
+ " \"msg_id\": \"350475701050148824\",\n"
+ " \"payload\": {\n"
+ " \"ext\": {},\n"
+ " \"bodies\": [{\n"
+ " \"secret\": \"lRjEWmCoEee9vfnPereH2W_JZYRVXhhnyCYX7ejQ5Y2Va6SN\",\n"
+ " \"size\": {\n"
+ " \"width\": 288,\n"
+ " \"height\": 384\n"
+ " },\n"
+ " \"filename\": \"1499166285637\",\n"
+ " \"url\": \"https://a1.easemob.com/1188170313178478/youhaodongxi/chatfiles/9518c450-60a8-11e7-8229-e3a51fcf21a3\",\n"
+ " \"type\": \"img\"\n"
+ " }]\n"
+ " },\n"
+ " \"direction\": \"\",\n"
+ " \"timestamp\": \"1499166285638\",\n"
+ " \"to\": \"19369592684545\",\n"
+ " \"chat_type\": \"GroupChat\"\n"
+ "},\n"
+ "{\n"
+ " \"from\": \"lz2\",\n"
+ " \"msg_id\": \"350475800664868824\",\n"
+ " \"payload\": {\n"
+ " \"ext\": {},\n"
+ " \"bodies\": [{\n"
+ " \"secret\": \"ouuV2mCoEeeZx7_YUlvSbZggsYQGkVkok97rpEyJDKPm5j3P\",\n"
+ " \"length\": 2,\n"
+ " \"thumb_secret\": \"oqzfKmCoEeebQ9fCtwzh9qz80EMC-5Aa4SIzlVf1yFGHox4o\",\n"
+ " \"size\": {\n"
+ " \"width\": 360,\n"
+ " \"height\": 480\n"
+ " },\n"
+ " \"file_length\": 258008,\n"
+ " \"filename\": \"149916630777674.mp4\",\n"
+ " \"thumb\": \"https://a1.easemob.com/1188170313178478/youhaodongxi/chatfiles/a2acdf20-60a8-11e7-b726-ab0d6e300107\",\n"
+ " \"type\": \"video\",\n"
+ " \"url\": \"https://a1.easemob.com/1188170313178478/youhaodongxi/chatfiles/a2eb95d0-60a8-11e7-b6a5-073d17c5638d\"\n"
+ " }]\n"
+ " },\n"
+ " \"timestamp\": \"1499166307402\",\n"
+ " \"to\": \"19369592684545\",\n"
+ " \"chat_type\": \"GroupChat\"\n"
+ "},\n"
+ "{\n"
+ " \"chat_type\": \"GroupChat\",\n"
+ " \"direction\": \"SEND\",\n"
+ " \"from\": \"lz3\",\n"
+ " \"msg_id\": \"350476658324539380\",\n"
+ " \"payload\": {\n"
+ " \"bodies\": [{\n"
+ " \"file_length\": \"1.94KB\",\n"
+ " \"filename\": \"yhdx-4620170704T190822.amr\",\n"
+ " \"length\": \"2\",\n"
+ " \"secret\": \"GfcMmmCpEeepRAuL5W3NLlab-3kBku0jcUNleNjnuNxbSoB0\",\n"
+ " \"type\": \"audio\",\n"
+ " \"url\": \"https://a1.easemob.com/1188170313178478/youhaodongxi/chatfiles/19f70c90-60a9-11e7-9927-df857dc80880\"\n"
+ " }]\n"
+ " },\n"
+ " \"timestamp\": \"1499166505987\",\n"
+ " \"to\": \"19369592684545\"\n"
+ "},\n"
+ "{\n"
+ " \"chat_type\": \"GroupChat\",\n"
+ " \"direction\": \"SEND\",\n"
+ " \"from\": \"lz4\",\n"
+ " \"msg_id\": \"350476682890577908\",\n"
+ " \"payload\": {\n"
+ " \"bodies\": [{\n"
+ " \"filename\": \"image-1849353007.jpg\",\n"
+ " \"secret\": \"HVLhymCpEeeBP8fLp1zGL-bWFjrve4Hclkni9XVDriCrrB2w\",\n"
+ " \"size\": {\n"
+ " \"height\": 1263,\n"
+ " \"width\": 840\n"
+ " },\n"
+ " \"type\": \"img\",\n"
+ " \"url\": \"https://a1.easemob.com/1188170313178478/youhaodongxi/chatfiles/1d52e1c0-60a9-11e7-94a9-071c28363fe3\"\n"
+ " }]\n"
+ " },\n"
+ " \"timestamp\": \"1499166511701\",\n"
+ " \"to\": \"19369592684545\"\n"
+ "},\n"
+ "{\n"
+ " \"chat_type\": \"GroupChat\",\n"
+ " \"direction\": \"SEND\",\n"
+ " \"from\": \"lz5\",\n"
+ " \"msg_id\": \"350461429712685056\",\n"
+ " \"payload\": {\n"
+ " \"bodies\": [{\n"
+ " \"filename\": \"image269235292.jpg\",\n"
+ " \"secret\": \"2GwAimCgEeepbS3Xzirr2IlxF_VoO4i_dByB0XMIIDx-UNqG\",\n"
+ " \"size\": {\n"
+ " \"height\": 360,\n"
+ " \"width\": 640\n"
+ " },\n"
+ " \"type\": \"img\",\n"
+ " \"url\": \"https://a1.easemob.com/1188170313178478/youhaodongxi/chatfiles/d86c0080-60a0-11e7-ade1-6dfb6ad635c4\"\n"
+ " }]\n"
+ " },\n"
+ " \"timestamp\": \"1499162960283\",\n"
+ " \"to\": \"19369592684545\"\n"
+ "},\n"
+ "{\n"
+ " \"from\": \"lz6\",\n"
+ " \"msg_id\": \"350445529139775524\",\n"
+ " \"payload\": {\n"
+ " \"ext\": {},\n"
+ " \"bodies\": [{\n"
+ " \"msg\": \"忒MSN\",\n"
+ " \"type\": \"txt\"\n"
+ " }]\n"
+ " },\n"
+ " \"timestamp\": \"1499159258091\",\n"
+ " \"to\": \"19369592684545\",\n"
+ " \"chat_type\": \"GroupChat\"\n"
+ "},\n"
+ "{\n"
+ " \"from\": \"lz7\",\n"
+ " \"msg_id\": \"350445455370360868\",\n"
+ " \"payload\": {\n"
+ " \"ext\": {},\n"
+ " \"bodies\": [{\n"
+ " \"msg\": \"测试\",\n"
+ " \"type\": \"txt\"\n"
+ " }]\n"
+ " },\n"
+ " \"timestamp\": \"1499159240911\",\n"
+ " \"to\": \"19369592684545\",\n"
+ " \"chat_type\": \"GroupChat\"\n"
+ "}]";
List<EMMessage> messageList = new ArrayList<EMMessage>();
try {
JSONArray jsonArray = new JSONArray(msgJson);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
long timestamp = jsonObject.optLong("timestamp");
String from = jsonObject.optString("from");
String to = jsonObject.optString("to");
String msgId = jsonObject.optString("msg_id");
JSONObject bodyObject = jsonObject.optJSONObject("payload").optJSONArray("bodies").getJSONObject(0);
String type = bodyObject.optString("type");
EMMessage message = null;
if (type.equals("txt")) {
message = EMMessage.createReceiveMessage(EMMessage.Type.TXT);
EMTextMessageBody body = new EMTextMessageBody(bodyObject.optString("msg"));
message.addBody(body);
} else if (type.equals("video")) {
message = EMMessage.createReceiveMessage(EMMessage.Type.VIDEO);
EMVideoMessageBody body = new EMVideoMessageBody();
body.setThumbnailUrl(bodyObject.optString("thumb"));
body.setThumbnailSecret(bodyObject.optString("thumb_secret"));
body.setRemoteUrl(bodyObject.optString("url"));
body.setVideoFileLength(bodyObject.optLong("file_length"));
body.setSecret(bodyObject.optString("secret"));
message.addBody(body);
} else if (type.equals("audio")) {
message = EMMessage.createReceiveMessage(EMMessage.Type.VOICE);
File file = new File("");
EMVoiceMessageBody body = new EMVoiceMessageBody(file, bodyObject.optInt("length"));
body.setRemoteUrl(bodyObject.optString("url"));
body.setSecret(bodyObject.optString("secret"));
body.setFileName(bodyObject.optString("filename"));
message.addBody(body);
} else if (type.equals("img")) {
message = EMMessage.createReceiveMessage(EMMessage.Type.IMAGE);
File file = new File("");
// 这里使用反射获取 ImageBody,为了设置 size
Class<?> bodyClass = Class.forName("com.hyphenate.chat.EMImageMessageBody");
Class<?>[] parTypes = new Class<?>[1];
parTypes[0] = File.class;
Constructor<?> constructor = bodyClass.getDeclaredConstructor(parTypes);
Object[] pars = new Object[1];
pars[0] = file;
EMImageMessageBody body = (EMImageMessageBody) constructor.newInstance(pars);
Method setSize = Class.forName("com.hyphenate.chat.EMImageMessageBody")
.getDeclaredMethod("setSize", int.class, int.class);
setSize.setAccessible(true);
int width = bodyObject.optJSONObject("size").optInt("width");
int height = bodyObject.optJSONObject("size").optInt("height");
setSize.invoke(body, width, height);
body.setFileName(bodyObject.optString("filename"));
body.setSecret(bodyObject.optString("secret"));
body.setRemoteUrl(bodyObject.optString("url"));
body.setThumbnailUrl(bodyObject.optString("thumb"));
message.addBody(body);
}
message.setFrom(from);
message.setTo(to);
message.setMsgTime(timestamp);
message.setMsgId(msgId);
message.setChatType(EMMessage.ChatType.GroupChat);
message.setStatus(EMMessage.Status.SUCCESS);
messageList.add(message);
}
VMLog.d("conversation 1- count: %d", EMClient.getInstance().chatManager().getAllConversations().size());
EMClient.getInstance().chatManager().importMessages(messageList);
VMLog.d("conversation 2- count: %d", EMClient.getInstance().chatManager().getAllConversations().size());
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
要回复问题请先
登录
或
注册
发起人
有你便是晴天
问题状态
最新活动:
2017-07-28 18:17
浏览:
3088
关注:
2
人
4 个回复
geri_yang
有你便是晴天
geri_yang
geri_yang
/**
* 导入多条消息
*/
private void importMessages() {
String msgJson = "[{\n"
+ " \"from\": \"lz1\",\n"
+ " \"msg_id\": \"350475701050148824\",\n"
+ " \"payload\": {\n"
+ " \"ext\": {},\n"
+ " \"bodies\": [{\n"
+ " \"secret\": \"lRjEWmCoEee9vfnPereH2W_JZYRVXhhnyCYX7ejQ5Y2Va6SN\",\n"
+ " \"size\": {\n"
+ " \"width\": 288,\n"
+ " \"height\": 384\n"
+ " },\n"
+ " \"filename\": \"1499166285637\",\n"
+ " \"url\": \"https://a1.easemob.com/1188170313178478/youhaodongxi/chatfiles/9518c450-60a8-11e7-8229-e3a51fcf21a3\",\n"
+ " \"type\": \"img\"\n"
+ " }]\n"
+ " },\n"
+ " \"direction\": \"\",\n"
+ " \"timestamp\": \"1499166285638\",\n"
+ " \"to\": \"19369592684545\",\n"
+ " \"chat_type\": \"GroupChat\"\n"
+ "},\n"
+ "{\n"
+ " \"from\": \"lz2\",\n"
+ " \"msg_id\": \"350475800664868824\",\n"
+ " \"payload\": {\n"
+ " \"ext\": {},\n"
+ " \"bodies\": [{\n"
+ " \"secret\": \"ouuV2mCoEeeZx7_YUlvSbZggsYQGkVkok97rpEyJDKPm5j3P\",\n"
+ " \"length\": 2,\n"
+ " \"thumb_secret\": \"oqzfKmCoEeebQ9fCtwzh9qz80EMC-5Aa4SIzlVf1yFGHox4o\",\n"
+ " \"size\": {\n"
+ " \"width\": 360,\n"
+ " \"height\": 480\n"
+ " },\n"
+ " \"file_length\": 258008,\n"
+ " \"filename\": \"149916630777674.mp4\",\n"
+ " \"thumb\": \"https://a1.easemob.com/1188170313178478/youhaodongxi/chatfiles/a2acdf20-60a8-11e7-b726-ab0d6e300107\",\n"
+ " \"type\": \"video\",\n"
+ " \"url\": \"https://a1.easemob.com/1188170313178478/youhaodongxi/chatfiles/a2eb95d0-60a8-11e7-b6a5-073d17c5638d\"\n"
+ " }]\n"
+ " },\n"
+ " \"timestamp\": \"1499166307402\",\n"
+ " \"to\": \"19369592684545\",\n"
+ " \"chat_type\": \"GroupChat\"\n"
+ "},\n"
+ "{\n"
+ " \"chat_type\": \"GroupChat\",\n"
+ " \"direction\": \"SEND\",\n"
+ " \"from\": \"lz3\",\n"
+ " \"msg_id\": \"350476658324539380\",\n"
+ " \"payload\": {\n"
+ " \"bodies\": [{\n"
+ " \"file_length\": \"1.94KB\",\n"
+ " \"filename\": \"yhdx-4620170704T190822.amr\",\n"
+ " \"length\": \"2\",\n"
+ " \"secret\": \"GfcMmmCpEeepRAuL5W3NLlab-3kBku0jcUNleNjnuNxbSoB0\",\n"
+ " \"type\": \"audio\",\n"
+ " \"url\": \"https://a1.easemob.com/1188170313178478/youhaodongxi/chatfiles/19f70c90-60a9-11e7-9927-df857dc80880\"\n"
+ " }]\n"
+ " },\n"
+ " \"timestamp\": \"1499166505987\",\n"
+ " \"to\": \"19369592684545\"\n"
+ "},\n"
+ "{\n"
+ " \"chat_type\": \"GroupChat\",\n"
+ " \"direction\": \"SEND\",\n"
+ " \"from\": \"lz4\",\n"
+ " \"msg_id\": \"350476682890577908\",\n"
+ " \"payload\": {\n"
+ " \"bodies\": [{\n"
+ " \"filename\": \"image-1849353007.jpg\",\n"
+ " \"secret\": \"HVLhymCpEeeBP8fLp1zGL-bWFjrve4Hclkni9XVDriCrrB2w\",\n"
+ " \"size\": {\n"
+ " \"height\": 1263,\n"
+ " \"width\": 840\n"
+ " },\n"
+ " \"type\": \"img\",\n"
+ " \"url\": \"https://a1.easemob.com/1188170313178478/youhaodongxi/chatfiles/1d52e1c0-60a9-11e7-94a9-071c28363fe3\"\n"
+ " }]\n"
+ " },\n"
+ " \"timestamp\": \"1499166511701\",\n"
+ " \"to\": \"19369592684545\"\n"
+ "},\n"
+ "{\n"
+ " \"chat_type\": \"GroupChat\",\n"
+ " \"direction\": \"SEND\",\n"
+ " \"from\": \"lz5\",\n"
+ " \"msg_id\": \"350461429712685056\",\n"
+ " \"payload\": {\n"
+ " \"bodies\": [{\n"
+ " \"filename\": \"image269235292.jpg\",\n"
+ " \"secret\": \"2GwAimCgEeepbS3Xzirr2IlxF_VoO4i_dByB0XMIIDx-UNqG\",\n"
+ " \"size\": {\n"
+ " \"height\": 360,\n"
+ " \"width\": 640\n"
+ " },\n"
+ " \"type\": \"img\",\n"
+ " \"url\": \"https://a1.easemob.com/1188170313178478/youhaodongxi/chatfiles/d86c0080-60a0-11e7-ade1-6dfb6ad635c4\"\n"
+ " }]\n"
+ " },\n"
+ " \"timestamp\": \"1499162960283\",\n"
+ " \"to\": \"19369592684545\"\n"
+ "},\n"
+ "{\n"
+ " \"from\": \"lz6\",\n"
+ " \"msg_id\": \"350445529139775524\",\n"
+ " \"payload\": {\n"
+ " \"ext\": {},\n"
+ " \"bodies\": [{\n"
+ " \"msg\": \"忒MSN\",\n"
+ " \"type\": \"txt\"\n"
+ " }]\n"
+ " },\n"
+ " \"timestamp\": \"1499159258091\",\n"
+ " \"to\": \"19369592684545\",\n"
+ " \"chat_type\": \"GroupChat\"\n"
+ "},\n"
+ "{\n"
+ " \"from\": \"lz7\",\n"
+ " \"msg_id\": \"350445455370360868\",\n"
+ " \"payload\": {\n"
+ " \"ext\": {},\n"
+ " \"bodies\": [{\n"
+ " \"msg\": \"测试\",\n"
+ " \"type\": \"txt\"\n"
+ " }]\n"
+ " },\n"
+ " \"timestamp\": \"1499159240911\",\n"
+ " \"to\": \"19369592684545\",\n"
+ " \"chat_type\": \"GroupChat\"\n"
+ "}]";
List<EMMessage> messageList = new ArrayList<EMMessage>();
try {
JSONArray jsonArray = new JSONArray(msgJson);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
long timestamp = jsonObject.optLong("timestamp");
String from = jsonObject.optString("from");
String to = jsonObject.optString("to");
String msgId = jsonObject.optString("msg_id");
JSONObject bodyObject = jsonObject.optJSONObject("payload").optJSONArray("bodies").getJSONObject(0);
String type = bodyObject.optString("type");
EMMessage message = null;
if (type.equals("txt")) {
message = EMMessage.createReceiveMessage(EMMessage.Type.TXT);
EMTextMessageBody body = new EMTextMessageBody(bodyObject.optString("msg"));
message.addBody(body);
} else if (type.equals("video")) {
message = EMMessage.createReceiveMessage(EMMessage.Type.VIDEO);
EMVideoMessageBody body = new EMVideoMessageBody();
body.setThumbnailUrl(bodyObject.optString("thumb"));
body.setThumbnailSecret(bodyObject.optString("thumb_secret"));
body.setRemoteUrl(bodyObject.optString("url"));
body.setVideoFileLength(bodyObject.optLong("file_length"));
body.setSecret(bodyObject.optString("secret"));
message.addBody(body);
} else if (type.equals("audio")) {
message = EMMessage.createReceiveMessage(EMMessage.Type.VOICE);
File file = new File("");
EMVoiceMessageBody body = new EMVoiceMessageBody(file, bodyObject.optInt("length"));
body.setRemoteUrl(bodyObject.optString("url"));
body.setSecret(bodyObject.optString("secret"));
body.setFileName(bodyObject.optString("filename"));
message.addBody(body);
} else if (type.equals("img")) {
message = EMMessage.createReceiveMessage(EMMessage.Type.IMAGE);
File file = new File("");
// 这里使用反射获取 ImageBody,为了设置 size
Class<?> bodyClass = Class.forName("com.hyphenate.chat.EMImageMessageBody");
Class<?>[] parTypes = new Class<?>[1];
parTypes[0] = File.class;
Constructor<?> constructor = bodyClass.getDeclaredConstructor(parTypes);
Object[] pars = new Object[1];
pars[0] = file;
EMImageMessageBody body = (EMImageMessageBody) constructor.newInstance(pars);
Method setSize = Class.forName("com.hyphenate.chat.EMImageMessageBody")
.getDeclaredMethod("setSize", int.class, int.class);
setSize.setAccessible(true);
int width = bodyObject.optJSONObject("size").optInt("width");
int height = bodyObject.optJSONObject("size").optInt("height");
setSize.invoke(body, width, height);
body.setFileName(bodyObject.optString("filename"));
body.setSecret(bodyObject.optString("secret"));
body.setRemoteUrl(bodyObject.optString("url"));
body.setThumbnailUrl(bodyObject.optString("thumb"));
message.addBody(body);
}
message.setFrom(from);
message.setTo(to);
message.setMsgTime(timestamp);
message.setMsgId(msgId);
message.setChatType(EMMessage.ChatType.GroupChat);
message.setStatus(EMMessage.Status.SUCCESS);
messageList.add(message);
}
VMLog.d("conversation 1- count: %d", EMClient.getInstance().chatManager().getAllConversations().size());
EMClient.getInstance().chatManager().importMessages(messageList);
VMLog.d("conversation 2- count: %d", EMClient.getInstance().chatManager().getAllConversations().size());
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}