欢迎访问 生活随笔!

尊龙游戏旗舰厅官网

当前位置: 尊龙游戏旗舰厅官网 > 运维知识 > android >内容正文

android

android网络开发之volley-尊龙游戏旗舰厅官网

发布时间:2025/1/21 android 33 豆豆
尊龙游戏旗舰厅官网 收集整理的这篇文章主要介绍了 android网络开发之volley--volley自定义request 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1、自定义一个解析json的request,这里使用jackson框架来解析json。你也可以自定义一个解析xml的request,或者使用fastson来解析json。

2、我们首先来看一下stringrequest的源码。继承自request,主要是重写parsenetworkresponse()和deliverresponse()方法。

public class stringrequest extends request {private final listener mlistener;/*** creates a new request with the given method.** @param method the request {@link method} to use* @param url url to fetch the string at* @param listener listener to receive the string response* @param errorlistener error listener, or null to ignore errors*/public stringrequest(int method, string url, listener listener,errorlistener errorlistener) {super(method, url, errorlistener);mlistener = listener;}/*** creates a new get request.** @param url url to fetch the string at* @param listener listener to receive the string response* @param errorlistener error listener, or null to ignore errors*/public stringrequest(string url, listener listener, errorlistener errorlistener) {this(method.get, url, listener, errorlistener);}@overrideprotected void deliverresponse(string response) {mlistener.onresponse(response);}@overrideprotected response parsenetworkresponse(networkresponse response) {string parsed;try {parsed = new string(response.data, httpheaderparser.parsecharset(response.headers));} catch (unsupportedencodingexception e) {parsed = new string(response.data);}return response.success(parsed, httpheaderparser.parsecacheheaders(response));} }

3、首先定义实体类

public class weather {private weatherinfo weatherinfo;public weatherinfo getweatherinfo() {return weatherinfo;}@jsonproperty("weatherinfo")public void setweatherinfo(weatherinfo weatherinfo) {this.weatherinfo = weatherinfo;}} public class weatherinfo {private string city;private string cityid;private string temp;private string wd;private string ws;private string sd;private string wse;private string time;private string isradar;private string radar;private string njd;private string qy;public string getcity() {return city;}@jsonproperty("city")public void setcity(string city) {this.city = city;}public string getcityid() {return cityid;}@jsonproperty("cityid")public void setcityid(string cityid) {this.cityid = cityid;}public string gettemp() {return temp;}@jsonproperty("temp")public void settemp(string temp) {this.temp = temp;}public string getwd() {return wd;}@jsonproperty("wd")public void setwd(string wd) {wd = wd;}public string getws() {return ws;}@jsonproperty("ws")public void setws(string ws) {ws = ws;}public string getsd() {return sd;}@jsonproperty("sd")public void setsd(string sd) {sd = sd;}public string getwse() {return wse;}@jsonproperty("wse")public void setwse(string wse) {wse = wse;}public string gettime() {return time;}@jsonproperty("time")public void settime(string time) {this.time = time;}public string getisradar() {return isradar;}@jsonproperty("isradar")public void setisradar(string isradar) {this.isradar = isradar;}public string getradar() {return radar;}@jsonproperty("radar")public void setradar(string radar) {radar = radar;}public string getnjd() {return njd;}@jsonproperty("njd")public void setnjd(string njd) {this.njd = njd;}public string getqy() {return qy;}@jsonproperty("qy")public void setqy(string qy) {this.qy = qy;}}

4、实现jacksonrequest

public class jacksonrequest extends request{private final listener mlistener;private class mclass;private static objectmapper objectmapper = new objectmapper();public jacksonrequest(int method, string url, class clazz, listener listener, errorlistener errorlistener) {super(method, url, errorlistener);// todo auto-generated constructor stubmlistener = listener;mclass = clazz;}public jacksonrequest(string url, class clazz, listener listener, errorlistener errorlistener){this(method.get, url, clazz, listener, errorlistener);}@overrideprotected response parsenetworkresponse(networkresponse response) {// todo auto-generated method stubtry {string jsonstring = new string(response.data, httpheaderparser.parsecharset(response.headers));return response.success(objectmapper.readvalue(jsonstring, mclass), httpheaderparser.parsecacheheaders(response));} catch (unsupportedencodingexception e) {// todo auto-generated catch blockreturn response.error(new parseerror(e));} catch (jsonparseexception e) {// todo auto-generated catch block e.printstacktrace();} catch (jsonmappingexception e) {// todo auto-generated catch block e.printstacktrace();} catch (ioexception e) {// todo auto-generated catch block e.printstacktrace();}return null;}@overrideprotected void deliverresponse(t response) {// todo auto-generated method stub mlistener.onresponse(response);}}

5、jackson的使用,和stringrequest的使用是一样的。

public class jacksonactivity extends activity {private requestqueue requestqueue;private textview mtvshow;private string result = "";@overrideprotected void oncreate(bundle savedinstancestate) {// todo auto-generated method stubsuper.oncreate(savedinstancestate);setcontentview(r.layout.activity_jackson);initview();}public void initview(){mtvshow = (textview) findviewbyid(r.id.tv_jackson);requestqueue = volley.newrequestqueue(getbasecontext());requestqueue.add(jacksonrequest);requestqueue.start();}private jacksonrequest jacksonrequest = new jacksonrequest("http://www.weather.com.cn/data/sk/101010100.html", weather.class,new response.listener() {@overridepublic void onresponse(weather response) {// todo auto-generated method stubweatherinfo info = response.getweatherinfo();result = info.getcity() "\n" info.gettemp() "\n" info.gettime();mtvshow.settext(result);}}, new response.errorlistener(){@overridepublic void onerrorresponse(volleyerror error) {// todo auto-generated method stub mtvshow.settext(error.tostring());}}); }

6、不要忘记加入网络访问权限

7、参考博文:

http://blog.csdn.net/guolin_blog/article/details/17482095/

转载于:https://www.cnblogs.com/begin1949/p/4925634.html

总结

以上是尊龙游戏旗舰厅官网为你收集整理的android网络开发之volley--volley自定义request的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得尊龙游戏旗舰厅官网网站内容还不错,欢迎将尊龙游戏旗舰厅官网推荐给好友。

网站地图