app端经常遇到这样的需求:弹出一个Toast显示服务端的提示信息,比如:“用户名已经被注册”。
方法一:
public abstract class CommonCallback<T> extends Callback {
private Type type;
public CommonCallback() {
Class<? extends CommonCallback> clazz = getClass();
Type genericSuperclass = clazz.getGenericSuperclass();
if (genericSuperclass instanceof Class) {
throw new RuntimeException("没有写泛型");
}
ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
type = parameterizedType.getActualTypeArguments()[0];
}
@Override
public void onError(Call call, Exception e, int id) {
onError(e);
}
@Override
public void onResponse(String response, int id) {
try {
JSONObject resp = new JSONObject(response);
int resultCode &