Gson解析和Volley框架并用

[java]  view plain  copy
  1. package com.example.liuyazhou.mythirdapplication;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8.   
  9. import com.android.volley.RequestQueue;  
  10. import com.android.volley.Response;  
  11. import com.android.volley.VolleyError;  
  12. import com.android.volley.toolbox.Volley;  
  13.   
  14. import weather.Data;  
  15. import weather.Life;  
  16. import weather.Info;  
  17. import weather.Realtime;  
  18. import weather.Result;  
  19. import weather.ALLWeather;  
  20.   
  21. public class MainActivity extends Activity {  
  22.   
  23.     private Button button;  
  24.   
  25.     private String urlJsonWeather = "http://op.juhe.cn/onebox/weather/query?cityname=%E5%8C%97%E4%BA%AC&dtype=json&key=******1b7942cbdcff19a08001";  
  26.     private static String TAG = MainActivity.class.getSimpleName();/key需要自己申请  
  27.     private String jsonResponse;  
  28.   
  29.     @Override  
  30.     protected void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.activity_main);  
  33.         button = (Button) findViewById(R.id.button);  
  34.         button.setOnClickListener(new View.OnClickListener() {  
  35.             @Override  
  36.             public void onClick(View v) {  
  37.                 funWeatherGsonRequest();  
  38.             }  
  39.         });  
  40.     }  
  41.     Volley 三  http://blog.csdn.net/guolin_blog/article/details/17612763  
  42.     public void funWeatherGsonRequest() {  
  43.         RequestQueue mQueue = Volley.newRequestQueue(MainActivity.this);  
  44.         GsonRequest<ALLWeather> gsonRequest = new GsonRequest<ALLWeather>(  
  45.                 urlJsonWeather, ALLWeather.class,  
  46.                 new Response.Listener<ALLWeather>() {  
  47.                     @Override  
  48.                     public void onResponse(ALLWeather allWeather) {  
  49.                         Log.i(TAG, allWeather.toString());  
  50.                         Log.i(TAG, "reason is: " + allWeather.getReason());  
  51.   
  52.                         Result result = allWeather.getResult();  
  53.                         Log.i(TAG, "result is: "+result.toString());  
  54.                        Data data = result.getData();  
  55.                         Log.i(TAG, "data is: "+data.toString());  
  56.   
  57.                         Realtime realtime =data.getRealtime();  
  58.                         Log.i(TAG, "realtime is: "+realtime.toString());  
  59.                         Log.i(TAG, "realtime.getCity_code(): "+ realtime.getCity_code());  
  60.   
  61.                         Life life  = data.getLife();  
  62.                         Log.i(TAG, "life is: "+life.toString());  
  63.                         Log.i(TAG, " life.getDate(): "+ life.getDate());  
  64.   
  65.   
  66.                         Info lifeInfo  = life.getInfo();///这里对象名是lifeInfo是允许的  
  67.                         Log.i(TAG, "lifeInfo is: "+lifeInfo.toString());  
  68. //                        Log.i(TAG, "chuanyi is: "+lifeInfo.toString());  
  69.                        Log.i(TAG, "chuanyi is: " + lifeInfo.getChuanyi()[0]+","+lifeInfo.getChuanyi()[1]);  
  70.   
  71.                     }  
  72.                 }, new Response.ErrorListener() {  
  73.             @Override  
  74.             public void onErrorResponse(VolleyError error) {  
  75.                 Log.e("TAG", error.getMessage(), error);  
  76.             }  
  77.         });  
  78.         mQueue.add(gsonRequest);  
  79.     }  
  80. }  
  81. package weather;  
  82.   
  83. /** 
  84.  * Created by liuyazhou on 2016/9/15. 
  85.  */  
  86. public class ALLWeather {  
  87.     private String reason;  
  88.     private Result result;  
  89.   
  90.     public String getReason() {  
  91.         return reason;  
  92.     }  
  93.   
  94.     public void setReason(String reason) {  
  95.         this.reason = reason;  
  96.     }  
  97.   
  98.     public Result getResult() {  
  99.         return result;  
  100.     }  
  101.   
  102.     public void setResult(Result result) {  
  103.         this.result = result;  
  104.     }  
  105. }  
  106. package weather;  
  107.   
  108. /** 
  109.  * Created by liuyazhou on 2016/9/15. 
  110.  */  
  111. public class Result {  
  112.     private Data data;  
  113.   
  114.     public Data getData() {  
  115.         return data;  
  116.     }  
  117.   
  118.     public void setData(Data data) {  
  119.         this.data = data;  
  120.     }  
  121. }  
  122. package weather;  
  123.   
  124. /** 
  125.  * Created by liuyazhou on 2016/9/15. 
  126.  */  
  127. public class Data {  
  128.     private Realtime  realtime;  
  129.     private  Life life;  
  130.   
  131.     public Realtime getRealtime() {  
  132.         return realtime;  
  133.     }  
  134.   
  135.     public void setRealtime(Realtime realtime) {  
  136.         this.realtime = realtime;  
  137.     }  
  138.   
  139.     public Life getLife() {  
  140.         return life;  
  141.     }  
  142.   
  143.     public void setLife(Life life) {  
  144.         this.life = life;  
  145.     }  
  146. }  
  147. package weather;  
  148.   
  149. /** 
  150.  * Created by liuyazhou on 2016/9/15. 
  151.  */  
  152. public class Realtime {  
  153. //    "city_code":"101010100",  
  154. //            "city_name":"北京",  
  155. //            "date":"2016-09-15",  
  156. //            "time":"15:00:00",  
  157. //            "week":4,  
  158. //            "moon":"八月十五",  
  159. //            "dataUptime":1473925023,  
  160.   //  private  String  cityCode;  
  161. private  String  city_code;  
  162.   
  163.   
  164. //    public String getCityCode() {  返回null  
  165. //        return cityCode;  
  166. //    }  
  167. //  
  168. //    public void setCityCode(String cityCode) {  
  169. //        this.cityCode = cityCode;  
  170. //    }  
  171.   
  172.     public String getCity_code() {  
  173.         return city_code;  
  174.     }  
  175.   
  176.     public void setCity_code(String city_code) {  
  177.         this.city_code = city_code;  
  178.     }  
  179.   
  180. }  
  181. package weather;  
  182.   
  183. /** 
  184.  * Created by liuyazhou on 2016/9/15. 
  185.  */  
  186. public class Life {  
  187.    private  String  date;  
  188.    // private Info lifeInfo;错误的,类定义中的属性名、对象名、类名要与json里的key一致  
  189.    private Info info;  
  190.     public String getDate() {  
  191.         return date;  
  192.     }  
  193.   
  194.     public void setDate(String date) {  
  195.         this.date = date;  
  196.     }  
  197.   
  198.     public Info getInfo() {  
  199.         return info;  
  200.     }  
  201.   
  202.     public void setInfo(Info info) {  
  203.         this.info = info;  
  204.     }  
  205. }  
  206. package weather;  
  207.   
  208. /** 
  209.  * Created by liuyazhou on 2016/9/15. 
  210.  */  
  211. public class Info {  
  212.   
  213. //    "info":{  
  214. //        "chuanyi":[  
  215. //        "热",  
  216. //                "天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。"  
  217. //        ],  
  218. //        "ganmao":[  
  219. //        "少发",  
  220. //                "各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。"  
  221. //        ],  
  222.   
  223.     private  String[] chuanyi;  
  224.     private  String[] ganmao;  
  225.   
  226.     public String[] getChuanyi() {  
  227.         return chuanyi;  
  228.     }  
  229.   
  230.     public void setChuanyi(String[] chuanyi) {  
  231.         this.chuanyi = chuanyi;  
  232.     }  
  233.   
  234.     public String[] getGanmao() {  
  235.         return ganmao;  
  236.     }  
  237.   
  238.     public void setGanmao(String[] ganmao) {  
  239.         this.ganmao = ganmao;  
  240.     }  
  241. }  

