Android中图片下载库XUtils封装

因为项目中经常用到图片上传下载操作,刚开始使用Volley的图片操作库,使用中发现图片较大时经常出现异常,所以更换成了XUtils库,但是如果直接用Xutils的方法,每次都要写一堆相同的代码,所以考虑再三,决定对Xutils库的图片上传、下载方法进行二次封装,满足自己项目的简单调用和后续复用的要求。
封装后的方法如下:

/**
     * 上传文件(xUtils)
     */
    public static<T> void uploadFile(final Activity activity, String url, Map<String, String> maps, Map<String, String> picMaps, final Class<T> jBean,final NetListenerT listener, final NetListenerError listenerError) {
        if (!UserConfig.isLogin()) {
            return;
        }
        addUserInfo(maps);
        LogUtil.d(TAG, url);

        RequestParams params = new RequestParams(); // 默认编码UTF-8
        if (picMaps != null) {
            for (Map.Entry<String,String> entry : picMaps.entrySet()) {
                params.addBodyParameter(entry.getKey(), new File(entry.getValue()));
            }
        }
        if (maps != null) {
            for (Map.Entry<String, String> entry : maps.entrySet()) {
                LogUtil.i(TAG, entry.getKey() + " : " + entry.getValue());
                params.addBodyParameter(entry.getKey(), entry.getValue());
            }
        }

        HttpUtils http = new HttpUtils();
        http.configSoTimeout(60 * 1000);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onStart() {
            }

            @Override
            public void onLoading(long total, long current, boolean isUploading) {
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                LogUtil.d(TAG, "上传完毕" + responseInfo.result);
              
                T r = GsonUtil.json2bean(responseInfo.result, jBean);
                if (r == null) {
                    LogUtil.e(TAG, "JSON解析错误");
                }
                listener.onResponse(r);// 回调
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                if (error != null && error.getExceptionCode() == 401) {
                    activity.finish();
                    //身份验证失败,退出登录
                }
                if (msg != null) {
                    LogUtil.e(TAG, "上传失败" + msg);
                }
                if (error != null && error.getMessage() != null) {
                    LogUtil.e(TAG, "上传失败" + error.getMessage());
                }
                if (listenerError != null) {
                    listenerError.onErrorResponse(msg);
                }
            }
        });
    }

    /**
     * 下载文件(xUtils)
     */
    public static<T> void downloadFile(String url, String target,final startListenerT slistener,  final NetListenerT listener, final NetListenerError listenerError) {
        if (!UserConfig.isLogin()) {
            return;
        }
        url = addUserInfo(url);
        LogUtil.d(TAG, url);

        HttpUtils http = new HttpUtils();
        http.configSoTimeout(60 * 1000);
        http.download(url, target, new RequestCallBack<File>(){


                    @Override
                    public void onStart() {
                        super.onStart();
                       if(slistener!=null){
                           slistener.onResponse();
                       }
                    }

                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                        super.onLoading(total, current, isUploading);
                        if(slistener!=null){
                            slistener.onResponse();
                        }
                    }

                    @Override
                    public void onSuccess(ResponseInfo responseInfo) {
                        if(listener!=null){
                            listener.onResponse(responseInfo.result);
                        }
                    }


                    @Override
                    public void onFailure(HttpException error, String msg) {
                        if(listenerError!=null){
                            listenerError.onErrorResponse(msg);
                        }
                    }});
    }


链接:https://www.jianshu.com/p/62ddc618dbc3

posted @ 2022-12-16 16:43  herry507  阅读(72)  评论(0编辑  收藏  举报