网络请求库和图片加载库

学习目标

熟练掌握目前最流行网络请求类库的使用,如:
Ion:Android Asynchronous Networking and Image Loading
Volley:谷歌官方推出的网络请求和图片加载库
Retrofit:Square开源的基于OKHttp的性能良好更安全的类库
熟练掌握目前最流行图片加载类库的使用,如:
Glide:专注于处理流畅加载的图片类库
Picasso:Square开源的强大的效率高的图片加载类库
Fresco:Facebook开源的专注于优化java堆内存的图片加载类库

网络请求库

概述:所有网络库的原理是: 网络请求一般是基于HttpURLConnection和HttpClient进行封装的,也有自己编写Socket实现的,比如ion和OkHttp;请求的执行一般是通过线程池来管理,异步请求得到结果,则通过回调接口接收;并且一般接收结果的回调都通过Handler去在主线程执行

Ion的使用

详情查看Github主页https://github.com/koush/ion

介绍:
它支持网络请求和进行图片加载的双重功能
拥有链式api风格(Fluent API)
当Activity结束的时候支持自动的取消操作
支持SPDY/HTTP2,缓存,Gzip压缩,HTTP连接的重用等
并且是基于AndroidAsync实现的,AndroidAsync是作者的另一个使用socket实现的,遵循http协议的类库
添加依赖

dependencies {
    compile 'com.koushikdutta.ion:ion:2.+'
}

使用ion进行get请求

Ion.with(this)
   .load(Api.TEST)
   .asString()//以字符串形式返回,服务器返回的都是流,只是类库将流转为字符串了
   .setCallback(callback);//设置接收结果的回调接口

    FutureCallback<String> callback = new FutureCallback<String>() {
    /**
     * 回调是在主线程执行的,所以可以直接更新UI
     * @param e
     * @param result
     */
    @Override
    public void onCompleted(Exception e, String result) {
        text.setText(e == null ? result : e.getMessage());
    }
};

使用ion进行post请求,提交key-value形式的参数

Ion.with(this)
   .load(Api.LOGIN)
   .setBodyParameter("username","俊哥")//设置请求参数
   .setBodyParameter("password","123")
   .asString()
   .setCallback(callback);

使用ion进行post请求,提交json对象参数

//构造json对象
JsonObject json = new JsonObject();
json.addProperty("city","北京");
json.addProperty("year","2016");

Ion.with(this)
   .load(Api.POST_JSON)
   .setJsonObjectBody(json)
   .asString()
   .setCallback(callback);

使用ion进行上传文件,并显示进度

 File file = new File(Environment.getExternalStorageDirectory(),"dog.jpg");

Ion.with(this)
        .load(Api.UPLOAD)
        .uploadProgress(new ProgressCallback() {
            @Override
            public void onProgress(long downloaded, long total) {
                int percent = (int) (downloaded*100f/total+0.5f);
                Log.e("tag","上传进度:"+percent+"%");
            }
        })
        .setMultipartFile("file",file)
        .asString()
        .setCallback(callback);

使用ion进行下载文件,并且显示进度

File file = new File(Environment.getExternalStorageDirectory(),"a.jpg");
Ion.with(this)
        .load(Api.IMAGE)
        .progress(new ProgressCallback() {
            @Override
             public void onProgress(long downloaded, long total) {
                int percent = (int) (downloaded*100f/total+0.5f);
                text.setText("下载进度:"+percent+"%");
            }
        })
        .write(file)
        .setCallback(new FutureCallback<File>() {
            @Override
            public void onCompleted(Exception e, File file) {
                text.setText("下载成功:"+file.getAbsolutePath());
            }
        });

Retrofit的使用

详情查看https://github.com/square/retrofit

介绍

Square公司为Android开源的类型安全的Http客户端
底层基于OkHttp,使用OkHttp进行请求
将java API的定义转换为interface形式
使用annotation描述http请求
支持配置json解析器
添加依赖

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'

创建Retrofit实例对象

//创建Retrofit
Retrofit retrofit = new Retrofit.Builder()
        //注意,服务器主机应该以/结束,
        .baseUrl("http://192.168.2.103:8080/apitest/")//设置服务器主机
        .addConverterFactory(GsonConverterFactory.create())//配置Gson作为json的解析器
        .build();

定义业务逻辑接口

public interface HeiMaApi {
    /**
     * 定义了一个业务方法,获取订单,
     */
    @GET("test")//指定该方法要请求的url,注意方法的路径不要以/开头,如/test,是错误的
    Call<Stu> getOrder();
}

创建接口实例对象

HeiMaApi heiMaApi = retrofit.create(HeiMaApi.class);
获取业务方法的调用对象,并进行请求

 //调用业务方法,得到要执行的业务请求对象
Call<Stu> order = heiMaApi.getOrder();