[java]  view plain  copy
  1. package com.example.liuyazhou.mythirdapplication;  
  2.   
  3. /** 
  4.  * Created by liuyazhou on 2016/9/15. 
  5.  */  
  6. Volley 三  http://blog.csdn.net/guolin_blog/article/details/17612763  
  7. import com.android.volley.NetworkResponse;  
  8. import com.android.volley.ParseError;  
  9. import com.android.volley.Request;  
  10. import com.android.volley.Response;  
  11. import com.android.volley.toolbox.HttpHeaderParser;  
  12. import com.google.gson.Gson;  
  13.   
  14. import java.io.UnsupportedEncodingException;  
  15.   
  16. /** 
  17.  * Created by liuyazhou on 2016/9/15. 
  18.  */  
  19. public class GsonRequest<T> extends Request<T> {  
  20.   
  21.     private final Response.Listener<T> mListener;  
  22.   
  23.     private Gson mGson;  
  24.   
  25.     private Class<T> mClass;  
  26.   
  27.     public GsonRequest(int method, String url, Class<T> clazz, Response.Listener<T> listener,  
  28.                        Response.ErrorListener errorListener) {  
  29.         super(method, url, errorListener);  
  30.         mGson = new Gson();  
  31.         mClass = clazz;  
  32.         mListener = listener;  
  33.     }  
  34.   
  35.     public GsonRequest(String url, Class<T> clazz, Response.Listener<T> listener,  
  36.                        Response.ErrorListener errorListener) {  
  37.         this(Method.GET, url, clazz, listener, errorListener);  
  38.     }  
  39.   
  40.     @Override  
  41.     protected Response<T> parseNetworkResponse(NetworkResponse response) {  
  42.         try {  
  43.             String jsonString = new String(response.data,  
  44.                     HttpHeaderParser.parseCharset(response.headers));  
  45.             return Response.success(mGson.fromJson(jsonString, mClass),  
  46.                     HttpHeaderParser.parseCacheHeaders(response));  
  47.         } catch (UnsupportedEncodingException e) {  
  48.             return Response.error(new ParseError(e));  
  49.         }  
  50.     }  
  51.   
  52.     @Override  
  53.     protected void deliverResponse(T response) {  
  54.         mListener.onResponse(response);  
  55.     }  
  56.   
  57. }  

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context="com.example.liuyazhou.mythirdapplication.MainActivity">  
  11.   
  12.     <TextView  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="Hello World!"  
  16.         android:id="@+id/textView" />  
  17.   
  18.     <Button  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:text="点击"  
  22.         android:id="@+id/button"  
  23.         android:layout_below="@+id/textView"  
  24.         android:layout_alignParentLeft="true"  
  25.         android:layout_alignParentStart="true"  
  26.         android:layout_marginTop="74dp" />  
  27.   
  28. </RelativeLayout>  
  29.   
  30.   
  31. <?xml version="1.0" encoding="utf-8"?>  
  32. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  33.     package="com.example.liuyazhou.mythirdapplication">  
  34.   
  35.     <uses-permission android:name="android.permission.INTERNET" />  
  36.   
  37.     <application  
  38.         android:allowBackup="true"  
  39.         android:icon="@mipmap/ic_launcher"  
  40.         android:label="@string/app_name"  
  41.         android:supportsRtl="true"  
  42.         android:theme="@style/AppTheme">  
  43.         <activity android:name=".MainActivity">  
  44.             <intent-filter>  
  45.                 <action android:name="android.intent.action.MAIN" />  
  46.   
  47.                 <category android:name="android.intent.category.LAUNCHER" />  
  48.             </intent-filter>  
  49.         </activity>  
  50.     </application>  
  51.   
  52. </manifest>  

