用andtoid studio获取天气数据并解析适配

1、申请拿到数据
  可以用“聚合数据”

2、在android studio中导入需要的jar包
  复制—>app—>libs—>粘贴—>右击—>Add As Library……—>选择model—>ok
  此项目所用的jar包建议:
    fastjson
    xUtils(访问部分可用异步任务jar包代替)

3、网络权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>

4、获取数据

  4-1:携带参数

RequestParams params = new RequestParams();
params.addBodyParameter("key", "你的key值");
params.addBodyParameter("cityname", "城市名");

  4-2:获取

 1 HttpUtils http = new HttpUtils();
 2 http.send(HttpRequest.HttpMethod.POST, "请求的网址", params, new RequestCallBack<String>() {
 3 @Override
 4 public void onSuccess(ResponseInfo<String> responseInfo) {
 5 String info = responseInfo.result;
 6 }
 7
 8 @Override
 9 public void onFailure(HttpException e, String s) {
10 Toast.makeText(MainActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show();
11 }
12 });

5、解析数据

  获取到的数据默认是json数据,用android studio的gsonformat一键生成实体类,再用parcelable序列化它(想要观看效果最佳 ,也可以重写toString方法)
    WeatherInfo就是自动生成的实体类
    WeatherInfo wea = JSON.parseObject(info, WeatherInfo.class);
    String weather = new WeatherInfo();

  1 package test.firefly.com.hehe;
  2
  3 import android.os.Parcel;
  4 import android.os.Parcelable;
  5
  6 import java.util.ArrayList;
  7 import java.util.List;
  8
  9 /**
 10  * 作者:pengdan on 2016/5/22.
 11  * 邮箱:[email protected]
 12  */
 13 public class WeatherInfo implements Parcelable {
 14     @Override
 15     public String toString() {
 16         return "WeatherInfo{" +
 17                 "reason=‘" + reason + ‘\‘‘ +
 18                 ", result=" + result +
 19                 ", error_code=" + error_code +
 20                 ‘}‘;
 21     }
 22
 23     /**
 24      * reason : successed!
 25      * result : {"data":{"realtime":{"wind":{"windspeed":"14.0","direct":"北风","power":"2级","offset":null},"time":"19:00:00","weather":{"humidity":"85","img":"2","info":"阴","temperature":"19"},"dataUptime":1463917984,"date":"2016-05-22","city_code":"101250105","city_name":"望城","week":0,"moon":"四月十六"},"life":{"date":"2016-5-22","info":{"kongtiao":["较少开启","您将感到很舒适,一般不需要开启空调。"],"yundong":["较不宜","有降水,推荐您在室内进行健身休闲运动;若坚持户外运动,须注意携带雨具并注意避雨防滑。"],"ziwaixian":["最弱","属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"],"ganmao":["较易发","天气较凉,较易发生感冒,请适当增加衣服。体质较弱的朋友尤其应该注意防护。"],"xiche":["不宜","不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。"],"wuran":["良","气象条件有利于空气污染物稀释、扩散和清除,可在室外正常活动。"],"chuanyi":["较舒适","建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。"]}},"weather":[{"date":"2016-05-22","info":{"night":["2","阴","18","无持续风向","微风","19:16"],"day":["1","多云","23","北风","微风","05:34"]},"week":"日","nongli":"四月十六"},{"date":"2016-05-23","info":{"dawn":["2","阴","18","无持续风向","微风","19:16"],"night":["2","阴","17","无持续风向","微风","19:16"],"day":["3","阵雨","22","西北风","微风","05:34"]},"week":"一","nongli":"四月十七"},{"date":"2016-05-24","info":{"dawn":["2","阴","17","无持续风向","微风","19:16"],"night":["1","多云","19","南风","微风","19:17"],"day":["1","多云","27","无持续风向","微风","05:33"]},"week":"二","nongli":"四月十八"},{"date":"2016-05-25","info":{"dawn":["1","多云","19","南风","微风","19:17"],"night":["8","中雨","20","南风","微风","19:17"],"day":["3","阵雨","27","南风","微风","05:33"]},"week":"三","nongli":"四月十九"},{"date":"2016-05-26","info":{"dawn":["8","中雨","20","南风","微风","19:17"],"night":["7","小雨","18","北风","3-4 级","19:18"],"day":["8","中雨","25","北风","3-4 级","05:33"]},"week":"四","nongli":"四月二十"},{"date":"2016-05-27","info":{"night":["3","阵雨","16","西北风","微风","19:30"],"day":["3","阵雨","20","西北风","微风","07:30"]},"week":"五","nongli":"四月廿一"},{"date":"2016-05-28","info":{"night":["1","多云","17","西北风","微风","19:30"],"day":["3","阵雨","23","西北风","微风","07:30"]},"week":"六","nongli":"四月廿二"}],"pm25":{"key":"","show_desc":0,"pm25":{"curPm":"","pm25":"","pm10":"","level":null,"quality":null,"des":null},"dateTime":"2016年05月22日19时","cityName":"望城"},"date":null,"isForeign":0}}
 26      * error_code : 0
 27      */
 28
 29     private String reason;
 30     /**
 31      * data : {"realtime":{"wind":{"windspeed":"14.0","direct":"北风","power":"2级","offset":null},"time":"19:00:00","weather":{"humidity":"85","img":"2","info":"阴","temperature":"19"},"dataUptime":1463917984,"date":"2016-05-22","city_code":"101250105","city_name":"望城","week":0,"moon":"四月十六"},"life":{"date":"2016-5-22","info":{"kongtiao":["较少开启","您将感到很舒适,一般不需要开启空调。"],"yundong":["较不宜","有降水,推荐您在室内进行健身休闲运动;若坚持户外运动,须注意携带雨具并注意避雨防滑。"],"ziwaixian":["最弱","属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"],"ganmao":["较易发","天气较凉,较易发生感冒,请适当增加衣服。体质较弱的朋友尤其应该注意防护。"],"xiche":["不宜","不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。"],"wuran":["良","气象条件有利于空气污染物稀释、扩散和清除,可在室外正常活动。"],"chuanyi":["较舒适","建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。"]}},"weather":[{"date":"2016-05-22","info":{"night":["2","阴","18","无持续风向","微风","19:16"],"day":["1","多云","23","北风","微风","05:34"]},"week":"日","nongli":"四月十六"},{"date":"2016-05-23","info":{"dawn":["2","阴","18","无持续风向","微风","19:16"],"night":["2","阴","17","无持续风向","微风","19:16"],"day":["3","阵雨","22","西北风","微风","05:34"]},"week":"一","nongli":"四月十七"},{"date":"2016-05-24","info":{"dawn":["2","阴","17","无持续风向","微风","19:16"],"night":["1","多云","19","南风","微风","19:17"],"day":["1","多云","27","无持续风向","微风","05:33"]},"week":"二","nongli":"四月十八"},{"date":"2016-05-25","info":{"dawn":["1","多云","19","南风","微风","19:17"],"night":["8","中雨","20","南风","微风","19:17"],"day":["3","阵雨","27","南风","微风","05:33"]},"week":"三","nongli":"四月十九"},{"date":"2016-05-26","info":{"dawn":["8","中雨","20","南风","微风","19:17"],"night":["7","小雨","18","北风","3-4 级","19:18"],"day":["8","中雨","25","北风","3-4 级","05:33"]},"week":"四","nongli":"四月二十"},{"date":"2016-05-27","info":{"night":["3","阵雨","16","西北风","微风","19:30"],"day":["3","阵雨","20","西北风","微风","07:30"]},"week":"五","nongli":"四月廿一"},{"date":"2016-05-28","info":{"night":["1","多云","17","西北风","微风","19:30"],"day":["3","阵雨","23","西北风","微风","07:30"]},"week":"六","nongli":"四月廿二"}],"pm25":{"key":"","show_desc":0,"pm25":{"curPm":"","pm25":"","pm10":"","level":null,"quality":null,"des":null},"dateTime":"2016年05月22日19时","cityName":"望城"},"date":null,"isForeign":0}
 32      */
 33
 34     private ResultBean result;
 35     private int error_code;
 36
 37     public String getReason() {
 38         return reason;
 39     }
 40
 41     public void setReason(String reason) {
 42         this.reason = reason;
 43     }
 44
 45     public ResultBean getResult() {
 46         return result;
 47     }
 48
 49     public void setResult(ResultBean result) {
 50         this.result = result;
 51     }
 52
 53     public int getError_code() {
 54         return error_code;
 55     }
 56
 57     public void setError_code(int error_code) {
 58         this.error_code = error_code;
 59     }
 60
 61     public static class ResultBean implements Parcelable {
 62         @Override
 63         public String toString() {
 64             return "ResultBean{" +
 65                     "data=" + data +
 66                     ‘}‘;
 67         }
 68
 69         /**
 70          * realtime : {"wind":{"windspeed":"14.0","direct":"北风","power":"2级","offset":null},"time":"19:00:00","weather":{"humidity":"85","img":"2","info":"阴","temperature":"19"},"dataUptime":1463917984,"date":"2016-05-22","city_code":"101250105","city_name":"望城","week":0,"moon":"四月十六"}
 71          * life : {"date":"2016-5-22","info":{"kongtiao":["较少开启","您将感到很舒适,一般不需要开启空调。"],"yundong":["较不宜","有降水,推荐您在室内进行健身休闲运动;若坚持户外运动,须注意携带雨具并注意避雨防滑。"],"ziwaixian":["最弱","属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"],"ganmao":["较易发","天气较凉,较易发生感冒,请适当增加衣服。体质较弱的朋友尤其应该注意防护。"],"xiche":["不宜","不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。"],"wuran":["良","气象条件有利于空气污染物稀释、扩散和清除,可在室外正常活动。"],"chuanyi":["较舒适","建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。"]}}
 72          * weather : [{"date":"2016-05-22","info":{"night":["2","阴","18","无持续风向","微风","19:16"],"day":["1","多云","23","北风","微风","05:34"]},"week":"日","nongli":"四月十六"},{"date":"2016-05-23","info":{"dawn":["2","阴","18","无持续风向","微风","19:16"],"night":["2","阴","17","无持续风向","微风","19:16"],"day":["3","阵雨","22","西北风","微风","05:34"]},"week":"一","nongli":"四月十七"},{"date":"2016-05-24","info":{"dawn":["2","阴","17","无持续风向","微风","19:16"],"night":["1","多云","19","南风","微风","19:17"],"day":["1","多云","27","无持续风向","微风","05:33"]},"week":"二","nongli":"四月十八"},{"date":"2016-05-25","info":{"dawn":["1","多云","19","南风","微风","19:17"],"night":["8","中雨","20","南风","微风","19:17"],"day":["3","阵雨","27","南风","微风","05:33"]},"week":"三","nongli":"四月十九"},{"date":"2016-05-26","info":{"dawn":["8","中雨","20","南风","微风","19:17"],"night":["7","小雨","18","北风","3-4 级","19:18"],"day":["8","中雨","25","北风","3-4 级","05:33"]},"week":"四","nongli":"四月二十"},{"date":"2016-05-27","info":{"night":["3","阵雨","16","西北风","微风","19:30"],"day":["3","阵雨","20","西北风","微风","07:30"]},"week":"五","nongli":"四月廿一"},{"date":"2016-05-28","info":{"night":["1","多云","17","西北风","微风","19:30"],"day":["3","阵雨","23","西北风","微风","07:30"]},"week":"六","nongli":"四月廿二"}]
 73          * pm25 : {"key":"","show_desc":0,"pm25":{"curPm":"","pm25":"","pm10":"","level":null,"quality":null,"des":null},"dateTime":"2016年05月22日19时","cityName":"望城"}
 74          * date : null
 75          * isForeign : 0
 76          */
 77
 78         private DataBean data;
 79
 80         public DataBean getData() {
 81             return data;
 82         }
 83
 84         public void setData(DataBean data) {
 85             this.data = data;
 86         }
 87
 88         public static class DataBean implements Parcelable {
 89             @Override
 90             public String toString() {
 91                 return "DataBean{" +
 92                         "realtime=" + realtime +
 93                         ", life=" + life +
 94                         ", date=" + date +
 95                         ", isForeign=" + isForeign +
 96                         ", weather=" + weather +
 97                         ‘}‘;
 98             }
 99
100             /**
101              * wind : {"windspeed":"14.0","direct":"北风","power":"2级","offset":null}
102              * time : 19:00:00
103              * weather : {"humidity":"85","img":"2","info":"阴","temperature":"19"}
104              * dataUptime : 1463917984
105              * date : 2016-05-22
106              * city_code : 101250105
107              * city_name : 望城
108              * week : 0
109              * moon : 四月十六
110              */
111
112             private RealtimeBean realtime;
113             /**
114              * date : 2016-5-22
115              * info : {"kongtiao":["较少开启","您将感到很舒适,一般不需要开启空调。"],"yundong":["较不宜","有降水,推荐您在室内进行健身休闲运动;若坚持户外运动,须注意携带雨具并注意避雨防滑。"],"ziwaixian":["最弱","属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"],"ganmao":["较易发","天气较凉,较易发生感冒,请适当增加衣服。体质较弱的朋友尤其应该注意防护。"],"xiche":["不宜","不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。"],"wuran":["良","气象条件有利于空气污染物稀释、扩散和清除,可在室外正常活动。"],"chuanyi":["较舒适","建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。"]}
116              */
117
118             private LifeBean life;
119             private Object date;
120             private int isForeign;
121             /**
122              * date : 2016-05-22
123              * info : {"night":["2","阴","18","无持续风向","微风","19:16"],"day":["1","多云","23","北风","微风","05:34"]}
124              * week : 日
125              * nongli : 四月十六
126              */
127
128             private List<WeatherBean> weather;
129
130             public RealtimeBean getRealtime() {
131                 return realtime;
132             }
133
134             public void setRealtime(RealtimeBean realtime) {
135                 this.realtime = realtime;
136             }
137
138             public LifeBean getLife() {
139                 return life;
140             }
141
142             public void setLife(LifeBean life) {
143                 this.life = life;
144             }
145
146             public Object getDate() {
147                 return date;
148             }
149
150             public void setDate(Object date) {
151                 this.date = date;
152             }
153
154             public int getIsForeign() {
155                 return isForeign;
156             }
157
158             public void setIsForeign(int isForeign) {
159                 this.isForeign = isForeign;
160             }
161
162             public List<WeatherBean> getWeather() {
163                 return weather;
164             }
165
166             public void setWeather(List<WeatherBean> weather) {
167                 this.weather = weather;
168             }
169
170             public static class RealtimeBean implements Parcelable {
171                 @Override
172                 public String toString() {
173                     return "RealtimeBean{" +
174                             "wind=" + wind +
175                             ", time=‘" + time + ‘\‘‘ +
176                             ", weather=" + weather +
177                             ", dataUptime=" + dataUptime +
178                             ", date=‘" + date + ‘\‘‘ +
179                             ", city_code=‘" + city_code + ‘\‘‘ +
180                             ", city_name=‘" + city_name + ‘\‘‘ +
181                             ", week=" + week +
182                             ", moon=‘" + moon + ‘\‘‘ +
183                             ‘}‘;
184                 }
185
186                 /**
187                  * windspeed : 14.0
188                  * direct : 北风
189                  * power : 2级
190                  * offset : null
191                  */
192
193                 private WindBean wind;
194                 private String time;
195                 /**
196                  * humidity : 85
197                  * img : 2
198                  * info : 阴
199                  * temperature : 19
200                  */
201
202                 private WeatherBean weather;
203                 private int dataUptime;
204                 private String date;
205                 private String city_code;
206                 private String city_name;
207                 private int week;
208                 private String moon;
209
210                 public WindBean getWind() {
211                     return wind;
212                 }
213
214                 public void setWind(WindBean wind) {
215                     this.wind = wind;
216                 }
217
218                 public String getTime() {
219                     return time;
220                 }
221
222                 public void setTime(String time) {
223                     this.time = time;
224                 }
225
226                 public WeatherBean getWeather() {
227                     return weather;
228                 }
229
230                 public void setWeather(WeatherBean weather) {
231                     this.weather = weather;
232                 }
233
234                 public int getDataUptime() {
235                     return dataUptime;
236                 }
237
238                 public void setDataUptime(int dataUptime) {
239                     this.dataUptime = dataUptime;
240                 }
241
242                 public String getDate() {
243                     return date;
244                 }
245
246                 public void setDate(String date) {
247                     this.date = date;
248                 }
249
250                 public String getCity_code() {
251                     return city_code;
252                 }
253
254                 public void setCity_code(String city_code) {
255                     this.city_code = city_code;
256                 }
257
258                 public String getCity_name() {
259                     return city_name;
260                 }
261
262                 public void setCity_name(String city_name) {
263                     this.city_name = city_name;
264                 }
265
266                 public int getWeek() {
267                     return week;
268                 }
269
270                 public void setWeek(int week) {
271                     this.week = week;
272                 }
273
274                 public String getMoon() {
275                     return moon;
276                 }
277
278                 public void setMoon(String moon) {
279                     this.moon = moon;
280                 }
281
282                 public static class WindBean implements Parcelable {
283                     @Override
284                     public String toString() {
285                         return "WindBean{" +
286                                 "windspeed=‘" + windspeed + ‘\‘‘ +
287                                 ", direct=‘" + direct + ‘\‘‘ +
288                                 ", power=‘" + power + ‘\‘‘ +
289                                 ", offset=" + offset +
290                                 ‘}‘;
291                     }
292
293                     private String windspeed;
294                     private String direct;
295                     private String power;
296                     private Object offset;
297
298                     public String getWindspeed() {
299                         return windspeed;
300                     }
301
302                     public void setWindspeed(String windspeed) {
303                         this.windspeed = windspeed;
304                     }
305
306                     public String getDirect() {
307                         return direct;
308                     }
309
310                     public void setDirect(String direct) {
311                         this.direct = direct;
312                     }
313
314                     public String getPower() {
315                         return power;
316                     }
317
318                     public void setPower(String power) {
319                         this.power = power;
320                     }
321
322                     public Object getOffset() {
323                         return offset;
324                     }
325
326                     public void setOffset(Object offset) {
327                         this.offset = offset;
328                     }
329
330                     @Override
331                     public int describeContents() {
332                         return 0;
333                     }
334
335                     @Override
336                     public void writeToParcel(Parcel dest, int flags) {
337                         dest.writeString(this.windspeed);
338                         dest.writeString(this.direct);
339                         dest.writeString(this.power);
340                         dest.writeParcelable((Parcelable) this.offset, flags);
341                     }
342
343                     public WindBean() {
344                     }
345
346                     protected WindBean(Parcel in) {
347                         this.windspeed = in.readString();
348                         this.direct = in.readString();
349                         this.power = in.readString();
350                         this.offset = in.readParcelable(Object.class.getClassLoader());
351                     }
352
353                     public static final Creator<WindBean> CREATOR = new Creator<WindBean>() {
354                         @Override
355                         public WindBean createFromParcel(Parcel source) {
356                             return new WindBean(source);
357                         }
358
359                         @Override
360                         public WindBean[] newArray(int size) {
361                             return new WindBean[size];
362                         }
363                     };
364                 }
365
366                 public static class WeatherBean implements Parcelable {
367                     @Override
368                     public String toString() {
369                         return "WeatherBean{" +
370                                 "humidity=‘" + humidity + ‘\‘‘ +
371                                 ", img=‘" + img + ‘\‘‘ +
372                                 ", info=‘" + info + ‘\‘‘ +
373                                 ", temperature=‘" + temperature + ‘\‘‘ +
374                                 ‘}‘;
375                     }
376
377                     private String humidity;
378                     private String img;
379                     private String info;
380                     private String temperature;
381
382                     public String getHumidity() {
383                         return humidity;
384                     }
385
386                     public void setHumidity(String humidity) {
387                         this.humidity = humidity;
388                     }
389
390                     public String getImg() {
391                         return img;
392                     }
393
394                     public void setImg(String img) {
395                         this.img = img;
396                     }
397
398                     public String getInfo() {
399                         return info;
400                     }
401
402                     public void setInfo(String info) {
403                         this.info = info;
404                     }
405
406                     public String getTemperature() {
407                         return temperature;
408                     }
409
410                     public void setTemperature(String temperature) {
411                         this.temperature = temperature;
412                     }
413
414                     @Override
415                     public int describeContents() {
416                         return 0;
417                     }
418
419                     @Override
420                     public void writeToParcel(Parcel dest, int flags) {
421                         dest.writeString(this.humidity);
422                         dest.writeString(this.img);
423                         dest.writeString(this.info);
424                         dest.writeString(this.temperature);
425                     }
426
427                     public WeatherBean() {
428                     }
429
430                     protected WeatherBean(Parcel in) {
431                         this.humidity = in.readString();
432                         this.img = in.readString();
433                         this.info = in.readString();
434                         this.temperature = in.readString();
435                     }
436
437                     public static final Creator<WeatherBean> CREATOR = new Creator<WeatherBean>() {
438                         @Override
439                         public WeatherBean createFromParcel(Parcel source) {
440                             return new WeatherBean(source);
441                         }
442
443                         @Override
444                         public WeatherBean[] newArray(int size) {
445                             return new WeatherBean[size];
446                         }
447                     };
448                 }
449
450                 public RealtimeBean() {
451                 }
452
453                 @Override
454                 public int describeContents() {
455                     return 0;
456                 }
457
458                 @Override
459                 public void writeToParcel(Parcel dest, int flags) {
460                     dest.writeParcelable(this.wind, flags);
461                     dest.writeString(this.time);
462                     dest.writeParcelable(this.weather, flags);
463                     dest.writeInt(this.dataUptime);
464                     dest.writeString(this.date);
465                     dest.writeString(this.city_code);
466                     dest.writeString(this.city_name);
467                     dest.writeInt(this.week);
468                     dest.writeString(this.moon);
469                 }
470
471                 protected RealtimeBean(Parcel in) {
472                     this.wind = in.readParcelable(WindBean.class.getClassLoader());
473                     this.time = in.readString();
474                     this.weather = in.readParcelable(WeatherBean.class.getClassLoader());
475                     this.dataUptime = in.readInt();
476                     this.date = in.readString();
477                     this.city_code = in.readString();
478                     this.city_name = in.readString();
479                     this.week = in.readInt();
480                     this.moon = in.readString();
481                 }
482
483                 public static final Creator<RealtimeBean> CREATOR = new Creator<RealtimeBean>() {
484                     @Override
485                     public RealtimeBean createFromParcel(Parcel source) {
486                         return new RealtimeBean(source);
487                     }
488
489                     @Override
490                     public RealtimeBean[] newArray(int size) {
491                         return new RealtimeBean[size];
492                     }
493                 };
494             }
495
496             public static class LifeBean implements Parcelable {
497                 @Override
498                 public String toString() {
499                     return "LifeBean{" +
500                             "date=‘" + date + ‘\‘‘ +
501                             ", info=" + info +
502                             ‘}‘;
503                 }
504
505                 private String date;
506                 private InfoBean info;
507
508                 public String getDate() {
509                     return date;
510                 }
511
512                 public void setDate(String date) {
513                     this.date = date;
514                 }
515
516                 public InfoBean getInfo() {
517                     return info;
518                 }
519
520                 public void setInfo(InfoBean info) {
521                     this.info = info;
522                 }
523
524                 public static class InfoBean implements Parcelable {
525                     @Override
526                     public String toString() {
527                         return "InfoBean{" +
528                                 "kongtiao=" + kongtiao +
529                                 ", yundong=" + yundong +
530                                 ", ziwaixian=" + ziwaixian +
531                                 ", ganmao=" + ganmao +
532                                 ", xiche=" + xiche +
533                                 ", wuran=" + wuran +
534                                 ", chuanyi=" + chuanyi +
535                                 ‘}‘;
536                     }
537
538                     private List<String> kongtiao;
539                     private List<String> yundong;
540                     private List<String> ziwaixian;
541                     private List<String> ganmao;
542                     private List<String> xiche;
543                     private List<String> wuran;
544                     private List<String> chuanyi;
545
546                     public List<String> getKongtiao() {
547                         return kongtiao;
548                     }
549
550                     public void setKongtiao(List<String> kongtiao) {
551                         this.kongtiao = kongtiao;
552                     }
553
554                     public List<String> getYundong() {
555                         return yundong;
556                     }
557
558                     public void setYundong(List<String> yundong) {
559                         this.yundong = yundong;
560                     }
561
562                     public List<String> getZiwaixian() {
563                         return ziwaixian;
564                     }
565
566                     public void setZiwaixian(List<String> ziwaixian) {
567                         this.ziwaixian = ziwaixian;
568                     }
569
570                     public List<String> getGanmao() {
571                         return ganmao;
572                     }
573
574                     public void setGanmao(List<String> ganmao) {
575                         this.ganmao = ganmao;
576                     }
577
578                     public List<String> getXiche() {
579                         return xiche;
580                     }
581
582                     public void setXiche(List<String> xiche) {
583                         this.xiche = xiche;
584                     }
585
586                     public List<String> getWuran() {
587                         return wuran;
588                     }
589
590                     public void setWuran(List<String> wuran) {
591                         this.wuran = wuran;
592                     }
593
594                     public List<String> getChuanyi() {
595                         return chuanyi;
596                     }
597
598                     public void setChuanyi(List<String> chuanyi) {
599                         this.chuanyi = chuanyi;
600                     }
601
602                     @Override
603                     public int describeContents() {
604                         return 0;
605                     }
606
607                     @Override
608                     public void writeToParcel(Parcel dest, int flags) {
609                         dest.writeStringList(this.kongtiao);
610                         dest.writeStringList(this.yundong);
611                         dest.writeStringList(this.ziwaixian);
612                         dest.writeStringList(this.ganmao);
613                         dest.writeStringList(this.xiche);
614                         dest.writeStringList(this.wuran);
615                         dest.writeStringList(this.chuanyi);
616                     }
617
618                     public InfoBean() {
619                     }
620
621                     protected InfoBean(Parcel in) {
622                         this.kongtiao = in.createStringArrayList();
623                         this.yundong = in.createStringArrayList();
624                         this.ziwaixian = in.createStringArrayList();
625                         this.ganmao = in.createStringArrayList();
626                         this.xiche = in.createStringArrayList();
627                         this.wuran = in.createStringArrayList();
628                         this.chuanyi = in.createStringArrayList();
629                     }
630
631                     public static final Creator<InfoBean> CREATOR = new Creator<InfoBean>() {
632                         @Override
633                         public InfoBean createFromParcel(Parcel source) {
634                             return new InfoBean(source);
635                         }
636
637                         @Override
638                         public InfoBean[] newArray(int size) {
639                             return new InfoBean[size];
640                         }
641                     };
642                 }
643
644                 @Override
645                 public int describeContents() {
646                     return 0;
647                 }
648
649                 @Override
650                 public void writeToParcel(Parcel dest, int flags) {
651                     dest.writeString(this.date);
652                     dest.writeParcelable(this.info, flags);
653                 }
654
655                 public LifeBean() {
656                 }
657
658                 protected LifeBean(Parcel in) {
659                     this.date = in.readString();
660                     this.info = in.readParcelable(InfoBean.class.getClassLoader());
661                 }
662
663                 public static final Creator<LifeBean> CREATOR = new Creator<LifeBean>() {
664                     @Override
665                     public LifeBean createFromParcel(Parcel source) {
666                         return new LifeBean(source);
667                     }
668
669                     @Override
670                     public LifeBean[] newArray(int size) {
671                         return new LifeBean[size];
672                     }
673                 };
674             }
675
676             public static class WeatherBean implements Parcelable {
677                 @Override
678                 public String toString() {
679                     return "WeatherBean{" +
680                             "date=‘" + date + ‘\‘‘ +
681                             ", info=" + info +
682                             ", week=‘" + week + ‘\‘‘ +
683                             ", nongli=‘" + nongli + ‘\‘‘ +
684                             ‘}‘;
685                 }
686
687                 private String date;//几月几日
688                 private InfoBean info;//信息
689                 private String week;//星期几
690                 private String nongli;//农历
691
692                 public String getDate() {
693                     return date;
694                 }
695
696                 public void setDate(String date) {
697                     this.date = date;
698                 }
699
700                 public InfoBean getInfo() {
701                     return info;
702                 }
703
704                 public void setInfo(InfoBean info) {
705                     this.info = info;
706                 }
707
708                 public String getWeek() {
709                     return week;
710                 }
711
712                 public void setWeek(String week) {
713                     this.week = week;
714                 }
715
716                 public String getNongli() {
717                     return nongli;
718                 }
719
720                 public void setNongli(String nongli) {
721                     this.nongli = nongli;
722                 }
723
724                 public static class InfoBean implements Parcelable {
725                     @Override
726                     public String toString() {
727                         return "InfoBean{" +
728                                 "night=" + night +
729                                 ", day=" + day +
730                                 ‘}‘;
731                     }
732
733                     private List<String> night;
734                     private List<String> day;
735
736                     public List<String> getNight() {
737                         return night;
738                     }
739
740                     public void setNight(List<String> night) {
741                         this.night = night;
742                     }
743
744                     public List<String> getDay() {
745                         return day;
746                     }
747
748                     public void setDay(List<String> day) {
749                         this.day = day;
750                     }
751
752                     @Override
753                     public int describeContents() {
754                         return 0;
755                     }
756
757                     @Override
758                     public void writeToParcel(Parcel dest, int flags) {
759                         dest.writeStringList(this.night);
760                         dest.writeStringList(this.day);
761                     }
762
763                     public InfoBean() {
764                     }
765
766                     protected InfoBean(Parcel in) {
767                         this.night = in.createStringArrayList();
768                         this.day = in.createStringArrayList();
769                     }
770
771                     public static final Creator<InfoBean> CREATOR = new Creator<InfoBean>() {
772                         @Override
773                         public InfoBean createFromParcel(Parcel source) {
774                             return new InfoBean(source);
775                         }
776
777                         @Override
778                         public InfoBean[] newArray(int size) {
779                             return new InfoBean[size];
780                         }
781                     };
782                 }
783
784                 @Override
785                 public int describeContents() {
786                     return 0;
787                 }
788
789                 @Override
790                 public void writeToParcel(Parcel dest, int flags) {
791                     dest.writeString(this.date);
792                     dest.writeParcelable((Parcelable) this.info, flags);
793                     dest.writeString(this.week);
794                     dest.writeString(this.nongli);
795                 }
796
797                 public WeatherBean() {
798                 }
799
800                 protected WeatherBean(Parcel in) {
801                     this.date = in.readString();
802                     this.info = in.readParcelable(InfoBean.class.getClassLoader());
803                     this.week = in.readString();
804                     this.nongli = in.readString();
805                 }
806
807                 public static final Creator<WeatherBean> CREATOR = new Creator<WeatherBean>() {
808                     @Override
809                     public WeatherBean createFromParcel(Parcel source) {
810                         return new WeatherBean(source);
811                     }
812
813                     @Override
814                     public WeatherBean[] newArray(int size) {
815                         return new WeatherBean[size];
816                     }
817                 };
818             }
819
820             public DataBean() {
821             }
822
823             @Override
824             public int describeContents() {
825                 return 0;
826             }
827
828             @Override
829             public void writeToParcel(Parcel dest, int flags) {
830                 dest.writeParcelable(this.realtime, flags);
831                 dest.writeParcelable(this.life, flags);
832                 dest.writeParcelable((Parcelable) this.date, flags);
833                 dest.writeInt(this.isForeign);
834                 dest.writeList(this.weather);
835             }
836
837             protected DataBean(Parcel in) {
838                 this.realtime = in.readParcelable(RealtimeBean.class.getClassLoader());
839                 this.life = in.readParcelable(LifeBean.class.getClassLoader());
840                 this.date = in.readParcelable(Object.class.getClassLoader());
841                 this.isForeign = in.readInt();
842                 this.weather = new ArrayList<WeatherBean>();
843                 in.readList(this.weather, WeatherBean.class.getClassLoader());
844             }
845
846             public static final Creator<DataBean> CREATOR = new Creator<DataBean>() {
847                 @Override
848                 public DataBean createFromParcel(Parcel source) {
849                     return new DataBean(source);
850                 }
851
852                 @Override
853                 public DataBean[] newArray(int size) {
854                     return new DataBean[size];
855                 }
856             };
857         }
858
859         public ResultBean() {
860         }
861
862         @Override
863         public int describeContents() {
864             return 0;
865         }
866
867         @Override
868         public void writeToParcel(Parcel dest, int flags) {
869             dest.writeParcelable(this.data, flags);
870         }
871
872         protected ResultBean(Parcel in) {
873             this.data = in.readParcelable(DataBean.class.getClassLoader());
874         }
875
876         public static final Creator<ResultBean> CREATOR = new Creator<ResultBean>() {
877             @Override
878             public ResultBean createFromParcel(Parcel source) {
879                 return new ResultBean(source);
880             }
881
882             @Override
883             public ResultBean[] newArray(int size) {
884                 return new ResultBean[size];
885             }
886         };
887     }
888
889     public WeatherInfo() {
890     }
891
892     @Override
893     public int describeContents() {
894         return 0;
895     }
896
897     @Override
898     public void writeToParcel(Parcel dest, int flags) {
899         dest.writeString(this.reason);
900         dest.writeParcelable(this.result, flags);
901         dest.writeInt(this.error_code);
902     }
903
904     protected WeatherInfo(Parcel in) {
905         this.reason = in.readString();
906         this.result = in.readParcelable(ResultBean.class.getClassLoader());
907         this.error_code = in.readInt();
908     }
909
910     public static final Creator<WeatherInfo> CREATOR = new Creator<WeatherInfo>() {
911         @Override
912         public WeatherInfo createFromParcel(Parcel source) {
913             return new WeatherInfo(source);
914         }
915
916         @Override
917         public WeatherInfo[] newArray(int size) {
918             return new WeatherInfo[size];
919         }
920     };
921 }