//执行请求对象
//1.同步执行,得到响应对象,会阻塞UI,不推荐
//Response<Stu> response = order.execute();
//Stu stu = response.body();
//2.异步执行业务方法
order.enqueue(new Callback<Stu>() {
    @Override
    public void onResponse(Call<Stu> call, Response<Stu> response) {
        Stu stu = response.body();
        text.setText(stu.toString());
    }
    @Override
    public void onFailure(Call<Stu> call, Throwable t) {
    }
});

Retrofit的url注解处理

使用@Path注解来处理url路径不固定的需求,如

 @GET("test/{order}")//获取订单的某段路径不固定,
Call<Stu> getOrder(@Path("order")String order);

使用@Query注解来替换url后面跟的参数,如:

//url为:test?id=333
//使用@Query来替换查询参数
@GET("test")
Call<Stu> getOrderById(@Query("id") String id);

使用@QueryMap来替换多个查询参数,如

//假设url为:login?username=heima&password=111
//使用@QueryMay来替换多个查询参数
@GET("login")
Call<Stu> login(@QueryMap Map<String,String> map);

使用@Post注解进行post请求,提交key-value数据,如

@FormUrlEncoded//配置对请求体参数进行url编码
@POST("login")
Call<Login> postLogin(@Field("username")String username, @Field("password")String password);

使用@Post注解进行post请求,提交json数据,如

//使用@Body设置请求体参数
//注意,@Body不需要配置@FormUrlEncoded或者@Multipart
@POST("login")
Call<Login> postJson(@Body JsonObject jsonObject);

使用@Headers定义请求头,如

//定义请求头

@Headers({
        "Accept: application/vnd.github.v3.full+json",
        "User-Agent: Retrofit-Sample-App"
})

使用ResponseBody来接收流的数据,比如下载文件

//下载文件
@GET("image")
Call<ResponseBody> getImage();

使用@Muptipart和@Part或者@PartMao封装多块请求体

//上传文件和其他参数
@Multipart//将请求体进行Multipart拼接
@POST("uploadMulti") //RequestBody表示数据和数据类型的封装体
//Call<UploadResult> upload(@Part("file") RequestBody       file,@Part("params1") RequestBody params1);
//或者使用@PartMap
Call<UploadResult> upload(@PartMap Map<String, RequestBody> map);

需要注意的是,构建RequestBody的时候要注意拼接:

File file = new File(Environment.getExternalStorageDirectory(),"a.jpg");
File file2 = new File(Environment.getExternalStorageDirectory(),"dog.jpg");
RequestBody fileBody = RequestBody.create(MediaType.parse("image/jpeg"),file);
RequestBody fileBody2 = RequestBody.create(MediaType.parse("image/jpeg"),file2);

HashMap<String,RequestBody> map = new HashMap<>();
map.put("file\"; filename=\""+file.getName(),fileBody);
map.put("file\"; filename=\""+file2.getName(),fileBody2);

Call<UploadResult> uploadResultCall = HeiMaClient.create().heiMaApi.upload(map);
uploadResultCall.enqueue(new Callback<UploadResult>() {
    @Override
    public void onResponse(Call<UploadResult> call, Response<UploadResult> response) {
        text.setText(response.body().toString());
    }
    @Override
    public void onFailure(Call<UploadResult> call, Throwable t) {

    }
});

Volley的使用

介绍
谷歌开源的,专注于处理高频率的数据比较小的请求
内部仍然是使用的HttpURLConnection和HttpClient进行网络请求的,只是对于不同的Android版本进行了响应的切换,2.3之前使用的HttpClient,2.3之后使用的是HttpURLConnection
支持取消请求
具有网络请求和图片加载的功能
添加依赖

compile 'com.android.volley:volley:1.0.0'

创建RequestQueue请求队列,它是用来执行请求对象的

RequestQueue queue = Volley.newRequestQueue(this);

创建请求对象,这里使用最简单的StringRequest:

StringRequest stringRequest = new StringRequest(Api.TEST, new com.android.volley.Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        text.setText(response);
    }
}, new com.android.volley.Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

    }
});

执行请求,将Request对象添加到RequestQueue中,即可

 //3.执行请求
queue.add(stringRequest);

使用JsonRequest进行请求,返回的是json对象

//1.创建JsonRequest请求
JsonObjectRequest joRequest = new JsonObjectRequest(url, null, new Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        tv_result.setText(response.toString());
    }
}, new MyErrorListener());
//2.执行请求
queue.add(joRequest);

使用Volley发送post请求,需要自己重写Request的getParams方法

public class PostReuqest extends StringRequest {
    private Map<String, String> params;
    public PostReuqest(String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(url, listener, errorListener);
    }
    public PostReuqest(int method,String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(method,url, listener, errorListener);
    }
    public void setParams(Map<String, String> params){
        this.params = params;
    }
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        return params;
    }
}