[plain]  view plain  copy
  1. {//AllWeather //返回的JSON数据  
  2.     "reason":"successed!",  
  3.     "result":{ //Result  
  4.         "data":{ //Data  
  5.             "realtime":{ //Realtime  
  6.                 "city_code":"101010100",  
  7.                 "city_name":"北京",  
  8.                 "date":"2016-09-15",  
  9.                 "time":"15:00:00",  
  10.                 "week":4,  
  11.                 "moon":"八月十五",  
  12.                 "dataUptime":1473925023,  
  13.                 "weather":{//Weather  
  14.                     "temperature":"30",  
  15.                     "humidity":"47",  
  16.                     "info":"多云",  
  17.                     "img":"1"  
  18.                 },  
  19.                 "wind":{ //Wind  
  20.                     "direct":"西南风",  
  21.                     "power":"2级",  
  22.                     "offset":null,  
  23.                     "windspeed":null  
  24.                 }  
  25.             },  
  26.             "life":{ //Life  
  27.                 "date":"2016-9-15",  
  28.                 "info":{  //Info  
  29.                     "chuanyi":[  
  30.                         "热",  
  31.                         "天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。"  
  32.                     ],  
  33.                     "ganmao":[  
  34.                         "少发",  
  35.                         "各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。"  
  36.                     ],  
  37.                     ......................  
jar文件需要自己下载
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值