package com.example.liuyazhou.mythirdapplication;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import weather.Data;
import weather.Life;
import weather.Info;
import weather.Realtime;
import weather.Result;
import weather.ALLWeather;
public class MainActivity extends Activity {
private Button button;
private String urlJsonWeather = "http://op.juhe.cn/onebox/weather/query?cityname=%E5%8C%97%E4%BA%AC&dtype=json&key=******1b7942cbdcff19a08001";
private static String TAG = MainActivity.class.getSimpleName();/key需要自己申请
private String jsonResponse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
funWeatherGsonRequest();
}
});
}
Volley 三 http://blog.csdn.net/guolin_blog/article/details/17612763
public void funWeatherGsonRequest() {
RequestQueue mQueue = Volley.newRequestQueue(MainActivity.this);
GsonRequest gsonRequest = new GsonRequest(
urlJsonWeather, ALLWeather.class,
new Response.Listener() {
@Override
public void onResponse(ALLWeather allWeather) {
Log.i(TAG, allWeather.toString());
Log.i(TAG, "reason is: " + allWeather.getReason());
Result result = allWeather.getResult();
Log.i(TAG, "result is: "+result.toString());
Data data = result.getData();
Log.i(TAG, "data is: "+data.toString());
Realtime realtime =data.getRealtime();
Log.i(TAG, "realtime is: "+realtime.toString());
Log.i(TAG, "realtime.getCity_code(): "+ realtime.getCity_code());
Life life = data.getLife();
Log.i(TAG, "life is: "+life.toString());
Log.i(TAG, " life.getDate(): "+ life.getDate());
Info lifeInfo = life.getInfo();///这里对象名是lifeInfo是允许的
Log.i(TAG, "lifeInfo is: "+lifeInfo.toString());
// Log.i(TAG, "chuanyi is: "+lifeInfo.toString());
Log.i(TAG, "chuanyi is: " + lifeInfo.getChuanyi()[0]+","+lifeInfo.getChuanyi()[1]);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", error.getMessage(), error);
}
});
mQueue.add(gsonRequest);
}
}
package weather;
/**
* Created by liuyazhou on 2016/9/15.
*/
public class ALLWeather {
private String reason;
private Result result;
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public Result getResult() {
return result;
}
public void setResult(Result result) {
this.result = result;
}
}
package weather;
/**
* Created by liuyazhou on 2016/9/15.
*/
public class Result {
private Data data;
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
}
package weather;
/**
* Created by liuyazhou on 2016/9/15.
*/
public class Data {
private Realtime realtime;
private Life life;
public Realtime getRealtime() {
return realtime;
}
public void setRealtime(Realtime realtime) {
this.realtime = realtime;
}
public Life getLife() {
return life;
}
public void setLife(Life life) {
this.life = life;
}
}
package weather;
/**
* Created by liuyazhou on 2016/9/15.
*/
public class Realtime {
// "city_code":"101010100",
// "city_name":"北京",
// "date":"2016-09-15",
// "time":"15:00:00",
// "week":4,
// "moon":"八月十五",
// "dataUptime":1473925023,
// private String cityCode;
private String city_code;
// public String getCityCode() { 返回null
// return cityCode;
// }
//
// public void setCityCode(String cityCode) {
// this.cityCode = cityCode;
// }
public String getCity_code() {
return city_code;
}
public void setCity_code(String city_code) {
this.city_code = city_code;
}
}
package weather;
/**
* Created by liuyazhou on 2016/9/15.
*/
public class Life {
private String date;
// private Info lifeInfo;错误的,类定义中的属性名、对象名、类名要与json里的key一致
private Info info;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public Info getInfo() {
return info;
}
public void setInfo(Info info) {
this.info = info;
}
}
package weather;
/**
* Created by liuyazhou on 2016/9/15.
*/
public class Info {
// "info":{
// "chuanyi":[
// "热",
// "天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。"
// ],
// "ganmao":[
// "少发",
// "各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。"
// ],
private String[] chuanyi;
private String[] ganmao;
public String[] getChuanyi() {
return chuanyi;
}
public void setChuanyi(String[] chuanyi) {
this.chuanyi = chuanyi;
}
public String[] getGanmao() {
return ganmao;
}
public void setGanmao(String[] ganmao) {
this.ganmao = ganmao;
}
}
package com.example.liuyazhou.mythirdapplication;
/**
* Created by liuyazhou on 2016/9/15.
*/
Volley 三 http://blog.csdn.net/guolin_blog/article/details/17612763
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;
/**
* Created by liuyazhou on 2016/9/15.
*/
public class GsonRequest extends Request {
private final Response.Listener mListener;
private Gson mGson;
private Class mClass;
public GsonRequest(int method, String url, Class clazz, Response.Listener listener,
Response.ErrorListener errorListener) {
super(method, url, errorListener);
mGson = new Gson();
mClass = clazz;
mListener = listener;
}
public GsonRequest(String url, Class clazz, Response.Listener listener,
Response.ErrorListener errorListener) {
this(Method.GET, url, clazz, listener, errorListener);
}
@Override
protected Response parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(mGson.fromJson(jsonString, mClass),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
}
}
@Override
protected void deliverResponse(T response) {
mListener.onResponse(response);
}
}
{//AllWeather //返回的JSON数据
"reason":"successed!",
"result":{ //Result
"data":{ //Data
"realtime":{ //Realtime
"city_code":"101010100",
"city_name":"北京",
"date":"2016-09-15",
"time":"15:00:00",
"week":4,
"moon":"八月十五",
"dataUptime":1473925023,
"weather":{//Weather
"temperature":"30",
"humidity":"47",
"info":"多云",
"img":"1"
},
"wind":{ //Wind
"direct":"西南风",
"power":"2级",
"offset":null,
"windspeed":null
}
},
"life":{ //Life
"date":"2016-9-15",
"info":{ //Info
"chuanyi":[
"热",
"天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。"
],
"ganmao":[
"少发",
"各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。"
],
......................
jar文件需要自己下载