必须导入Volley包
package com.example.volley_custom_gson;
import com.android.volley.RequestQueue;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
/** * @author HD * @date 2015-11-23 * @package_name com.example.volley_custom_gson * @file_name MainActivity.java */
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RequestQueue mQueue = Volley.newRequestQueue(MainActivity.this);
GsonRequest<Weather> request = new GsonRequest<Weather>(Method.GET,
"http://www.weather.com.cn/adat/sk/101010100.html", Weather.class,
new Response.Listener<Weather>() {
@Override
public void onResponse(Weather response) {
// TODO 自动生成的方法存根
Log.i("hd", response.getWeatherinfo().getCityid()+"");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO 自动生成的方法存根
Log.i("hd", "onErrorResponse");
}
});
mQueue.add(request);
}
}
package com.example.volley_custom_gson;
import java.io.UnsupportedEncodingException;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
import com.google.gson.Gson;
/** * @author HD * @date 2015-11-23 * @package_name com.example.volley_custom_gson * @file_name GsonRequest.java * @param <T> */
public class GsonRequest<T> extends Request<T> {
private Listener<T> mListener;
private Class<T> mClazz;
private Gson mGson;
public GsonRequest(int method, String url, ErrorListener listener) {
super(method, url, listener);
// TODO 自动生成的构造函数存根
}
public GsonRequest(int method, String url, Class clazz,
Listener<T> listener, ErrorListener errlistener) {
super(method, url, errlistener);
mGson = new Gson();
mClazz = clazz;
mListener = listener;
// TODO 自动生成的构造函数存根
}
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
// TODO 自动生成的方法存根
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(mGson.fromJson(jsonString, mClazz),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
// TODO 自动生成的 catch 块
return Response.error(new ParseError());
}
}
@Override
protected void deliverResponse(T response) {
// TODO 自动生成的方法存根
mListener.onResponse(response);
}
}
package com.example.volley_custom_gson;
/** * @author HD * @date 2015-11-23 * @package_name com.example.volley_custom_gson * @file_name Weather.java */
public class Weather {
public Weatherinfo weatherinfo;
public Weatherinfo getWeatherinfo() {
return weatherinfo;
}
public void setWeatherinfo(Weatherinfo weatherinfo) {
this.weatherinfo = weatherinfo;
}
}
package com.example.volley_custom_gson;
/** * @author HD * @date 2015-11-23 * @package_name com.example.volley_custom_gson * @file_name Weatherinfo.java */
public class Weatherinfo {
public String city;
public int cityid;
public int temp;
public String WD;
public String WS;
public String SD;
public String WSE;
public String time;
public String isRadar;
public String Radar;
public String njd;
public String qy;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getCityid() {
return cityid;
}
public void setCityid(int cityid) {
this.cityid = cityid;
}
public int getTemp() {
return temp;
}
public void setTemp(int temp) {
this.temp = temp;
}
public String getWD() {
return WD;
}
public void setWD(String wD) {
WD = wD;
}
public String getWS() {
return WS;
}
public void setWS(String wS) {
WS = wS;
}
public String getSD() {
return SD;
}
public void setSD(String sD) {
SD = sD;
}
public String getWSE() {
return WSE;
}
public void setWSE(String wSE) {
WSE = wSE;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getIsRadar() {
return isRadar;
}
public void setIsRadar(String isRadar) {
this.isRadar = isRadar;
}
public String getRadar() {
return Radar;
}
public void setRadar(String radar) {
Radar = radar;
}
public String getNjd() {
return njd;
}
public void setNjd(String njd) {
this.njd = njd;
}
public String getQy() {
return qy;
}
public void setQy(String qy) {
this.qy = qy;
}
}
/*{"weatherinfo":{"city":"北京","cityid":"101010100","temp":"9","WD":"西南风","WS":"2级", * "SD":"24%","WSE":"2","time":"10:30","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB","njd":"暂无实况", * "qy":"1015"}}*/
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.volley_custom_gson.MainActivity" >
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.volley_custom_gson" android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>