zoukankan      html  css  js  c++  java
  • 小记:对Android网络下载工具的初步封装!(包括json,字符串下载(volley),和图片下载(glide))

    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.widget.ImageView;
    import com.android.volley.RequestQueue;
    import com.android.volley.Response;
    import com.android.volley.VolleyError;
    import com.android.volley.toolbox.Volley;
    import com.bumptech.glide.Glide;
    import com.bumptech.glide.load.engine.DiskCacheStrategy;
    import com.example.nr.carton.R;
    import de.hdodenhof.circleimageview.CircleImageView;
    
    /**
     * Created by hjw on 2016/7/27.
     */
    public class HttpUtils {
        /**
         * 验证当前是否有网络*/
        public static boolean checkNetWork(Context ct) {
            //获取连接管理对象--
            ConnectivityManager cm=(ConnectivityManager) ct.getSystemService(Context.CONNECTIVITY_SERVICE);
            //获取网络信息对象
            NetworkInfo ni=cm.getActiveNetworkInfo();
            if(ni!=null&&ni.isAvailable())//非null验证
            {
                return true;
            }
            else {
                return false;
            }
        }
        public  static void downloadJson(Context context,String url,final ResultCallBack callBack){
            RequestQueue mQueue = Volley.newRequestQueue(context);
            MyStringRequest stringRequest = new MyStringRequest(url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            if (response.toString()!=null){
                                callBack.onSuccess(response.toString());
                            }
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
    
    
                }
            });
    
            mQueue.add(stringRequest);
        }
        public static void downLoadPic(String url,Context context,ImageView imageView){
            Glide.with(context)
                    .load(url)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .placeholder(R.drawable.bizhi)
                    .error(R.drawable.bizhi)
                    .centerCrop()
                    .into(imageView);
        }
        public static void downLoadPic1(String url,Context context,ImageView imageView){
            Glide.with(context)
                    .load(url)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .placeholder(R.drawable.bizhi)
                    .error(R.drawable.bizhi)
                    .into(imageView);
        }
        public static void downLoadPicCircle(String url,Context context,CircleImageView imageView){
            Glide.with(context)
                    .load(url)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
    //                .placeholder(R.mipmap.ic_launcher)
    //                .error(R.mipmap.ic_launcher)
                    .centerCrop()
                    .into(imageView);
        }
        public static void downLoadPicReSize(String url,Context context,ImageView imageView,int width,int height){
            Glide.with(context)
                    .load(url)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .placeholder(R.drawable.bizhi)
                    .error(R.drawable.bizhi)
                    .centerCrop()
                    .override(width, height)
                    .into(imageView);
        }
        /**
         *异步下载时,通过接口回掉保存和传输数据
         */
        public interface ResultCallBack{
            void onSuccess(String s);
            //void onError();
        }
    }
    善守者藏于九地之下, 善攻者动于九天之上
  • 相关阅读:
    NTP时钟服务器(卫星授时)技术让网络时间同步不再难
    视频监控系统为何需要GPS北斗授时服务器?
    如何选购GPS北斗卫星时钟同步装置?市场均价多少?
    Orange'S学习笔记(一)
    Oracle 11g中文版高清视频教程
    新书推荐:Orange'S:一个操作系统的实现
    引导扇区汇编代码解释
    灰度图转伪彩色图代码
    Visual Odometry For GPSDenied Flight And Mapping Using A Kinect
    [原创]日行一善,命运在不知不觉中改变
  • 原文地址:https://www.cnblogs.com/jiutiankunpeng/p/5723189.html
Copyright © 2011-2022 走看看