小记:对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();
    }
}

 

posted @ 2016-07-31 15:52  九天鲲鹏  阅读(319)  评论(0编辑  收藏  举报