volley 实现验证码功能

    公司的项目,为防止机器注册,需增加验证码功能,开始以为只是一张图片,通过glide加载个地址就好,但接口的同事说验证码接口是返回一个流,因为服务端不应该做验证码图片的存储,因为验证是不停变动的,但公司的接口都是https,有相应的证书校验环节及头部信息校验,而项目中现存的网络请求是封装的volley json请求,只应该返回json的接口,之前了解的也是volley有jsonRequest,StringReque.

     开始打算自己在封装一套网络请求用于验证码接口,一个偶然的想法,volley是否提供有相应的api呢?通过调研发现 volley有 提供ImageRequest ,实现方法如下:

MyImageRequest imageRequest = new MyImageRequest( NetworkHelper.processUrl(NetConfig.imgVerificationCode), new Response.Listener<Bitmap>() { @Override public void onResponse(Bitmap response) { try { isGettingImageCode = false;

1 img\_verificationCode\_btn.setImageBitmap(response); 2 img\_verificationCode\_btn.setVisibility(View.VISIBLE); 3 tv\_get\_imgCode\_error.setVisibility(View.GONE); 4 pb\_loading\_imgcode.setVisibility(View.GONE); 5 } catch (Exception e) { 6 e.printStackTrace(); 7 } 8 } 9 }, 0, 0, Bitmap.Config.RGB\_565, new Response.ErrorListener() { 10 @Override 11 public void onErrorResponse(VolleyError error) { 12 isGettingImageCode = false; 13 img\_verificationCode\_btn.setVisibility(View.GONE); 14 tv\_get\_imgCode\_error.setVisibility(View.VISIBLE); 15 pb\_loading\_imgcode.setVisibility(View.GONE); 16 tv\_get\_imgCode\_error.setText("点击重试");

// ToastUtil.show("网络异常,图片验证码获取失败"); }

1 }); 2 MyApplication.getInstance().getRequestQueuequeue().add(imageRequest);

public class MyImageRequest extends ImageRequest { public MyImageRequest(String url, Response.Listener<Bitmap> listener, int maxWidth, int maxHeight, ImageView.ScaleType scaleType, Bitmap.Config decodeConfig, Response.ErrorListener errorListener) { super(url, listener, maxWidth, maxHeight, scaleType, decodeConfig, errorListener); }

1public MyImageRequest(String url, Response.Listener<Bitmap> listener, int maxWidth, int maxHeight, Bitmap.Config decodeConfig, Response.ErrorListener errorListener) { 2 super(url, listener, maxWidth, maxHeight, decodeConfig, errorListener); 3 4} 5 6@Override 7public Map<String, String> getHeaders() throws AuthFailureError { 8 return initHeader(urlBuilder(getUrl(),null),null,"GET"); //封装head信息 9 10}

相关的证书校验  在之前的文章中提到 重写

HTTPSTrustManager中的

checkServerTrusted

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

SpringBoot自定义序列化的使用方式

场景及需求:项目接入了SpringBoot开发,现在需求是服务端接口返回的字段如果为空,那么自动转为空字符串。例如:\    {        "id":1,        "name":null    },    {        "id":2,        "name":"x