public class ResponseResult{
private String code;
Private T data;
private String msg;
Private String requestId;
Private String time;
Public String getCode () {
return code;
}
Public void setCode (String code) {
This. Code=code;
}
Public T getData () {
return data;
}
Public void setData (T data) {
This data=https://bbs.csdn.net/topics/data;
}
Public String getMsg () {
Return MSG.
}
Public void setMsg (String MSG) {
This. MSG=MSG;
}
Public String getRequestId () {
Return requestId;
}
Public void setRequestId (String requestId) {
Enclosing requestId=requestId;
}
Public String getTime () {
The return time.
}
Public void setTime (String time) {
This. Time=the time;
}
}
The public & lt; T> OnHttpResultListener jsonHttpWithData (ClassClazz OnUiCallback OnUiCallback OnNextListener OnNextListener) {
Return new OnHttpResultListener () {
@ Override
Public void onSuccess (String s) {
if (! TextUtils. IsEmpty (s)) {
GsonResponsePasarePasare=new GsonResponsePasare () {
};
ResponseResultResponseResult=pasare. Deal (s);
ResponseResult LogUtils i. (TAG, "==" + new Gson (). The toJson (responseResult));
JsonData (responseResult onUiCallback, onNextListener);
} else {
If (onUiCallback!=null) {
OnUiCallback. one rror (EXC_CODE JSON_NULL);
}
}
}
@ Override
Public void one rror (Throwable e) {
OnUiCallback. one rror (EXC_CODE, um participant etMessage ()==null? "" : um participant etMessage ());
}
};
}
The class GsonResponsePasareImplements ParameterizedType {
Public ResponseResultDeal (String response) {
//Type gsonType=new ParameterizedType () {//... };//do not recommend this way, recommend using GsonResponsePasare ParameterizedType. Because the getActualTypeArguments here involves obtaining GsonResponsePasare generic collections
Type gsonType=this;
ResponseResultResult=new Gson (). FromJson (response, gsonType);
//LogUtils. E (" Data is: "+ commonResponse. Data," the Class Type is: "+ commonResponse. Data. The getClass (), toString ());
return result;
}
@ NotNull
@ Override
The public Type [] getActualTypeArguments () {
The Class CLZ=this. GetClass ();
//here must pay attention to use new GsonResponsePasare< outside; GsonResponsePasare. DataInfo> () {}; Instantiation must take {}, or access to the superclass for Object
Type superclass.=CLZ getGenericSuperclass ();//getGenericSuperclass () to obtain a generic parent
If (superclass instanceof Class) {
Throw new RuntimeException (" Missing the type parameter. ");
}
ParameterizedType parameterized=(ParameterizedType) superclass;
Type [] types=the parameterized. The getActualTypeArguments ();
If (Build) VERSION) SDK_INT & gt;=Build. VERSION_CODES. P) {
LogUtils i. (TAG, "getActualTypeArguments types=" + types [0]. GetTypeName ());
}
The return types.
}
@ Override
The public Type getOwnerType () {
return null;
}
@ Override
The public Type getRawType () {
Return ResponseResult. Class;
}
}
Public OnHttpResultListener jsonLogin (OnUiCallbackThe callback) {
Return jsonHttpWithData (Token. The class, the callback, new OnNextListener() {
@ Override
Public void onNext (Token Token) {
CacheDataModel. GetInstance (). SaveLoginData (token);
}
});
}
Method calls started with jsonLogin
Exception:
under Caused by: Java. Lang. ClassCastException: com. Google. Gson. Internal. LinkedTreeMap always be cast to com. Xx. Xx. The entity. The response. VehicleBindingList
Request the results Shared ResponseResult
Problem: generic do not specify a specific type,
Want to ask how to solve, to share fromJson this line of code,
Request the results analysis, sharing the jsonHttpWithData method, Class
Why not use the
return new ParameterizedType () {
@ Override
The public Type [] getActualTypeArguments () {
Return the args.
}
@ Override
The public Type getRawType () {
Return raw;
}
@ Override
The public Type getOwnerType () {
return null;
}
};
No such specified directly reason is: the data not only the object, also may be List