Retrofit与OkHttp进行HTTP网络编程使用指南

Retrofit与okhttp共同出自于Square公司.

OkHttp我就不用多说了,相信大家已经非常熟悉了,不太了解的可以看下这里OkHttp,也可以看下hongyang大牛的OkHttpUtils

Retrofit它是一个类型安全的网络请求库,Retrofit简化了网络请求流程,基于OkHtttp做了封装,解耦的更彻底。

Retrofit使用:
1.在build.gradle中添加如下配置

compile 'com.squareup.retrofit2:retrofit:2.0.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0'
compile 'com.squareup.okhttp3:okhttp:3.0.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'  

2.初始化Retrofit

 retrofit = new Retrofit.Builder()
.client(httpClientBuilder.build())
.addConverterFactory(GsonConverterFactory.create())  //添加转化库
.baseUrl(Constant.BASE_URL)//注意:url必须是以/结尾
.build();

3.初始化OkHttpClient

//创建一个OkHttpClient
OkHttpClient.Builder builder = new OkHttpClient().newBuilder()
    .connectTimeout(10, TimeUnit.SECONDS)  //设置超时时间
    .readTimeout(10, TimeUnit.SECONDS)     //设置读取超时时间
    .writeTimeout(10, TimeUnit.SECONDS);   //设置写入超时时间

int cacheSize = 10 * 1024 * 1024; // 设置缓存大小10 MiB
Cache cache = new Cache(App.getContext().getCacheDir(), cacheSize);
builder.cache(cache);
builder.addInterceptor(interceptor);//添加一个拦截器
mOkHttpClient = builder.build();  

4.关于ConverterFactory
ConverterFactory初次接触多少有点陌生,其实这个就是用来统一解析ResponseBody返回数据的。
常见的ConverterFactory:

Gson: com.squareup.retrofit2:converter-gson
Jackson: com.squareup.retrofit2:converter-jackson
Moshi: com.squareup.retrofit2:converter-moshi
Protobuf: com.squareup.retrofit2:converter-protobuf
Wire: com.squareup.retrofit2:converter-wire
Simple XML: com.squareup.retrofit2:converter-simplexml
Scalars (primitives, boxed, and String): com.squareup.retrofit2:converter-scalars  

5.定义接口 get 请求
(1)get请求 不带任何参数

public interface UserService {

  @GET("users")//不带参数get请求
  Call<List<UserBean>> getUsers();

}  

(2)get请求 带参数

public interface UserService {

  @GET("users")//不带参数get请求
  Call<List<UserBean>> getUsers(@Query("username") String username);

}  

(3)get请求 动态路径 @Path使用

//groupId你可以把它当做一个占位符
public interface UserService {

  @GET("users/{groupId}")//动态路径get请求
  Call<UserBean> getUserInfo(@Path("userId") String userId);

}  

(4)get请求 拼接参数 @Query使用

public interface UserService {

  @GET("users/{userId}")
  Call<List<UserBean>> getUsers(@Path("userId") String userId, @Query("age")int age);

}  

6.定义接口 post请求
(1)post请求 @body使用

public interface UserService {

  @POST("add")//直接把对象通过ConverterFactory转化成对应的参数
  Call<List<UserBean>> addUser(@Body UserBean user);

}  

(2)post请求 @FormUrlEncoded,@Field使用

public interface UserService {

  @POST("login")
  @FormUrlEncoded//读参数进行urlEncoded
  Call<UserBean> login(@Field("username") String username, @Field("password") String password);  

(3)post请求 @FormUrlEncoded,@FieldMap使用

public interface UserService {

  @POST("login")
  @FormUrlEncoded//读参数进行urlEncoded
 Call<UserBean> login(@FieldMap HashMap<String, String> paramsMap);

}  

(4)post请求 @Multipart,@Part使用

public interface UserService {

  @Multipart
  @POST("login")
  Call<UserBean> login(@Part("userId") String userId, @Part("password") String password);

}  

7.Cache-Control缓存控制

public interface UserService {

  @Headers("Cache-Control: max-age=640000")
  @GET("users")//不带参数get请求
  Call<List<User>> getUsers();

}  

8.请求使用
(1)返回UserApi

  private void initUserService() {
       userApi = retrofit.create(UserService.class);
  }

  public static UserService api() {
       return api.userApi;
  }  

(2)发送请求

Call<String> call = Api.api().login(userId,password);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
    Log.e("", "response---->" + response.body());
}

@Override
public void onFailure(Call<String> call, Throwable t) {
    Log.e("", "response----失败");
});

9.拦截器配置
OkHttp 3.0 的配置由原本的 okHttp 对象直接各种 set 进行配置改为 Builder 配置模式,所以原本对应的方法应该到 OkHttpClient.Builder 类对象下寻找

(1)HttpLoggingInterceptor
用于输出网络请求和结果的 Log,可以配置 level 为 BASIC / HEADERS / BODY,都很好理解,对应的是原来 retrofit 的 set log level 方法,现在 retrofit 已经没有这个方法了,所以只能到 OkHttp 这边来配置,并且 BODY 对应原来到 FUL

HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        //同样okhttp3后也使用build设计模式
        mOkHttpClient = new OkHttpClient.Builder()
                //设置请求读写的超时时间
                .connectTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .cache(cache)//设置缓存
                .addInterceptor(httpLoggingInterceptor)//添加拦截器
//              .authenticator()
                .build();  

(2)retryOnConnectionFailure 方法为设置出现错误进行重新连接。
(3)connectTimeout 设置超时时间
(4)addNetworkInterceptor

//拦截器
Interceptor interceptor = new Interceptor() {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        if (!OkHttpUtil.isNetworkConnected(App.getContext())) {//判断网络是否可用
            request = request.newBuilder()
                    .cacheControl(CacheControl.FORCE_CACHE)
                    .build();
        }
        Response response = chain.proceed(request);
        if (OkHttpUtil.isNetworkConnected(App.getContext())) {
            int maxAge = 60 * 60;
            // 有网络时 设置缓存超时时间1个小时
            response.newBuilder()
                    .header("Cache-Control", "public, max-age=" + maxAge)
                    .removeHeader("Pragma")// 清除头信息,因为服务器如果不支持,会返回一些干扰信息,不清除下面无法生效
                    .build();
        } else {
            // 无网络时,设置超时为4周
            int maxStale = 60 * 60 * 24 * 28;
            response.newBuilder()
                    .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                    .removeHeader("Pragma")
                    .build();
        }  
         Request originalRequest = chain.request();
        if (UserBean.token == null || alreadyHasAuthorizationHeader(originalRequest)) {//判断token值知否为空和是否变化
            return chain.proceed(originalRequest);
        }
        Request authorised = originalRequest.newBuilder()
                .header("Authorization", UserBean.token)
                .build();  

        return response;
    }
};  

说明:
第一个设置缓存,网络可用时设置缓存超时时间,网络不可用时设置超时时间
最后是一个类似于登录token校验,为登录时,没有token值,登录之后就有token值,比如账号在别的手机登录,token值变了,可用和请求中已经带有验证 header 了,需手动更换token值如果你需要在遇到诸如 401 Not Authorised 的时候进行刷新 token,可以使用 Authenticator,这是一个专门设计用于当验证出现错误的时候,进行询问获取处理的拦截器:

Authenticator mAuthenticator = new Authenticator() {
  @Override public Request authenticate(Route route, Response response)
      throws IOException {
    UserBean.token = service.refreshToken();
    return response.request().newBuilder()
            .addHeader("Authorization", newAccessToken)
            .build();    
  }
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值