WeatherInfo

6、适配数据
   新建一个Adapter,用ViewHolder优化它
    新建item_layout,给数据一个模板

 1 package test.firefly.com.hehe;
 2
 3 import android.content.Context;
 4 import android.view.LayoutInflater;
 5 import android.view.View;
 6 import android.view.ViewGroup;
 7 import android.widget.BaseAdapter;
 8 import android.widget.TextView;
 9
10 import java.util.List;
11
12 /**
13  * 作者:pengdan on 2016/5/23.
14  * 邮箱:[email protected]
15  */
16 public class WeatherAdaper extends BaseAdapter {
17
18     private List<WeatherInfo.ResultBean.DataBean.WeatherBean> list;
19
20     private Context ctx;
21
22     private LayoutInflater mInflater;
23
24
25     public WeatherAdaper(Context context, List<WeatherInfo.ResultBean.DataBean.WeatherBean> list) {
26         ctx = context;
27         this.list = list;
28         mInflater = LayoutInflater.from(context);
29     }
30
31     @Override
32     public int getCount() {
33         return list.size();
34     }
35
36     @Override
37     public Object getItem(int position) {
38         return list.get(position);
39     }
40
41     @Override
42     public long getItemId(int position) {
43         return position;
44     }
45
46     @Override
47     public View getView(int position, View convertView, ViewGroup parent) {
48
49         WeatherInfo.ResultBean.DataBean.WeatherBean entity = list.get(position);
50
51         ViewHolder viewHolder = null;
52         if (convertView == null) {
53                 convertView = mInflater.inflate( R.layout.item_weather, null);
54
55             viewHolder = new ViewHolder();
56             viewHolder.tv_name = (TextView) convertView
57                     .findViewById(R.id.tv_name);
58             viewHolder.tv_temp = (TextView) convertView
59                     .findViewById(R.id.tv_temp);
60             viewHolder.tv_day = (TextView) convertView
61                     .findViewById(R.id.tv_day);
62             viewHolder.tv_date = (TextView) convertView
63                     .findViewById(R.id.tv_date);
64             convertView.setTag(viewHolder);
65         } else {
66             viewHolder = (ViewHolder) convertView.getTag();
67         }
68
69         viewHolder.tv_name.setText(entity.getInfo().getDay().get(1));
70         viewHolder.tv_temp.setText(entity.getInfo().getDay().get(2)+"℃");
71         viewHolder.tv_day.setText("星期"+entity.getWeek());
72         viewHolder.tv_date.setText(entity.getDate());
73         return convertView;
74     }
75
76
77     static class ViewHolder {
78         public TextView tv_date;
79         public TextView tv_name;
80         public TextView tv_temp;
81         public TextView tv_day;
82     }
83
84 }

