Skip to content

Commit 56c51e9

Browse files
committed
fix bug crash when convert message from android native
1 parent 23122e3 commit 56c51e9

2 files changed

Lines changed: 176 additions & 119 deletions

File tree

android/src/main/java/com/stringee/stringeeflutterplugin/Utils.java

Lines changed: 62 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -227,70 +227,88 @@ public static Map convertLastMessageToMap(@NonNull JSONObject msgObj, Type type)
227227
break;
228228
case PHOTO:
229229
JSONObject photoObj = msgObj.optJSONObject("photo");
230-
Map photoMap = new HashMap();
231-
photoMap.put("filePath", photoObj.optString("filePath"));
232-
photoMap.put("fileUrl", photoObj.optString("fileUrl"));
233-
photoMap.put("thumbnail", photoObj.optString("thumbnail"));
234-
photoMap.put("ratio", ((Integer) photoObj.optInt("ratio")).floatValue());
235-
msgMap.put("photo", photoMap);
230+
if (photoObj != null) {
231+
Map photoMap = new HashMap();
232+
photoMap.put("filePath", photoObj.optString("filePath"));
233+
photoMap.put("fileUrl", photoObj.optString("fileUrl"));
234+
photoMap.put("thumbnail", photoObj.optString("thumbnail"));
235+
photoMap.put("ratio", ((Integer) photoObj.optInt("ratio")).floatValue());
236+
msgMap.put("photo", photoMap);
237+
}
236238
break;
237239
case VIDEO:
238240
JSONObject videoObj = msgObj.optJSONObject("video");
239-
Map videoMap = new HashMap();
240-
videoMap.put("filePath", videoObj.optString("filePath"));
241-
videoMap.put("fileUrl", videoObj.optString("fileUrl"));
242-
videoMap.put("thumbnail", videoObj.optString("thumbnail"));
243-
videoMap.put("ratio", ((Integer) videoObj.optInt("ratio")).floatValue());
244-
videoMap.put("duration", (double) videoObj.optInt("duration"));
245-
msgMap.put("video", videoMap);
241+
if (videoObj != null) {
242+
Map videoMap = new HashMap();
243+
videoMap.put("filePath", videoObj.optString("filePath"));
244+
videoMap.put("fileUrl", videoObj.optString("fileUrl"));
245+
videoMap.put("thumbnail", videoObj.optString("thumbnail"));
246+
videoMap.put("ratio", ((Integer) videoObj.optInt("ratio")).floatValue());
247+
videoMap.put("duration", (double) videoObj.optInt("duration"));
248+
msgMap.put("video", videoMap);
249+
}
246250
break;
247251
case AUDIO:
248252
JSONObject audioObj = msgObj.optJSONObject("audio");
249-
Map audioMap = new HashMap();
250-
audioMap.put("filePath", audioObj.optString("filePath"));
251-
audioMap.put("fileUrl", audioObj.optString("fileUrl"));
252-
audioMap.put("duration", (double) audioObj.optInt("duration"));
253-
msgMap.put("audio", audioMap);
253+
if (audioObj != null) {
254+
Map audioMap = new HashMap();
255+
audioMap.put("filePath", audioObj.optString("filePath"));
256+
audioMap.put("fileUrl", audioObj.optString("fileUrl"));
257+
audioMap.put("duration", (double) audioObj.optInt("duration"));
258+
msgMap.put("audio", audioMap);
259+
}
254260
break;
255261
case FILE:
256262
JSONObject fileObj = msgObj.optJSONObject("file");
257-
Map fileMap = new HashMap();
258-
fileMap.put("filePath", fileObj.optString("filePath"));
259-
fileMap.put("fileUrl", fileObj.optString("fileUrl"));
260-
fileMap.put("fileName", fileObj.optString("fileName"));
261-
fileMap.put("fileLength", fileObj.optLong("length"));
262-
msgMap.put("file", fileMap);
263+
if (fileObj != null) {
264+
Map fileMap = new HashMap();
265+
fileMap.put("filePath", fileObj.optString("filePath"));
266+
fileMap.put("fileUrl", fileObj.optString("fileUrl"));
267+
fileMap.put("fileName", fileObj.optString("fileName"));
268+
fileMap.put("fileLength", fileObj.optLong("length"));
269+
msgMap.put("file", fileMap);
270+
}
263271
break;
264272
case CREATE_CONVERSATION:
265273
case RENAME_CONVERSATION:
266274
msgMap.put("groupName", msgObj.optString("groupName"));
267275
msgMap.put("creator", msgObj.optString("creator"));
268-
JSONArray participantsArray = msgObj.getJSONArray("participants");
276+
JSONArray participantsArray = msgObj.optJSONArray("participants");
269277
List participants = new ArrayList();
270-
for (int i = 0; i < participantsArray.length(); i++) {
271-
participants.add(participantsArray.get(i));
278+
if (participantsArray != null) {
279+
if (participantsArray.length() > 0) {
280+
for (int i = 0; i < participantsArray.length(); i++) {
281+
participants.add(participantsArray.get(i));
282+
}
283+
}
272284
}
273285
msgMap.put("participants", participants);
274286
break;
275287
case LOCATION:
276288
JSONObject locationObj = msgObj.optJSONObject("location");
277-
Map locationMap = new HashMap();
278-
locationMap.put("lat", (double) locationObj.optInt("lat"));
279-
locationMap.put("lon", (double) locationObj.optInt("lon"));
280-
msgMap.put("location", locationMap);
289+
if (locationObj != null) {
290+
Map locationMap = new HashMap();
291+
locationMap.put("lat", (double) locationObj.optInt("lat"));
292+
locationMap.put("lon", (double) locationObj.optInt("lon"));
293+
msgMap.put("location", locationMap);
294+
}
281295
break;
282296
case CONTACT:
283297
JSONObject contactObj = msgObj.optJSONObject("contact");
284-
Map contactMap = new HashMap();
285-
contactMap.put("vcard", contactObj.optString("vcard"));
286-
msgMap.put("contact", contactMap);
298+
if (contactObj != null) {
299+
Map contactMap = new HashMap();
300+
contactMap.put("vcard", contactObj.optString("vcard"));
301+
msgMap.put("contact", contactMap);
302+
}
287303
break;
288304
case STICKER:
289305
JSONObject stickerObj = msgObj.optJSONObject("sticker");
290-
Map stickerMap = new HashMap();
291-
stickerMap.put("name", stickerObj.optString("name"));
292-
stickerMap.put("category", stickerObj.optString("category"));
293-
msgMap.put("sticker", stickerMap);
306+
if (stickerObj != null) {
307+
Map stickerMap = new HashMap();
308+
stickerMap.put("name", stickerObj.optString("name"));
309+
stickerMap.put("category", stickerObj.optString("category"));
310+
msgMap.put("sticker", stickerMap);
311+
}
294312
break;
295313
case NOTIFICATION:
296314
msgMap = convertNotifyContentToMap(msgObj);
@@ -359,8 +377,12 @@ public static Map convertMessageToMap(@NonNull Message message) {
359377
contentMap.put("creator", messageObject.getString("creator"));
360378
JSONArray participantsArray = messageObject.getJSONArray("participants");
361379
List participants = new ArrayList();
362-
for (int i = 0; i < participantsArray.length(); i++) {
363-
participants.add(participantsArray.get(i));
380+
if (participantsArray != null) {
381+
if (participantsArray.length() > 0) {
382+
for (int i = 0; i < participantsArray.length(); i++) {
383+
participants.add(participantsArray.get(i));
384+
}
385+
}
364386
}
365387
contentMap.put("participants", participants);
366388
break;

0 commit comments

Comments
 (0)