文章目录

利用Gson和Volley实现URL读取JSON数据转model类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
GsonRequest<T> extends Request<T> {
private final GSON gson = new Gson();
private final Class<T> clazz;
private final Map<String,String> headers;
private final Listener<T> listener;

public GsonRequest(String url,Class<T> clazz,Map<String,String> headers,Listener<T> listener,ErrorListener errorListener){
super(Method.GET,url,errorListener);
this.clazz = clazz;
this.headers headers;
this.listener = listener;
}

@Override
public Map<String,String> getHeaders() throws AuthFailureError {
return headers != null ? headers : super.getHeaders();
}

@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));
}
}
}

  • 使用
  1. 创建executeRequest方法

    1
    2
    3
    void executeRequest(Request request) {
    RequestManager.addRequest(request, this);
    }
  2. 调用请求

    1
    executeRequest(new GsonRequest<Model>(url,Model.class,null,listener,errorListner);
  3. 在listener中处理数据

    1
    2
    3
    4
    5
    6
    new Listner<Moderl>(){
    @Override
    public void onResponse(final Model requestData) {
    //
    }
    }
文章目录