WeatherAdaper

7、在Activity中使用它
 1 list= wea.getResult().getData().getWeather(); 2 adapter = new WeatherAdaper(MainActivity.this, list); 3 lv.setAdapter(adapter);

文件下载链接:http://pan.baidu.com/s/1o7QcrP4

时间: 2024-10-14 04:24:26

用andtoid studio获取天气数据并解析适配的相关文章

Python 获取接口数据,解析JSON,写入文件

Python 获取接口数据,解析JSON,写入文件 用于练手的例子,从国家气象局接口上获取JSON数据,将它写入文件中,并解析JSON: 总的来说,在代码量上,python代码量要比java少很多.而且python看起来更直观一些: 以下是代码: import types import urllib2 import json duan ="--------------------------" #在控制台断行区别的 #利用urllib2获取网络数据 def registerUrl():

android通过httpClient请求获取JSON数据并且解析

android通过httpClient请求获取JSON数据并且解析:http://www.cnblogs.com/gzggyy/archive/2013/05/08/3066288.html Android--使用Http向服务器发送请求并取得返回结果,下载图片:http://www.2cto.com/kf/201307/229489.html Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据):http://blog.csdn.net/he

C# 解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市

百度天气 接口地址:http://api.map.baidu.com/telematics/v3/weather?location=上海&output=json&ak=hXWAgbsCC9UTkBO5V5Qg1WZ9,其中ak是密钥,自行去申请即可,便于大家测试,楼主就公布并了自己的Key,这样可以直接获取到数据. 获取到的数据是这样的: {"error":0,"status":"success","date"