PostReuqest stringRequest = new PostReuqest(Request.Method.POST,Api.LOGIN, new com.android.volley.Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        text.setText(response);
    }
}, new com.android.volley.Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

    }
});
HashMap<String,String> map = new HashMap<>();
map.put("username","hehehe");
map.put("password","12321");
stringRequest.setParams(map);

图片加载库

原理概述:图片加载的工作流(task flow)都是3级缓存的流程;图片的内存缓存一定是LruCache实现;图片下载和读取线程的调度一定是通过线程池管理

画图说明图片加载原理

Glide的使用
详情查看https://github.com/bumptech/glide

介绍:

专注于处理平滑滑动的图片类库
默认使用HttpUrlConnection下载图片
支持设置渐渐显示的动画
支持设置加载中的图片
添加依赖

compile 'com.github.bumptech.glide:glide:3.7.0'

使用Glide加载图片

Glide.with(this)
        .load("")
        .centerCrop()//设置从中间剪切
        .placeholder(R.mipmap.ic_launcher)//设置加载过程中显示的图片
        .error(R.mipmap.ic_launcher)//设置加载失败显示的图片
        .crossFade()//设置渐渐显示的效果
        .into(image);

Picasso的使用

详情查看https://github.com/square/picasso

介绍:

Square开源的比较早的图片加载类库
自动处理adapter中的ImageView的回收时取消下载图片
支持加载多种来源的图片,比如网络,sd卡,res资源
支持设置占位图片
支持对图片的自定义处理
添加依赖

compile 'com.squareup.picasso:picasso:2.5.2'

使用Picasso加载图片

Picasso.with(this)
        .load("url")
        .placeholder(R.mipmap.ic_launcher)
        .error(R.mipmap.ic_launcher)
        .centerCrop()
        .noFade()//设置不需要渐渐显示的动画效果
        .resize(120,120)//指定压缩参考的宽高比
        .into(image);

加载其他资源路径的图片

Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.with(context).load(new File(...)).into(imageView3);

注意:如果不设置resize(120,120),则Picasso会加载整个图片,显然这样消耗的内存比较大,一般都需要指定一下,而Glide内部已经默认参考了控件的宽高来进行缩放了。

Fresco的使用

详情查看https://github.com/facebook/fresco

介绍:

Facebook开源的专注于优化java堆内存,最大程度减少OOM
在Android4.4以及以下,将图片存储在Android的一块特殊的内存区域,这会让图片处理更加快速
支持Gif和WebP格式的图片
添加依赖

compile 'com.facebook.fresco:fresco:0.11.0'

首先初始化Fresco,一般在Application的onCreate中初始化

//先初始化Fresco
Fresco.initialize(this);

使用Fresco提供的SimpleDraweeView显示图片

 draweeView.setImageURI("url");

由于使用的是自定义控件加载图片,那么通过定义属性来进行设置:

<com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/image_view"
    android:layout_width="300dp"
    android:layout_height="300dp"
    fresco:fadeDuration="300"
    fresco:actualImageScaleType="focusCrop"
    fresco:placeholderImage="@color/wait_color"
    fresco:placeholderImageScaleType="fitCenter"
    fresco:failureImage="@drawable/error"
    fresco:failureImageScaleType="centerInside"
    fresco:retryImage="@drawable/retrying"
    fresco:retryImageScaleType="centerCrop"
    fresco:progressBarImage="@drawable/progress_bar"
    fresco:progressBarImageScaleType="centerInside"
    fresco:progressBarAutoRotateInterval="1000"
    fresco:backgroundImage="@color/blue"
    fresco:overlayImage="@drawable/watermark"
    fresco:pressedStateOverlayImage="@color/red"
    fresco:roundAsCircle="false"
    fresco:roundedCornerRadius="1dp"
    fresco:roundTopLeft="true"
    fresco:roundTopRight="false"
    fresco:roundBottomLeft="false"
    fresco:roundBottomRight="true"
    fresco:roundWithOverlayColor="@color/corner_color"
    fresco:roundingBorderWidth="2dp"
    fresco:roundingBorderColor="@color/border_color"
/>

属性解释:
placeholderImage就是所谓的展位图啦,在图片没有加载出来之前你看到的就是它

failureIamge看到名字就知道是什么了,图片加载失败时显示的图片就是它了

retryImage图片加载失败时显示,提示用户点击重新加载,重复加载4次还是没有加载出来的时候才会显示failureImage的图片

progressBarImage进度条图片
backgroundImage背景图片,这里的背景图片首先被绘制

overlayImage设置叠加图,在xml中只能设置一张叠加图片,如果需要多张图片的话,需要在java代码中设置哦
pressedStateOverlayImage设置点击状态下的叠加图,此叠加图不能缩放

ImageScaleType这个就是各种各样的图片缩放样式了,center,centerCrop,fouseCrop,centerInside,fitCenter,fitStart,fitEnd,fitXY

猜你喜欢

转载自blog.csdn.net/a94721990/article/details/81051143