常见网络请求库汇总

概述:

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

几大网络请求库:

  • Ion:Android Asynchronous Networking and Image Loading
  • Volley:谷歌官方推出的网络请求和图片加载库
  • Retrofit:Square开源的基于OKHttp的性能良好更安全的类库

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

网络请求总结

Anroid原生网络请求

  • HttpURLConnention: 谷歌原生提供的用于网络请求的轻量级方案,支持数据压缩;缺点是封装性差,使用起来麻烦,尤其是做multipart提交的时候需要自己拼接符合http协议的请求参数;另外HttpURLConnection在2.3版本以下有bug,具体是关闭流的时候会导致线程池的线程无法停止;2.3以上修复了该bug。
  • HttpClient: 谷歌原生内置的apache的HttpClient请求库,功能多样,缺点是API设计繁多,使用起来麻烦,随着OkHttp越来越流行,谷歌决定在android4.4以后的版本,废除HttpClient,而且HttpURLConnection的底层实现也采用的OkHttp;

第三方网络请求库

  • Retrofit: Square公司出品,基于OkHttp封装,增加了动态代理和数据解析的封装;支持多种数据格式的解析如json,xml等;由于使用了动态代理,所以更安全,api风格和OkHttp相似;
  • OkHttp: Square公司出品,底层封装Socket实现,特点是多个网络请求重用一个Socket长连接,并且使用连接池来缓存连接,所以网络延时低,请求高效稳定;支持http2和SPDY协议;使用OkIO进行读写操作,由于OkIO底层使用NIO,所以读写更高效;
  • Ion: 也是优秀的网络库,底层同样是封装Socket使用,重用长连接,维护连接池;支持多种数据格式的解析返回;Api设计简洁好用;但是流行程度没有OkHttp高。
  • Volley:谷歌官方在2012年开发者大会推出的网络库,特点是适合处理请求频繁但是数据量小的场景。本身对文件上传支持比较差,需要自己编写代码。底层实现是在2.3之前使用HttpClient,2.3之后使用HttpURLConnection;同时具有加载图片的功能,图片加载模块没有实现图片的内存缓存,需要我们自己实现;
  • XUtil: 快速开发综合框架,拥有ViewInject,DB,网络请求,图片加载4个模块。网络模块底层封装的HttpURLConnection,知名度没有OkHttp高。
原文地址:https://www.cnblogs.com/yegong0214/p/6398897.html