Volley请求HTTPS,及其实现post,delete,get,put,上传下载图片。demo
demo效果
封装了post,delete,get,put,上传下载图片。
如POST方法:
HashMap<String, String> hashMap1 = new HashMap<String, String>();
hashMap1.put("title", "yujing");
hashMap1.put("name", "测试");
Ynet.getIntance(this).post(mHandler, url, flag, hashMap1);
HTTPS请求 传HTTPS的密码
Ynet.getIntance(this, "123456").put(mHandler, url, "4", hashMap4);
封装成单例模式,需要参数
1. handler 用于收到消息后通知前台界面更改
2.目标URL
3.flag 标记这是哪个请求,用户收到结果后判断
4.hashMap就是POST 的键值对应
handler:
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler() {
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public void handleMessage(Message msg) {
YnetMsg ynetMsg = (YnetMsg) msg.obj;
switch (msg.what) {
case Ynet.MSG_STRING:
String flag = ynetMsg.getFlag();
String uRL = ynetMsg.getUrl();
Log.i("URL..." + flag, uRL);
if ("1".equals(flag) {
String value = ynetMsg.getText();
Log.e("value", value);
}
break;
case Ynet.MSG_PIC:
String flagMSG_PIC = ynetMsg.getFlag();
if ("5".equals(flagMSG_PIC)) {
Bitmap value = ynetMsg.getBitmap();
relativeLayout_Background.setBackground(new BitmapDrawable(
value));
}
break;
}
}
};
package com.yujing.ynet;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Handler;
import android.os.Message;
import com.alibaba.fastjson.JSON;
import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
public class Ynet {
public static final int MSG_STRING = 85008;
public static final int MSG_PIC = 85009;
public static final int MSG_UPLOADIMG = 85010;
public int timeout = 7000;// 延迟时间
public int reNumber = 0;// 重试次数
private static RequestQueue requestQueue;// 请求队列
private static RequestQueue requestQueueHttps;// 请求队列https
private static Ynet ynet;
private Context context;
private boolean isHttps=false;
/**
* 普通http请求
* @param context
* @return
*/
public static Ynet getIntance(final Context context) {
if (ynet == null) {
ynet = new Ynet();
}
ynet.context = context;
ynet.isHttps=false;
if (requestQueue == null) {
requestQueue = Volley.newRequestQueue(context);
}
return ynet;
}
/**
* https请求
* @param context
* @return
*/
public static Ynet getIntance(final Context context,String httpsKey) {
if (ynet == null) {
ynet = new Ynet();
}
ynet.context = context;
ynet.isHttps=true;
if (requestQueueHttps == null) {
requestQueueHttps = Volley.newRequestQueue(context,new OkHttpStack(context));//HTTPS
}
return ynet;
}
public synchronized void get(Handler mHandler, String url, String flag,
HashMap<String, String> hashMap) {
if (hashMap != null) {
url += Where.toURL(url,hashMap);
}
StringRequest request=creat(Method.GET, mHandler, url, flag, null);
addRequestQueue(request);
}
public synchronized void post(Handler mHandler, String url, String flag,
HashMap<String, String> hashMap) {
StringRequest request=creat(Method.POST, mHandler, url, flag, hashMap);
addRequestQueue(request);
}
public synchronized void put(Handler mHandler, String url, String flag,
HashMap<String, String> hashMap) {
StringRequest request=creat(Method.PUT, mHandler, url, flag, hashMap);
addRequestQueue(request);
}
public synchronized void delete(Handler mHandler, String url, String flag,
HashMap<String, String> hashMap) {
if (hashMap != null) {
url += Where.toURL(url,hashMap);
}
StringRequest request=creat(Method.DELETE, mHandler, url, flag, null);
addRequestQueue(request);
}
private StringRequest creat(int Method, final Handler mHandler,
final String url, final String flag,
final HashMap<String, String> hashMap) {
final Message msg = new Message();// Handler通知前台
msg.what = MSG_STRING;// 消息永远是8
StringRequest mStringRequest = new StringRequest(Method, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
msg.obj = new YnetMsg(flag, response, "成功", url
+ (hashMap == null ? ("") : ("\n[参数]:" + JSON
.toJSONString(hashMap))));
mHandler.sendMessage(msg);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Message msg = new Message();// Handler通知前台
msg.what = MSG_STRING;// 消息永远是8
msg.obj = new YnetMsg(flag,volleyError.toString(),"失败",url+
(hashMap == null ? (""): ("\n[参数]:" + JSON.toJSONString(hashMap))));
mHandler.sendMessage(msg);
}
}) {
// 携带参数
@Override
protected HashMap<String, String> getParams()
throws AuthFailureError {
return hashMap;
}
};
mStringRequest.setShouldCache(false); // 控制是否缓存
mStringRequest.setRetryPolicy(new DefaultRetryPolicy(timeout, reNumber,
1.0f)); // 设置超时时间重试次数
return mStringRequest;
}
private void addRequestQueue(Request request){
if (isHttps) {
requestQueueHttps.add(request);
}else {
requestQueue.add(request);
}
}
public synchronized void DownImage(final Handler mHandler, final String url,
final String flag) {
final Message msg = new Message();// Handler通知前台
msg.what = MSG_PIC;
ImageRequest imageRequest = new ImageRequest(url,
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
msg.obj = new YnetMsg(flag, response, "成功", url);
mHandler.sendMessage(msg);
}
}, 0, 0, Config.RGB_565, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
msg.obj = new YnetMsg(flag, volleyError.toString(),
"失败", url);
;
mHandler.sendMessage(msg);
}
});
imageRequest.setShouldCache(true); // 控制是否缓存
imageRequest.setRetryPolicy(new DefaultRetryPolicy(7000, 0, 1.0f)); // 设置超时时间7秒重试次数0
addRequestQueue(imageRequest);
}
public synchronized void uploadImage(final Handler mHandler,
final String url, final String flag, final Map<String, File> files,
final Map<String, String> hashMap) {
final ProgressDialog progressDialog = ProgressDialog.show(ynet.context,
"请稍后", "正在上传...", false, true);
List<File> list = new ArrayList<File>();
list.add(files.get("file1"));
final Message msg = new Message();// Handler通知前台
msg.what = MSG_UPLOADIMG;// 上传图片消息10
@SuppressWarnings("rawtypes")
Request request = new PostUploadRequest(url, list, hashMap,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (progressDialog.isShowing()
&& progressDialog != null) {
progressDialog.dismiss();
}
msg.obj = new YnetMsg(flag, response, "成功", url
+ (hashMap == null ? (""): ("\n[参数]:" + JSON.toJSONString(hashMap))));
mHandler.sendMessage(msg);
}
});
addRequestQueue(request);
}
}
点击这里下载demo:下载链接