Home > Mobile >  Results of analytical problem, network request for guidance
Results of analytical problem, network request for guidance

Time:01-12


 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 (Class Clazz OnUiCallback OnUiCallback OnNextListener OnNextListener) {
Return new OnHttpResultListener () {
@ Override
Public void onSuccess (String s) {
if (! TextUtils. IsEmpty (s)) {
GsonResponsePasare Pasare=new GsonResponsePasare () {

};
ResponseResult ResponseResult=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 GsonResponsePasare Implements ParameterizedType {

Public ResponseResult Deal (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;
ResponseResult Result=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 (OnUiCallback The 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 Result=new Gson (). FromJson (response, gsonType);

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 Clazz now don't have access to, useful to the before,
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

Thank you



CodePudding user response:

Wow, you see the description of my fog
1. Your problem is to convert the result of the network request to entity class?
2. Do you want to multiple requests the results with the same entity class conversion?

CodePudding user response:

reference 1st floor can't write code monkey reply:
wow, you see the description of my fog
1. Your problem is to convert the result of the network request to entity class?
2. Do you want to multiple requests the results with the same entity class conversion?
sorry, description is not good,
Want to share a parsing methods do,
nullnullnullnullnullnullnull
  • Related