-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRpcResponse.java
More file actions
110 lines (89 loc) · 2.56 KB
/
RpcResponse.java
File metadata and controls
110 lines (89 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package rpc.json.message;
import org.json.JSONException;
import org.json.JSONObject;
import rpc.framework.message.RpcKey;
import rpc.framework.message.RpcMessage;
import rpc.util.RpcLog;
import rpc.util.RpcTools;
public class RpcResponse implements RpcMessage {
private static final String TAG = "RpcResponse";
private int mId = 0;
private Object mResult = null;
public Object getResult() {
return mResult;
}
public Object getError() {
return mError;
}
private Object mError = null;
private boolean mSuccessful = false;
public RpcResponse() {
}
public RpcResponse(int id, Object result, boolean successful) {
mId = id;
if (successful) {
mResult = result;
if (mResult == null) {
mResult = true; // fix some service like as "void method() {}"
}
} else {
mError = result;
}
mSuccessful = successful;
}
public RpcResponse(int id, Object result) {
this(id, result, true);
}
@Override
public String encode() {
JSONObject message = new JSONObject();
try {
message.put(RpcKey.ID, mId);
if (mResult != null) {
message.put(RpcKey.RESULT, mResult);
}
if (mError != null) {
message.put(RpcKey.ERROR, mError);
}
} catch (JSONException e) {
RpcLog.e(TAG, e);
}
return message.toString();
}
@Override
public void decode(String codes) {
try {
JSONObject result = new JSONObject(codes);
this.mId = result.getInt(RpcKey.ID);
if (result.has(RpcKey.RESULT)) {
this.setResult(result.get(RpcKey.RESULT));
mSuccessful = true;
} else {
this.mError = result.get(RpcKey.ERROR);
mSuccessful = false;
}
} catch (JSONException e) {
RpcLog.e(TAG, e);
}
}
public boolean isSuccessful() {
return mSuccessful;
}
public void setResult(Object result) {
mResult = result;
}
@Override
public String toString() {
String str = "RpcRespone: id=" + mId;
if (mError == null) {
str += ", \nresult=" + (mResult==null ? null : mResult.toString());
} else {
str += ", \nerror=" + (mError==null ? null : mError.toString());
}
return str + ",\nSuccessful=" + mSuccessful;
}
@Override
public int getId() {
return mId;
}
}