Volley请求HTTPS,及其实现post,delete,get,put,上传下载图片。demo

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;
			}
		}
	};


下面是net网络请求类全部源代码:


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:下载链接




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值