码迷,mamicode.com
首页 > 移动开发 > 详细

小记:对Android网络下载工具的初步封装!(包括json,字符串下载(volley),和图片下载(glide))

时间:2016-07-31 17:36:37      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

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();
    }
}

 

小记:对Android网络下载工具的初步封装!(包括json,字符串下载(volley),和图片下载(glide))

标签:

原文地址:http://www.cnblogs.com/jiutiankunpeng/p/5723189.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!