自定义Volley的GsonRequest使用

本文介绍了一种利用Gson与Volley库相结合的方式进行JSON数据解析的方法。通过自定义GsonRequest类实现了从指定URL获取JSON数据,并自动转换为Java对象的过程。示例中展示了如何创建请求并处理响应数据。
摘要由CSDN通过智能技术生成
Gson结合Volley类:
package com.example.cuboo.myapplication;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.google.gson.Gson;

import java.io.UnsupportedEncodingException;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;

/**
 * Created by cuboo on 2016/10/26.
 */

public class GsonRequest<T> extends Request<T> {
    private Response.Listener<T> mListener;
    private Gson mGson;
    private Class<T> mClass;

    public GsonRequest(int method, String url,Class<T> mClass, Response.Listener<T> listener,Response.ErrorListener errorListener) {
        super(method, url, errorListener);
        this.mListener = listener;
        this.mClass = mClass;
    }
    public GsonRequest(String url,Class<T> mClass, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        this(Method.GET, url, mClass, listener, errorListener);
    }

    @Override
    protected Response<T> parseNetworkResponse(NetworkResponse response) {
        String jsonString;
        try {
            jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers));

        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        }
        return Response.success(mGson.fromJson(jsonString, mClass),
                HttpHeaderParser.parseCacheHeaders(response));
    }

    @Override
    protected void deliverResponse(T response) {
        this.mListener.onResponse(response);
    }
}


数据类:

<pre style="font-family: 宋体; font-size: 9.8pt; background-color: rgb(255, 255, 255);"><pre name="code" class="java">package com.example.cuboo.myapplication;


public class weather {
    private weatherinfo weatherinfo;

    public weatherinfo getWeatherinfo() {
        return weatherinfo;
    }

    public void setWeatherinfo(weatherinfo weatherinfo) {
        this.weatherinfo = weatherinfo;
    }
}

 
 
package com.example.cuboo.myapplication;

/**
 * Created by cuboo on 2016/10/26.
 */

public class weatherinfo {
    private String city;

    private String temp;

    private String time;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getTemp() {
        return temp;
    }

    public void setTemp(String temp) {
        this.temp = temp;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }
}


使用:

    RequestQueue mqueue = Volley.newRequestQueue(this);
        GsonRequest<weather> gsonRequest = new GsonRequest<weather>
                ("http://www.weather.com.cn/data/sk/101010100.html"
                , weather.class, new Response.Listener<weather>() {
            @Override
            public void onResponse(weather s) {
                weatherinfo weatherInfo = s.getWeatherinfo();
                Log.d("TAG", "city is " + weatherInfo.getCity());
                Log.d("TAG", "temp is " + weatherInfo.getTemp());
                Log.d("TAG", "time is " + weatherInfo.getTime());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("TAG", volleyError.getMessage(), volleyError);
            }
        });
        mqueue.add(gsonRequest);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值