网络获取json数据并解析

1.升级流程分析 2.Android通过URL获取网络资源 JSONObject来解析Json字符串 //创建URL对象 //本机地址用localhost, 但是如果用模拟器加载本机的地址时,可以用ip(10.0.2.2)来替换 URL url=new URL("http://10.0.2.2:8080/update.json"); //第一步是creat HttpURLConnection conn = (HttpURLConnection) url.openConnection()

android中webservce获取soapObject数据的解析问题

问题描述 最近在尝试做个开发,用到网络通讯这块.有几个问题: 1android客户端已经得到weibservice发挥的soapObject对象  但是怎么解析成我需要的数据类型? 通过网页得到的xml文件 <?xml version="1.0" encoding="utf-8" ?> - <user_info> <USER_NO>adminbb</USER_NO> <USER_NAME>admin<

Java从网络中请求获取JSon数据以及解析JSON数据----(自创,请注明)

Json数据是比较常用的数据类型解析,优点就不多说啦.来看看方法: public static JSONObject getJsonObject(String url) { JSONObject jsonObject = null; try { HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); HttpParams httpParams = httpClient.getPara

angular框架下的跨域问题(获取天气数据)

1.新浪天气:http://php.weather.sina.com.cn/iframe/index/w_cl.php?code=js&day=0&city=&dfc=1&charset=utf-8 2.中国天气网:1)http://www.weather.com.cn/data/sk/101010100.html 2)http://www.weather.com.cn/data/cityinfo/101010100.html 3.中国万年历:1)http://wthrcd

获取重要气象台数据 并解析

项目要用到json解析,写了一个demo,从中央气象台获取json数据并解析到本地.记录一下. 联网获取数据逻辑: private void loadData(){ Log.d("wlj", "loadData >>>>> "); HttpParams params=new BasicHttpParams(); //设置连接超时或响应超时 HttpConnectionParams.setConnectionTimeout(params,

Java通过webservice接口获取天气信息

通过SOAP请求的方式获取天气信息并解析返回的XML文件. 参考: http://www.webxml.com.cn/WebServices/WeatherWS.asmx import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.URL; import java.net.URLConnection; import javax.xml.par