加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Volley 结合GSON或FastJson用法

发布时间:2020-12-16 19:04:15 所属栏目:百科 来源:网络整理
导读:自定义GSON类 1 public class GsonRequestT extends RequestT { 2 private final Gson mGson = new Gson(); 3 final ClassT mClazz; 4 final ListenerT mListener; 5 final MapString,String mHeaders; 6 7 public GsonRequest(String url,ClassT clazz,List

自定义GSON类

 1 public class GsonRequest<T> extends Request<T> {
 2     private final Gson mGson = new Gson();
 3     final Class<T> mClazz;
 4     final Listener<T> mListener;
 5     final Map<String,String> mHeaders;
 6 
 7     public GsonRequest(String url,Class<T> clazz,Listener<T> listener,ErrorListener errorListener) {
 8         this(Method.GET,url,clazz,null,listener,errorListener);
 9     }
10 
11     public GsonRequest(int method,String url,Map<String,String> headers,12             Listener<T> listener,128); line-height:1.5!important">13         super(method,128); line-height:1.5!important">14         this.mClazz = clazz;
15         this.mHeaders = headers;
16         this.mListener = listener;
17     }
18 
19     @Override
20     public Map<String,String> getHeaders() throws AuthFailureError {
21         return mHeaders != null ? mHeaders : super.getHeaders();
22     }
23 
24     @Override
25     protected void deliverResponse(T response) {
26         mListener.onResponse(response);
27     }
28 
29     @Override
30     protected Response<T> parseNetworkResponse(NetworkResponse response) {
31         try {
32             String json = new String(response.data,HttpHeaderParser.parseCharset(response.headers));
33             return Response.success(mGson.fromJson(json,mClazz),128); line-height:1.5!important">34                     HttpHeaderParser.parseCacheHeaders(response));
35         } catch (UnsupportedEncodingException e) {
36             return Response.error(new ParseError(e));
37         } catch (JsonSyntaxException e) {
38             39         }
40     }
41 }

自定义FastJson类

class FastJsonRequest<T> extends Request<T> { 5 6 public FastJsonRequest(String url,128); line-height:1.5!important"> 7 8 } 9 10 public FastJsonRequest(11 Listener<T> listener,128); line-height:1.5!important">12 16 } 17 18 @Override 19 20 21 } 22 23 @Override 24 25 mListener.onResponse(response); 26 } 27 28 @Override 29 30 31 String json = 32 return Response.success(JSON.parSEObject(json,128); line-height:1.5!important">33 HttpHeaderParser.parseCacheHeaders(response)); 34 } 35 36 } 37 } 38 }
class MainActivity extends Activity { private Context context; private String url="http://www.weather.com.cn/data/sk/101010100.html"; private TextView text; 5 6 @Override void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 context=this; 11 text=(TextView)this.findViewById(R.id.txttitle); 12 RequestQueue mQueue = Volley.newRequestQueue(context); 13 FastJsonRequest<Weather> fastJson=new FastJsonRequest<Weather>(url,Weather.class,128); line-height:1.5!important">14 new Response.Listener<Weather>() { 15 16 @Override 17 void onResponse(Weather weather) { 18 // TODO Auto-generated method stub 19 WeatherInfo weatherInfo = weather.getWeatherinfo(); 20 text.setText(weatherInfo.getCity()); 21 } 22 },255); line-height:1.5!important">new Response.ErrorListener() { 24 @Override 25 void onErrorResponse(VolleyError arg0) { 26 27 28 } 29 }); 30 mQueue.add(fastJson); 31 32 GsonRequest<Weather> gsonRequest=new GsonRequest<Weather>(url,128); line-height:1.5!important">33 34 35 @Override 36 37 38 WeatherInfo weatherInfo = weather.getWeatherinfo(); 39 text.setText(text.getText()+weatherInfo.getTime()); 40 } 41 },128); line-height:1.5!important">42 43 @Override 44 45 46 47 } 48 }); 49 mQueue.add(gsonRequest); 50 51 52 } 53 }
Model类

class Weather { private WeatherInfo weatherinfo; 3 public WeatherInfo getWeatherinfo() { 5 return weatherinfo; 6 } 7 8 void setWeatherinfo(WeatherInfo weatherinfo) { 9 this.weatherinfo = weatherinfo; 10 } 11 }
class WeatherInfo { private String city; private String temp; 5 private String time; public String getCity() { return city; 11 12 void setCity(String city) { this.city = city; 14 } 15 16 public String getTemp() { 17 return temp; 18 } 19 void setTemp(String temp) { this.temp = temp; 22 } 23 public String getTime() { 25 return time; 26 } 27 28 void setTime(String time) { 29 this.time = time; 30 } 31 }

转自:http://www.cnblogs.com/freexiaoyu/p/3955137.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读