我已经创建了凌空自定义帖子GSon请求,但我无法调用它。
GSON POST请求:
public class GsonRequest<T> extends Request<T> {
private final Gson gson = new Gson();
private final Class<T> clazz;
private final Map<String, String> headers;
private final Map<String, String> params;
private final Response.Listener<T> listener;
/**
* Make a GET request and return a parsed object from JSON.
*
* @param url URL of the request to make
* @param clazz Relevant class object, for Gson's reflection
* @param headers Map of request headers
*/
public GsonRequest(String url, Class<T> clazz, Map<String, String> headers,
Response.Listener<T> listener, Response.ErrorListener errorListener) {
super(Method.GET, url, errorListener);
this.clazz = clazz;
this.headers = headers;
this.listener = listener;
this.params = null;
}
/**
* Make a POST request and return a parsed object from JSON.
*
* @param url URL of the request to make
* @param clazz Relevant class object, for Gson's reflection
*/
public GsonRequest(int method, String url, Class<T> clazz, Map<String, String> params,
Response.Listener<T> listener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.clazz = clazz;
this.params = params;
this.listener = listener;
this.headers = null;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers != null ? headers : super.getHeaders();
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return params;
}
@Override
protected void deliverResponse(T response) {
listener.onResponse(response);
}
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(
response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(
gson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JsonSyntaxException e) {
return Response.error(new ParseError(e));
}
}
}
**响应通用模型类:**
public class BaseResponse<T> implements Serializable {
private boolean response;
private String message;
private String status;
private String userStatus;
private String token;
private T data;
public boolean isResponse() {
return response;
}
public void setResponse(boolean response) {
this.response = response;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public String getStatus() {
return status;
}
public String getUserStatus() {
return userStatus;
}
public String getToken() {
return token;
}
}
数据模型类:
public class OTPCode implements Serializable {
private String code;
@SerializedName("mobile")
private String phone;
@SerializedName("id")
private String userId;
private String name;
private String email;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
以下是显示错误的回调:
GsonRequest<BaseResponse<OTPCode>> baseResponseGsonRequest = new GsonRequest<BaseResponse<OTPCode>>
(Request.Method.POST,url,BaseResponse<OTPCode>.class, params, new Response.Listener<BaseResponse<OTPCode>>() {
@Override
public void onResponse(BaseResponse<OTPCode> response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
我正在将BaseResponse.class解析为GsonRequest但是它 告诉我错误。我可能会犯一些错误。请帮忙!!
答案 0 :(得分:0)
由于BaseResponse是泛型类,.class不起作用,使用Type
输入baseResponseType = new TypeToken&lt;&gt;(){}。getType();