|
@@ -13,56 +13,20 @@ import cn.org.bjca.trust.android.lib.im.im.msg.ResultMessage;
|
|
|
|
|
|
public class RequestManager implements RequestInterface {
|
|
public class RequestManager implements RequestInterface {
|
|
|
|
|
|
-
|
|
|
|
@Override
|
|
@Override
|
|
public <T> void request(String path, Object data, Class<T> clazz, RequestCallback<T> callback) {
|
|
public <T> void request(String path, Object data, Class<T> clazz, RequestCallback<T> callback) {
|
|
-
|
|
|
|
- RequestMessage rm = new RequestMessage(path, GsonImplHelp.get().toJson(data));
|
|
|
|
-
|
|
|
|
- IMHelper.getInstance().request(rm, new RequestImCallback() {
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void success(ResultMessage message) {
|
|
|
|
- Type type = TypeBuilder
|
|
|
|
- .newInstance(HttpResult.class)
|
|
|
|
- .addTypeParam(clazz)
|
|
|
|
- .build();
|
|
|
|
- HttpResult<T> h = GsonImplHelp.get().toObject(message.getBody(), type);
|
|
|
|
- if (h.getCode() == 200) {
|
|
|
|
- callback.success(h);
|
|
|
|
- } else {
|
|
|
|
- callback.failed(h.getCode(), h.getMsg());
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void failed(int code, String error) {
|
|
|
|
- if (null != callback) {
|
|
|
|
- callback.standby(new RequestCallback<T>() {
|
|
|
|
- @Override
|
|
|
|
- public void success(HttpResult<T> result) {
|
|
|
|
- callback.success(result);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void failed(int code, String error) {
|
|
|
|
- callback.failed(code, error);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void standby(RequestCallback<T> imCallback) {
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- });
|
|
|
|
|
|
+ performRequest(path, data, clazz, callback);
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public <T> void requestList(String path, Object data, Class<T> clazz, RequestCallback<List<T>> callback) {
|
|
public <T> void requestList(String path, Object data, Class<T> clazz, RequestCallback<List<T>> callback) {
|
|
|
|
+ performRequest(path, data, TypeBuilder.newInstance(List.class).addTypeParam(clazz).build(), callback);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private <T> void performRequest(String path, Object data, Type type, RequestCallback<T> callback) {
|
|
|
|
+ if (callback == null) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
|
|
RequestMessage rm = new RequestMessage(path, GsonImplHelp.get().toJson(data));
|
|
RequestMessage rm = new RequestMessage(path, GsonImplHelp.get().toJson(data));
|
|
|
|
|
|
@@ -70,23 +34,25 @@ public class RequestManager implements RequestInterface {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void success(ResultMessage message) {
|
|
public void success(ResultMessage message) {
|
|
- Type type = TypeBuilder
|
|
|
|
- .newInstance(HttpResult.class)
|
|
|
|
- .beginSubType(List.class)
|
|
|
|
- .addTypeParam(clazz)
|
|
|
|
- .endSubType()
|
|
|
|
- .build();
|
|
|
|
- HttpResult<List<T>> h = GsonImplHelp.get().toObject(message.getBody(), type);
|
|
|
|
- if (h.getCode() == 200) {
|
|
|
|
- callback.success(h);
|
|
|
|
- } else {
|
|
|
|
- callback.failed(h.getCode(), h.getMsg());
|
|
|
|
|
|
+ try {
|
|
|
|
+ if (message == null || message.getBody() == null) {
|
|
|
|
+ callback.failed(-1, "Empty response");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ HttpResult<T> h = GsonImplHelp.get().toObject(message.getBody(), TypeBuilder.newInstance(HttpResult.class).addTypeParam(type).build());
|
|
|
|
+ if (h == null || h.getCode() != 200) {
|
|
|
|
+ callback.failed(h != null ? h.getCode() : -1, h != null ? h.getMsg() : "Unknown error");
|
|
|
|
+ } else {
|
|
|
|
+ callback.success(h);
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ callback.failed(-1, "Error parsing response: " + e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void failed(int code, String error) {
|
|
public void failed(int code, String error) {
|
|
- if (null != callback) {
|
|
|
|
|
|
+ if (code == 400504) {
|
|
callback.standby(new RequestCallback<List<T>>() {
|
|
callback.standby(new RequestCallback<List<T>>() {
|
|
@Override
|
|
@Override
|
|
public void success(HttpResult<List<T>> result) {
|
|
public void success(HttpResult<List<T>> result) {
|
|
@@ -103,10 +69,11 @@ public class RequestManager implements RequestInterface {
|
|
|
|
|
|
}
|
|
}
|
|
});
|
|
});
|
|
-
|
|
|
|
|
|
+ } else {
|
|
|
|
+ callback.failed(code, error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|