NIMSDK-AOS  9.16.0
NetCallAttachment.java
浏览该文件的文档.
1 package com.netease.nimlib.sdk.msg.attachment;
2 
3 import org.json.JSONArray;
4 import org.json.JSONException;
5 import org.json.JSONObject;
6 
7 import java.io.Serializable;
8 import java.util.LinkedList;
9 import java.util.List;
10 
11 public class NetCallAttachment implements MsgAttachment {
12 
13  private int type;
14 
15  private long channelId;
16 
17  private int status;
18 
19  private List<Duration> durations;
20 
21  public int getType() {
22  return type;
23  }
24 
25  public long getChannelId() {
26  return channelId;
27  }
28 
29  public int getStatus() {
30  return status;
31  }
32 
33  public List<Duration> getDurations() {
34  return durations;
35  }
36 
37  @Override
38  public String toJson(boolean send) {
39  JSONObject jsonObject = new JSONObject();
40  try {
41  jsonObject.put("type", type);
42  jsonObject.put("channelId", channelId);
43  jsonObject.put("status", status);
44 
45  JSONArray jsonArray = new JSONArray();
46  for (Duration duration : durations) {
47  jsonArray.put(duration.toJson());
48  }
49 
50  jsonObject.put("durations", jsonArray);
51  } catch (JSONException e) {
52  e.printStackTrace();
53  }
54 
55  return jsonObject.toString();
56  }
57 
58  public static NetCallAttachment fromJson(String jsonString) {
59  NetCallAttachment netCallAttachment = new NetCallAttachment();
60 
61  try {
62  JSONObject jsonObject = new JSONObject(jsonString);
63  int type = jsonObject.optInt("type");
64  long channelId = jsonObject.optLong("channelId");
65  int status = jsonObject.optInt("status");
66 
67  List<Duration> durations = new LinkedList<>();
68  JSONArray jsonArray = jsonObject.optJSONArray("durations");
69  if (jsonArray != null) {
70  for (int i = 0; i < jsonArray.length(); i++) {
71  JSONObject durationJson = jsonArray.optJSONObject(i);
72  if (durationJson != null) {
73  durations.add(Duration.fromJson(durationJson));
74  }
75  }
76  }
77 
78  netCallAttachment.type = type;
79  netCallAttachment.channelId = channelId;
80  netCallAttachment.status = status;
81  netCallAttachment.durations = durations;
82  } catch (JSONException e) {
83  e.printStackTrace();
84  }
85 
86  return netCallAttachment;
87  }
88 
89 
90  public static final class NetCallAttachmentBuilder {
91  private int type;
92  private long channelId;
93  private int status;
94  private List<Duration> durations = new LinkedList<>();
95 
96  public NetCallAttachmentBuilder() {
97  }
98 
99  public NetCallAttachmentBuilder withType(int type) {
100  this.type = type;
101  return this;
102  }
103 
104  public NetCallAttachmentBuilder withChannelId(long channelId) {
105  this.channelId = channelId;
106  return this;
107  }
108 
109  public NetCallAttachmentBuilder withStatus(int status) {
110  this.status = status;
111  return this;
112  }
113 
114  public NetCallAttachmentBuilder withDurations(List<Duration> durations) {
115  this.durations = durations;
116  return this;
117  }
118 
119  public NetCallAttachment build() {
120  NetCallAttachment netCallAttachment = new NetCallAttachment();
121  netCallAttachment.durations = this.durations;
122  netCallAttachment.type = this.type;
123  netCallAttachment.channelId = this.channelId;
124  netCallAttachment.status = this.status;
125  return netCallAttachment;
126  }
127  }
128 
129  public static class Duration implements Serializable {
130  private String accid;
131  private int duration;
132 
133  public String getAccid() {
134  return accid;
135  }
136 
137  public int getDuration() {
138  return duration;
139  }
140 
141  public JSONObject toJson() {
142  JSONObject durationJson = new JSONObject();
143  try {
144  durationJson.put("accid", accid);
145  durationJson.put("duration", duration);
146  } catch (JSONException e) {
147  e.printStackTrace();
148  }
149  return durationJson;
150  }
151 
152  public static Duration fromJson(JSONObject durationJson) {
153  Duration duration = new Duration();
154  duration.accid = durationJson.optString("accid");
155  duration.duration = durationJson.optInt("duration");
156  return duration;
157  }
158  }
159 }
String toJson(boolean send)
将消息附件序列化为字符串,存储到消息数据库或发送到服务器。
static NetCallAttachment fromJson(String jsonString)