import java.io.UnsupportedEncodingException; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.HashMap; import java.util.Map; import org.json.JSONException; import org.json.JSONObject; import com.android.volley.AuthFailureError; import com.android.volley.NetworkResponse; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.Request.Method; import com.android.volley.Response.ErrorListener; import com.android.volley.Response.Listener; import com.android.volley.toolbox.HttpHeaderParser; import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.itboye.sunsun.utils.DataEncryptionUtil; import android.util.Log; /** * 建造者模式创建Request * .默认请求 DEPRECATED_GET_OR_POST * @author Young * * @param <T> 请求结束后返回的对象,可以是 List<自定义类> 或 自定义类 */ public class XJsonRequest<T> extends Request<T> { private Listener<T> listener; private Map<String,String>headers=new HashMap<String, String>(); private int method=Method.DEPRECATED_GET_OR_POST; private Map<String,String>params=new HashMap<String, String>(); /** * * @param url * @param listener * @param errlistener */ public XJsonRequest(String url, XRequestListener<T> listener, ErrorListener errlistener) { super(Method.DEPRECATED_GET_OR_POST,url, errlistener); this.listener = listener; } public XJsonRequest(int method,String url, XRequestListener<T> listener, ErrorListener errlistener) { super(method, url, errlistener); this.listener = listener; } @Override protected void deliverResponse(T response) { if (null != listener) { listener.onResponse(response); } } public XJsonRequest<T>addHeader(String k,String value){ headers.put(k, value); return this; } @Override public Map<String, String> getHeaders() throws AuthFailureError { return headers; } public XJsonRequest<T>setMethod(int method){ this.method=method; return this; } @Override public int getMethod() { return method; } public XJsonRequest<T>addParam(String k,String value){ params.put(k, value); return this; } @Override protected Map<String, String> getParams() throws AuthFailureError { return params; } //该方法中gson解析可能需要调整 @Override protected Response<T> parseNetworkResponse(NetworkResponse response) { String parsed; try { parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); } catch (UnsupportedEncodingException e) { parsed = new String(response.data); } Log.v("rawResponse", parsed); if (listener != null) { Type typeClass1 = listener.getClass().getGenericSuperclass(); if (typeClass1 instanceof ParameterizedType) { Type actualType1 = ((ParameterizedType) typeClass1).getActualTypeArguments()[0]; System.out.println("type==="+actualType1);
// 下面的解析过程仅供参考,具体解析方式由服务器返回的json决定。 // Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create(); // T res = gson.fromJson(parsed, actualType1); JSONObject jsonObject; try { jsonObject = new JSONObject(parsed); String resultData = jsonObject.getString("data"); parsed = DataEncryptionUtil.decodeData(resultData); jsonObject = new JSONObject(parsed); int code = jsonObject.getInt("code"); if (code!=0) { return null; } resultData = jsonObject.getString("data"); T res = new Gson().fromJson(resultData, actualType1); if (null!=res) { return Response.success(res, HttpHeaderParser.parseCacheHeaders(response)); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return null; } }
.
import com.android.volley.Response.Listener; /** * * @author Young * * @param <T>请求结束后返回的对象,可以是 List<自定义类> 或 自定义类 */ public abstract class XRequestListener<T> implements Listener<T> { }
String url = "http://。。。。。。"; XJsonRequest<List<BikeBean>> request = new XJsonRequest<>( url, new XRequestListener<List<BikeBean>>() { @Override public void onResponse(List<BikeBean> response) { Log.v("result", response.toString()); } }, new ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.v("volleyerr", error.toString()); } }) .setMethod(Method.GET) ; Volley.newRequestQueue(context).add(request);
百度api store获取天气信息
String httpUrl = "http://apis.baidu.com/apistore/weatherservice/citylist"; String httpArg = "cityname=%E6%9C%9D%E9%98%B3"; String url = httpUrl + "?" + httpArg; XJsonRequest<WeatherBean> request = new XJsonRequest<WeatherBean>( Method.GET,url, new XRequestListener<WeatherBean>() { @Override public void onResponse(WeatherBean arg0) { Log.v("result", arg0.toString()); } }, new ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { Log.v("volleyerr", "天气。。"+arg0.toString()); } }).addHeader("apikey", "0ec4ef9237be00e62fcda68c1417a28e"); Volley.newRequestQueue(context).add(request);