HTTP POST请求在Volly工厂关于需要鉴权的请求部分代码包含了处理图片实现ImageLoder法

来源:互联网 发布:淘宝三木社的衣服好吗 编辑:程序博客网 时间:2024/05/29 11:48

POST请求在Volly工厂关于需要鉴权的请求部分代码,包含了处理图片实现ImageLoder法

@Overridepublic String getStringFromPost(String url, String name, final String key1, final String value1, final String key2, final String value2, final String key3, final String value3, final String key4, final String value4, final String key5, final String value5) {    MyStringRequest stringRequest = new MyStringRequest(Request.Method.POST, url, new Response.Listener<String>() {        @Override        public void onResponse(String s) {            Message message = handler.obtainMessage();            message.what = 10;            Bundle bundle = new Bundle();            bundle.putString("one", s);            message.setData(bundle);            message.sendToTarget();        }    }, new Response.ErrorListener() {        @Override        public void onErrorResponse(VolleyError volleyError) {            Log.d("----", "not");        }    }) {        @Override        protected Map<String, String> getParams() throws AuthFailureError {            Map<String, String> map = new HashMap<>();            if (null != key1) {                map.put(key1, value1);            }            if (null != key2) {                map.put(key2, value2);            }            if (null != key3) {                map.put(key3, value3);            }            if (null != key4) {                map.put(key4, value4);            }            if (null != key5) {                map.put(key5, value5);            }            return map;        }    };    Map<String, String> map = new HashMap<>();    map.put("Authorization", name);    stringRequest.setHeader(map);    queue.add(stringRequest);    return null;}

ImageLoder处理图片的工厂方法

    @Override    public Image getImageSaveRadioSmallIcon(String url, ImageView imageView) {//        ImageLoader loader=new ImageLoader(queue,new BitmapCache());        ImageLoader loader = new ImageLoader(queue, new ImageLoader.ImageCache() {            @Override            public Bitmap getBitmap(String s) {                //此处添加处理bit图的逻辑代码,逻辑为获取bit图                File file = new File(FILL_PATH);                String bitmapName = "";                if (file.exists() && file.isDirectory()) {                    bitmapName = FILL_PATH + "/" + md5(s);                } else {                    bitmapName = FILE_PATE_BACK + "/" + md5(s);                }//                File bitmap = new File(bitmapName);//                if (bitmap.exists()) {                BitmapFactory.Options options = new BitmapFactory.Options();                options.inJustDecodeBounds = true;                int heightOut = options.outHeight;//稍后将以此内容做等比缩放                int widthOut = options.outWidth;//稍后将以此内容做等比缩放                options.inPreferredConfig = Bitmap.Config.RGB_565;                options.inSampleSize = 2;//                if ( heightOut != 0 && widthOut != 0) {//                    options.inSampleSize = (heightOut / heigth + widthOut / width) / 2;//                }                options.inJustDecodeBounds = false;                Bitmap bitmap = BitmapFactory.decodeFile(bitmapName, options);//                    Bitmap bitmapObj = BitmapFactory.decodeFile(bitmapName);                return bitmap;//                } else return null;            }            //此处添加处理bit图代码,逻辑为将bit保存在本地            @Override            public void putBitmap(String s, Bitmap bitmap) {                File file = new File(FILL_PATH);                boolean flag = false;                if (file.exists() && file.isDirectory()) {                    flag = true;                } else {                    flag = file.mkdirs();                    if (!flag) {                        file = new File(FILE_PATE_BACK);                        if (file.exists() && file.isDirectory()) {                        } else {                            file.mkdirs();                        }                    }                }                File bitmapFile;                if (!flag) {                    bitmapFile = new File(FILE_PATE_BACK + "/" + md5(s));                } else {                    bitmapFile = new File(FILL_PATH + "/" + md5(s));                }                try {                    if (!bitmapFile.exists()) {                        bitmapFile.createNewFile();                    }                    FileOutputStream fos = new FileOutputStream(bitmapFile);                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);                    fos.flush();                    fos.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        });        ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView, R.mipmap.bg, R.mipmap.bg);        loader.get(url, listener,80,80);        return null;    }
MD5加密算法保存文件
public final static String md5(String pwd) {    //用于加密的字符    char md5String[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',            'A', 'B', 'C', 'D', 'E', 'F'};    try {        //使用平台的默认字符集将此 String 编码为 byte序列,并将结果存储到一个新的 byte数组中        byte[] btInput = pwd.getBytes();        //信息摘要是安全的单向哈希函数,它接收任意大小的数据,并输出固定长度的哈希值。        MessageDigest mdInst = MessageDigest.getInstance("MD5");        //MessageDigest对象通过使用 update方法处理数据, 使用指定的byte数组更新摘要        mdInst.update(btInput);        // 摘要更新之后,通过调用digest()执行哈希计算,获得密文        byte[] md = mdInst.digest();        // 把密文转换成十六进制的字符串形式        int j = md.length;        char str[] = new char[j * 2];        int k = 0;        for (int i = 0; i < j; i++) {   //  i = 0            byte byte0 = md[i];  //95            str[k++] = md5String[byte0 >>> 4 & 0xf];    //    5            str[k++] = md5String[byte0 & 0xf];   //   F        }        //返回经过加密后的字符串        return new String(str);    } catch (Exception e) {        return null;    }}

0 0