Volley架包: https://github.com/mcxiaoke/android-volley.git
数据来源:聚合数据
/**
* Created by Libin don 2016/7/16.
* Voelly请求网络数据,工具类
*
*/
public class NetUtile {
private final RequestQueue queue;
// 1.创建RequestQueue
public NetUtile(Context context) {
queue = Volley.newRequestQueue(context);
}
// 2.创建StringRequest,get请求
public void RequestQueue_Get(String url) {
/*
* StringQueue 中的三种参数:
* 1请求路径;
* 2请求成功;
* 3请求失败;
*
* */
StringRequest sr = new StringRequest("http://apis.juhe.cn/goodbook/catalog?key=3e100e799d37b42ff2cdbecc67ecb12b&dtype=json", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("MainActivity", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("MainActivity", "请求失败!!!");
}
});
queue.add(sr);
}
// 2.创建StringRequest,post请求
public void RequestQueue_Post(String url) {
StringRequest psr = new StringRequest(Request.Method.POST, "http://v.juhe.cn/toutiao/index", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("MainActivity", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("MainActivity", "请求失败!!!");
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<>();
map.put("key", "a4806d02149387aaeb859a73888a081c");
map.put("type", "top");
return map;
}
};
queue.add(psr);
}
/* 网络请求第二种方法:
1.JsonObjectRequest 请求一段JSON数据
2.JsonArrayRequest 请求一段JSON数组数据
*/
/* JsonObjectRequest 中的三个参数(Get请求):
* 1.请求路径
* 2.请求成功
* 3.请求失败
* */
// 1.JsonObjectRequest 请求一段JSON数据 ,Get请求
public void JsonObjectRequest_Get(String url){
JsonObjectRequest jor=new JsonObjectRequest("http://apis.juhe.cn/goodbook/catalog?key=3e100e799d37b42ff2cdbecc67ecb12b&dtype=json", new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("MainActivity", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("MainActivity","请求失败!!!" );
}
});
queue.add(jor);
}
// 2.JsonObjectRequest 请求一段JSON数据 ,Post请求
/* JsonObjectRequest 中的四个参数(Post请求):
1.请求方式
* 2.请求路径
* 3.请求成功
* 4.请求失败
* 由于是post请求,所以我们要重写getParams();
* */
public void JsonObjectRequest_Post(String url){
Map<String, String> map=new HashMap<>();
map.put("key", "a4806d02149387aaeb859a73888a081c");
map.put("type", "top");
JSONObject jsonobject=new JSONObject(map);
final String mRequestBody = appendParameter(url,map);
JsonObjectRequest pjor=new JsonObjectRequest(Request.Method.POST, "http://v.juhe.cn/toutiao/index",jsonobject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("MainActivity", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=" + getParamsEncoding();
}
@Override
public byte[] getBody() {
try {
return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET);
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
mRequestBody, PROTOCOL_CHARSET);
return null;
}
}
};
queue.add(pjor);
}
private String appendParameter(String url,Map<String,String> params){
Uri uri = Uri.parse(url);
Uri.Builder builder = uri.buildUpon();
for(Map.Entry<String,String> entry:params.entrySet()){
builder.appendQueryParameter(entry.getKey(),entry.getValue());
}
return builder.build().getQuery();
}
}