清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
1. GET请求 // 1.创建请求队列
RequestQueue volleyRequestQueue = Volley.newRequestQueue(this);
// 2.json get请求处理
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET, "your server url", null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject arg0) {
try {
String strState = arg0.getString("state");
if (strState != null
&& strState.equalsIgnoreCase("success")) {
System.out.println("request success!");
} else {
System.out.println("request failed!");
}
} catch (Exception e) {
System.out.println("request failed!");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
System.out.println("request failed!");
}
}) {
@Override
protected Response<JSONObject> parseNetworkResponse(
NetworkResponse arg0) {
try {
JSONObject jsonObject = new JSONObject(new String(
arg0.data, "UTF-8"));
return Response.success(jsonObject,
HttpHeaderParser.parseCacheHeaders(arg0));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (Exception je) {
return Response.error(new ParseError(je));
}
}
};
// 3.请求对象放入请求队列
volleyRequestQueue.add(jsonObjectRequest);
2. POST请求
try { // 1.创建请求队列 RequestQueue volleyRequestQueue = Volley.newRequestQueue(this); // 2.POST请求参数 JSONObject params = new JSONObject(); params.put("name", "zhangsan"); params.put("password", "123456"); // 3.json post请求处理 JsonObjectRequest jsonObjectRequest = new JsonObjectRequest( Request.Method.POST, "your server url", params, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject arg0) { try { String strState = arg0.getString("state"); if (strState != null && strState.equalsIgnoreCase("success")) { System.out.println("request success!"); } else { System.out.println("request failed!"); } } catch (Exception e) { System.out.println("request failed!"); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { System.out.println("request failed!"); } }) { @Override protected Response<JSONObject> parseNetworkResponse( NetworkResponse arg0) { try { JSONObject jsonObject = new JSONObject(new String( arg0.data, "UTF-8")); return Response.success(jsonObject, HttpHeaderParser.parseCacheHeaders(arg0)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (Exception je) { return Response.error(new ParseError(je)); } } }; // 4.请求对象放入请求队列 volleyRequestQueue.add(jsonObjectRequest); } catch (Exception e) { }