由于只是为了快速出效果,这里只是做了简单的网络请求封装
首先,为了后面进行缓存处理,需要Volley的缓存线程,所以这里采用了JsonRequst作为基类对象
package net;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.android.volley.AuthFailureError;
import com.android.volley.Cache;
import com.android.volley.NetworkResponse;
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.android.volley.toolbox.JsonRequest;
import com.google.gson.Gson;
public class Protocol extends JsonRequest {
private Class mClass;
private Gson mGson;
private Map mHeaders;
public Protocol(Class mClass, String url, Listener listener, ErrorListener errorListener,
Map headers) {
super(Method.GET, url, null, listener, errorListener);
this.mClass = mClass;
this.mHeaders = headers;
mGson = new Gson();
}
/**
* 添加请求头
*/
@Override
public Map getHeaders() throws AuthFailureError {
Map headers = new HashMap<>();
if (mHeaders != null) {
for (Entry en : mHeaders.entrySet()) {
String key = en.getKey();
String value = en.getValue();
headers.put(key, value);
}
}
return headers;
}
@Override
protected Response parseNetworkResponse(NetworkResponse response) {
// 解析网络请求成功时的数据
try {
String json = new String(response.data, PROTOCOL_CHARSET); //PROTOCOL_CHARSET Volley中Request默认的UTF-8编码格式
// 解析成javabean
T dataBean = mGson.fromJson(json, mClass);
// 从响应头中解析获得缓存线程
Cache.Entry cEntry = HttpHeaderParser.parseCacheHeaders(response);
//通过success方法获得数据,如果缓存线程有数据,直接先从缓存中获取
return Response.success(dataBean, cEntry);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
为了防止多次创建请求队列,创建一个管理类,用于管理请求,程序启动创建请求队列,后面发送请求只需添加进去即可,不用再次创建请求队列
package manager;
import android.app.Application;
public class PrjAplication extends Application{
@Override
public void onCreate() {
super.onCreate();
// 程序启动时,创建NetWorkManager实例,以便创建请求队列
NetWorkManager.init(getApplicationContext());
}
}
注意,这里需要清单文件做一些配置
package manager;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.Volley;
import android.content.Context;
/**
* 网络请求的管理
* @author Administrator
*
*/
public class NetWorkManager {
private static Context context;
private static RequestQueue requestQueue;
public static void init(Context applicationContext) {
context = applicationContext;
//程序启动时,创建请求队列
requestQueue = Volley.newRequestQueue(context);
}
/**
* 添加发送请求
* @param request
*/
public static void sendRequest(JsonRequest request){
requestQueue.add(request);
}
public static Context getContext(){
return context;
}
}
最后,在需要发送请求的类中实现请求
private void loadData() {
String url = "http://apis.baidu.com/txapi/huabian/newtop?num=10&page=" + mPageIndex;
Map headers = new HashMap<>();
headers.put("apikey", "a4596ace542f168b5f1c080b5b80d898");
// 创建请求队列
// 创建请求
Protocol request = new Protocol(NewsBean.class, url, listener, errorListener, headers);
// 添加发送请求
NetWorkManager.sendRequest(request);
}
private Listener listener = new Listener() {
@Override
public void onResponse(NewsBean response) {
mList = response.newslist;
mAdapter = new EveryDayAdapter(MainActivity.this, mList);
// 3.加载数据到每个Item视图中,通过容器进行添加
loadOutView();
}
};
private ErrorListener errorListener = new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
};