自定義Request
public class StringRequest extends Request<String> {
//監(jiān)聽器
private final Listener<String> mListener;
//構(gòu)造方法 這時(shí)一定要調(diào)用super
public StringRequest(int method, String url, Listener<String> listener, ErrorListener errorListener) {
super(method, url, errorListener);
this.mListener = listener;
}
//調(diào)用上面的方法
public StringRequest(String url, Listener<String> listener, ErrorListener errorListener) {
this(0, url, listener, errorListener);
}
//只是deliverResponse()方法中的實(shí)現(xiàn)很簡(jiǎn)單社付,僅僅是調(diào)用了mListener中的onResponse()方法,并將response內(nèi)容傳入即可焰络,這樣就可以將服務(wù)器響應(yīng)的數(shù)據(jù)進(jìn)行回調(diào)了
protected void deliverResponse(String response) {
this.mListener.onResponse(response);
}
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String parsed;
try {
//parseCharset字符編碼
parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
} catch (UnsupportedEncodingException var4) {
parsed = new String(response.data);
}
//頭信息
return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
}
由于Request類中的deliverResponse()和parseNetworkResponse()是兩個(gè)抽象方法,
因此StringRequest中需要對(duì)這兩個(gè)方法進(jìn)行實(shí)現(xiàn)抡柿。deliverResponse()方法中的實(shí)現(xiàn)很簡(jiǎn)單舔琅,僅僅是調(diào)用了mListener中的onResponse()方法,并將response內(nèi)容傳入即可洲劣,這樣就可以將服務(wù)器響應(yīng)的數(shù)據(jù)進(jìn)行回調(diào)了备蚓。
parseNetworkResponse()方法中則應(yīng)該對(duì)服務(wù)器響應(yīng)的數(shù)據(jù)進(jìn)行解析,其中數(shù)據(jù)是以字節(jié)的形式存放在NetworkResponse的data變量中的,這里將數(shù)據(jù)取出然后組裝成一個(gè)String囱稽,并傳入Response的success()方法中即可郊尝。
自定義GsonRequest
package com.liu.volleydemo.requestion;
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;
/**
* @Description: 描述
* @AUTHOR 劉楠 Create By 2016/9/26 0026 18:11
*/
public class GsonRequest<T> extends Request<T> {
private final Response.Listener<T> mListener;
private Gson mGson;
private Class<T> mClazz;
public GsonRequest(int method, String url, Class<T> clazz, Response.Listener<T> listener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.mListener = listener;
mGson= new Gson();
mClazz = clazz;
}
public GsonRequest(String url, Class<T> clazz, Response.Listener<T> listener, Response.ErrorListener errorListener) {
this(Method.GET, url, clazz, listener, errorListener);
}
@Override
protected void deliverResponse(T response) {
mListener.onResponse(response);
}
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
/**
* 獲取數(shù)據(jù)
*/
String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
// 轉(zhuǎn)化反回
return Response.success(mGson.fromJson(jsonString, mClazz), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return Response.error(new ParseError(e));
}
}
}
=================BEAN==========
package com.liu.volleydemo.bean;
import java.util.List;
/**
* @Description: 描述
* @AUTHOR 劉楠 Create By 2016/9/26 0026 18:36
*/
public class WeixinSelected {
public String reason;
public ResultEntity result;
public int error_code;
@Override
public String toString() {
return "WeixinSelected{" +
"reason='" + reason + '\'' +
", result=" + result +
", error_code=" + error_code +
'}';
}
public static class ResultEntity {
public int totalPage;
public int ps;
public int pno;
@Override
public String toString() {
return "ResultEntity{" +
"totalPage=" + totalPage +
", ps=" + ps +
", pno=" + pno +
", list=" + list +
'}';
}
public List<ListEntity> list;
public static class ListEntity {
public String firstImg;
public String id;
public String source;
public String title;
public String url;
public String mark;
@Override
public String toString() {
return "ListEntity{" +
"firstImg='" + firstImg + '\'' +
", id='" + id + '\'' +
", source='" + source + '\'' +
", title='" + title + '\'' +
", url='" + url + '\'' +
", mark='" + mark + '\'' +
'}';
}
}
}
}
請(qǐng)求
final RequestQueue requestQueue = MyVolley.getRequestQueue(this);
String url ="http://v.juhe.cn/weixin/query";
final String appkey ="24834287692b8d54cf634668ce5de0b4";
GsonRequest<WeixinSelected> gsonRequest = new GsonRequest<WeixinSelected>(Request.Method.POST,url, WeixinSelected.class, new Response.Listener<WeixinSelected>() {
@Override
public void onResponse(WeixinSelected weixinSelected) {
Log.d("vivi", "onResponse: "+weixinSelected.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
volleyError.printStackTrace();
Log.d("vivi", "onErrorResponse: "+volleyError.getMessage());
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put("key",appkey);
return params;
}
};
requestQueue.add(gsonRequest);
日志
WeixinSelected{reason='success', result=ResultEntity{totalPage=25, ps=20, pno=1, list=[ListEntity{firstImg='http://zxpic.gtimg.com/infonew/0/wechat_pics_-8255071.jpg/640', id='wechat_20160915039850', source='功夫體育', title='女排冠軍們“高”人一等,誰(shuí)敢娶战惊?', url='http://v.juhe.cn/weixin/redirect?wid=wechat_20160915039850', mark=''}, ListEntity{firstImg='http://zxpic.gtimg.com/infonew/0/wechat_pics_-8575838.jpg/640', id='wechat_20151201058612', source='龍神迷失', title='中國(guó)首輛上牌的布加迪威龍流昏,以為車牌很一般,最后發(fā)現(xiàn)車牌很霸氣', url='http://v.juhe.cn/weixin/redirect?wid=wechat_20151201058612', mark=''}, ListEntity{firstImg='http://zxpic.gtimg.com/infonew/0/wechat_pics_-8206632.jpg/640', id='wechat_20160102022165', source='籃球頻道', title='【球迷關(guān)注點(diǎn)】書豪:一度想退役不再打球 曾被科比一句蔑視激怒', url='http://v.juhe.cn/weixin/redirect?wid=wechat_20160102022165', mark=''}, ListEntity{firstImg='http://zxpic.gtimg.com/infonew/0/wechat_pics_-8064767.jpg/640', id='wechat_20160908053966', source='娛樂(lè)圈扒姐', title='某知名女星在節(jié)目中威脅選手收賄吞获,私下愛(ài)搞潛規(guī)則', url='http://v.juhe.cn/weixin/redirect?wid=wechat_20160908053966', mark=''}, ListEntity{firstImg='http://zxpic.gtimg.com/infonew/0/wechat_pics_-7986866.jpg/640', id='wechat_20160906017693', source='顏小白的籃球夢(mèng)', title='科比跟喬丹比到底差在哪里况凉??jī)H僅是身體素質(zhì)嗎', url='http://v.juhe.cn/weixin/redirect?wid=wechat_20160906017693', mark=''}, ListEntity{firstImg='http://zxpic.gtimg.com/infonew/0/wechat_pics_-7986843.jpg/640', id='wechat_20160906017663', source='嘻嘻哈哈', title='神動(dòng)圖|這手感絕對(duì)不差!', url='http://v.juhe.cn/weixin/redirect?wid=wechat_20160906017663', mark=''}, ListEntity{firstImg='http://zxpic.gtimg.com/infonew/0/wechat_pics_-7986833.jpg/640', id='wechat_20160906017658', source='嘻嘻哈哈', title='江湖流傳的這些奇葩書各拷,簡(jiǎn)直刷新我的三觀~', url='http://v.juhe.cn/weixin/redirect?wid=wechat_20160906017658', mark=''}, ListEntity{firstImg='http://zxpic.gtimg.com/infonew/0/wechat_pics_-7986819.jpg/640', id='wechat_20160906017552', source='笑你妹', title='【搞笑段子】有心無(wú)力啊刁绒,辦起事來(lái)幾分鐘完事~', url='http://v.juhe.cn/weixin/redirect?wid=wechat_20160906017552', mark=''}, ListEntity{firstImg='http://zxpic.gtimg.com/infonew/0/wechat_pics_-7986802.jpg/640', id='wechat_20160906017288', source='TechWeb', title='在iPhone7發(fā)布之前我們先來(lái)看看它的跑分吧', url='http://v.juhe.cn/weixin/redirect?wid=wechat_20160906017288', mark=''}, ListEntity{firstImg='http://zxpic.gtimg.com/infonew/0/wechat_pics_-7986693.jpg/640', id='wechat_20160906016354', source='隴南生活網(wǎng)', title='甘肅:將發(fā)生嚴(yán)重腐敗問(wèn)題的地區(qū)部門直接評(píng)為“一般”', url='http://v.juhe.cn/weixin/redirect?wid=wechat_20160906016354', mark=''}, ListEntity{firstImg='http://zxpic.gtimg.com/infonew/0/wechat_pics_-7986675.jpg/640', id='wechat_20160906016213', source='云上的空母', title='萬(wàn)噸兩棲艦上的袖珍火炮,射速為每分鐘200發(fā)', url='http://v.juhe.cn/weixin/redirect?wid=wechat_20160906016213', mark=''}, ListEntity{firstImg='http://zxpic.gtimg.com/infonew/0/wechat_pics_-7986645.jpg/640', id='wechat_20160906016204', source='云上的空母', title='全球最大口徑的加特林機(jī)關(guān)炮烤黍,一眨眼打出50發(fā)炮彈', url='http://v.juhe.cn/weixin/redirect?wid=wechat_20160906016204', mark=''}, ListEntity{firstImg='http://zxpic.gtimg.com/infonew/0/wechat_pics_-7986534.jpg/640', id='wechat_20160906015882', source='鈦媒體', title='三星耗10億美元召回Note7知市,并稱損失令人real心痛鈦晨報(bào)', url='http://v.juhe.cn/weixin/redirect?wid=wechat_20160906015882', mark=''}, ListEntity{firstImg='http://zxpic.gtimg.com/infonew/0/wechat_pics_-7986438.jpg/640', id='wechat_20160906015702', source='攝影筆記', title='一篇文章看懂尼康數(shù)碼單反', url='http://v.juhe.cn/weixin/redirect?wid=wechat_20