volley是Android开发团队在2013年Google I/O大会上推出了一个新的网络通信框架。因为公司服务端与android通信采用https协议,android用的访问网络框架就是volley。现在将项目中使用volley访问https的方法记录一下。
1.首先下载volley框架,下载地址http://download.csdn.net/detail/jifashihan/9316875。将volley.jar复制到Android Studio中libs文件夹中,右键点击jar,选择”Add As Library”
2.将需要用到的类下载下来,下载地址http://download.csdn.net/detail/jifashihan/9317813。将.java文件复制到工程中,将.jar文件复制到libs文件夹下,并引用
3.创建一个单例类。
public class VolleySingleton {
private static VolleySingleton volleySingleton;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private Context mContext;
public VolleySingleton(Context context) {
this.mContext = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache(){
private final LruCache<String,Bitmap> cache = new LruCache<String ,Bitmap>(20);
@Override
public Bitmap getBitmap(String url){
return cache.get(url);
}
@Override
public void putBitmap(String url,Bitmap bitmap){
cache.put(url,bitmap);
}
});
}
public static synchronized VolleySingleton getVolleySingleton(Context context){
if(volleySingleton == null){
volleySingleton = new VolleySingleton(context);
}
return volleySingleton;
}
public RequestQueue getRequestQueue(){
if(mRequestQueue == null){
InputStream keyStore =mContext.getResources().openRawResource(R.raw.server);
mRequestQueue = Volley
.newRequestQueue(mContext,
new ExtHttpClientStack(new SslHttpClient(keyStore, “123456”, 443)));
//mRequestQueue = Volley.newRequestQueue(mContext.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req){
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
其中 InputStream keyStore =mContext.getResources().openRawResource(R.raw.server);是读取raw文件夹下的server.bks,也就是服务端生成的证书。new SslHttpClient(keyStore, “123456”, 443),123456是密码,443是端口。
4.创建volley访问网络的通用方法,因为服务器接口是post类型的,所以封装post方法
public class VolleyMethods {
private String result = “”;//访问数据,返回结果
private Context mContext;
private RequestQueue mRequestQueue;
public VolleyMethods(Context context) {
mContext = context;
mRequestQueue = VolleySingleton.getVolleySingleton(mContext).getRequestQueue();
}
/**
* Post
*
* @param url 访问url
* @param requestTag 请求Tag,用于停止请求
* @param paramkeys Post参数的key
* @param paramValues Post参数的value
*/
public void StringRequestPostMethod(String url, String requestTag, final ArrayList<String> paramkeys, final ArrayList<String> paramValues
,Response.Listener<String> listener,Response.ErrorListener errorListener) {
result = “”;
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, listener, errorListener) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < paramkeys.size(); i++) {
map.put(paramkeys.get(i), paramValues.get(i));
}
return map;
}
};
stringRequest.setTag(requestTag);
mRequestQueue.add(stringRequest);
}
}
5.调用volley访问网络
//得到获取验证码接口,返回参数名。StaticParameter.interfaceUrl接口地址,interfaceMethodName接口名称
public static ArrayList<String> SendCaptchaMethod(){
interfaceMethodName = StaticParameter.interfaceUrl+”sendCaptcha”;
ArrayList<String> paramkeys = new ArrayList<String>();
paramkeys.add(“phonenumber”);
return paramkeys;
}
//调用获取验证码的接口
final VolleyMethods volleyMethods = new VolleyMethods(ForgetPasswordActivity.this);
ArrayList<String> paramkeys = new ArrayList<String>();
ArrayList<String> paramValues = new ArrayList<String>();
paramValues.add(userPhone);
paramkeys = UrlInterfaceMethods.SendCaptchaMethod();
volleyMethods.StringRequestPostMethod(UrlInterfaceMethods.interfaceMethodName,
“SendCaptcha”, paramkeys, paramValues, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
Log.e(“”, s);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.e(“volleyError”, volleyError.toString());
}
});
参考博客
http://blog.sina.com.cn/s/blog_4b20ae2e0102vpwv.html