HTTP POST請求在Volly工廠關於需要鑑權的請求部分代碼包含了處理圖片實現ImageLoder法

POST請求在Volly工廠關於需要鑑權的請求部分代碼,包含了處理圖片實現ImageLoder法

@Override
public 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;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章