Android Retrofit + RxJava使用详解

1.Retrofit基本使用

首先来了解下Retrofit是什么,在官网中对于Retrofit的描述是这样的:

A type-safe HTTP client for Android and Java.

适用于Android和Java的类型安全的HTTP客户端。

可以理解成一个封装好的网络请求库。

Retrofit GitHub地址

接下来学习一下Retrofit的基本使用方法:

在app根目录的build.gradle文件中加入依赖:

compile 'com.squareup.retrofit2:retrofit:2.3.0'

创建实体类:

我们使用http://www.kuaidi100.com/query?type=yuantong&postid=11111111111这个快递接口来测试,根据返回数据创建实体类,类名为PostInfo,在这里就不贴代码了,可以在文末下载Demo查看代码。

定义请求参数接口:

public interface RetrofitService {

    /**
     * 获取快递信息
     *
     * @param type   快递类型
     * @param postid 快递单号
     * @return Call<PostInfo>
     */
    @GET("query")
    Call<PostInfo> getPostInfo(@Query("type") String type, @Query("postid") String postid);
}

定义一个接口RetrofitService,和普通的接口类相同,在里面定义一个方法getPostInfo,可以看到这个方法使用了@GET(“query”)注解,代表这是一个GET请求,请求接口为query(完整请求地址中域名/到?之间的字段),方法返回值为Call实体,这个稍后会用于具体的网络请求,PostInfo就是我们刚刚定义的实体类。参数中也使用了注解@Query,用于拼接传入的字段(type=yuantong&postid=11111111111)。

网络请求:

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://www.kuaidi100.com/")
        .addConverterFactory(GsonConverterFactory.create())
        .build();

RetrofitService service = retrofit.create(RetrofitService.class);
Call<PostInfo> call = service.getPostInfo("yuantong", "11111111111");
call.enqueue(new Callback<PostInfo>() {
    @Override
    public void onResponse(Call<PostInfo> call, Response<PostInfo> response) {
        Log.i("http返回:", response.body().toString() + "");
    }

    @Override
    public void onFailure(Call<PostInfo> call, Throwable t) {

    }
});

可以看到Retrofit使用了Builder模式,首先传入baseUrl,由于返回的数据是json类型的,还需要添加转换工厂类,这需要在build.gradle文件中加入依赖:

compile 'com.squareup.retrofit2:converter-gson:2.3.0'

然后调用Retrofit的create方法创建RetrofitService的实体类,得到实体类之后就可以调用其中的getPostInfo方法并传入参数,getPostInfo方法会返回一个Call实体类,接着调用Call的enqueue方法,传入Callback回调,重写onResponse(请求成功回调)和onFailure(请求失败回调)方法,response参数中包含了请求结果中的所有信息(body、code、headers等)。

OK,到这里Retrofit的基本用法就讲完了,接下来我们来了解一下Retrofit的其他用法。

2.Retrofit更多用法

请求方法

在RetrofitService的getPostInfo方法中,我们使用了@GET注解,说明这是一个GET方法,当然也可以写成HTTP协议中的其他请求方法(比如POST、PUT、DELETE、HEAD等)。

请求参数

在getPostInfo方法的参数中使用了@Query注解,除此之外还可以使用@QueryMap、@Path、@Body、@FormUrlEncoded/@Field、@Multipart/@Part、@Header/@Headers。

  • @Query()
@GET("query")
Call<PostInfo> getPostInfo(@Query("type") String type, @Query("postid") String postid);

相当于

@GET(query?type=type&postid=postid)
Call<PostInfo> getPostInfo(@Query("type") String type, @Query("postid") String postid);
  • @QueryMap

当参数很多的时候可以使用Map集合:

@GET("query")
Call<Book> getSearchBook(@QueryMap Map<String, String> parameters);
  • @Path

用于替换url中的某些字段,当url中字段不确定时可以使用:

@GET("group/{id}/users")
Call<List<User>> groupList(@Path("id") int groupId);

id可以为任意字段,需要和@Path(“id”)中的字段保持一致,如果需要请求参数,也可以使用@Query拼接:

@GET("group/{id}/users")
Call<List<User>> groupList(@Path("id") int groupId, @Query("sort") String sort);
  • @Body

可以使用实体类作为请求体,Retrofit会帮我们自动转换:

@POST("users/new")
Call<User> createUser(@Body User user);
  • @FormUrlEncoded/@Field

用于传送表单数据,注意在头部需要加上@FormUrlEncoded,first_name代表key,first代表value:

@FormUrlEncoded
@POST("user/edit")
Call<User> updateUser(@Field("first_name") String first, @Field("last_name") String last);
  • @Multipart/@Part

用于上传文件:

@Multipart
@PUT("user/photo")
Call<User> updateUser(@Part("photo") RequestBody photo, @Part("description") RequestBody description);
  • @Header/@Headers

用于设置请求头:

@GET("user")
Call<User> getUser(@Header("Authorization") String authorization)

也可以通过@Headers设置:

@Headers("Cache-Control: max-age=640000")
@GET("widget/list")
Call<List<Widget>> widgetList();
@Headers({
    "Accept: application/vnd.github.v3.full+json",
    "User-Agent: Retrofit-Sample-App"
})
@GET("users/{username}")
Call<User> getUser(@Path("username") String username);

转换器

Retrofit支持的序列化库:

  • 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

3.Retrofit + RxJava结合使用

如果你对RxJava还不太了解,可以看下这篇文章《给 Android 开发者的 RxJava 详解》

首先在build.gradle文件中加入依赖:

compile 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
compile 'io.reactivex:rxandroid:1.2.1'

将RetrofitService类中getPostInfo方法的返回值修改为Observable(被观察者):

public interface RetrofitService {

    /**
     * 获取快递信息
     * Rx方式
     *
     * @param type   快递类型
     * @param postid 快递单号
     * @return Observable<PostInfo>
     */
    @GET("query")
    Observable<PostInfo> getPostInfoRx(@Query("type") String type, @Query("postid") String postid);
}

在创建Retrofit时添加RxJava支持:

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://www.kuaidi100.com/")
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) // 支持RxJava
        .build();

获取被观察者:

RetrofitService service = retrofit.create(RetrofitService.class);
Observable<PostInfo> observable = service.getPostInfoRx("yuantong", "11111111111");

订阅:

observable.subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
        .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
        .subscribe(new Observer<PostInfo>() { // 订阅

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(PostInfo postInfo) {
                Log.i("http返回:", postInfo.toString() + "");
            }
        });

在RxJava中,由于链式调用的影响,是被观察者订阅观察者。

到这里Retrofit + RxJava结合使用就讲完了,由于Retrofit、RxJava属于同门师兄弟,结合使用还是很容易的。

4.Retrofit打印请求参数

Retrofit中无法打印请求参数,由于Retrofit是基于OkHttp进行封装的,可以对OkHttp添加日志拦截器来打印请求参数:

在build.gradle文件中加入依赖:

compile 'com.squareup.okhttp3:logging-interceptor:3.8.0'

OkHttp添加拦截器:

public class RetrofitUtils {

    /**
     * 获取OkHttpClient
     * 用于打印请求参数
     *
     * @return OkHttpClient
     */
    public static OkHttpClient getOkHttpClient() {
        // 日志显示级别
        HttpLoggingInterceptor.Level level = HttpLoggingInterceptor.Level.BODY;
        // 新建log拦截器
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.i("Http请求参数:", message);
            }
        });
        loggingInterceptor.setLevel(level);
        // 定制OkHttp
        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
        // OkHttp进行添加拦截器loggingInterceptor
        httpClientBuilder.addInterceptor(loggingInterceptor);
        return httpClientBuilder.build();
    }
}

将定制的OkHttpClient添加到Retrofit中:

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(Constant.SERVER_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .client(RetrofitUtils.getOkHttpClient()) // 打印请求参数
        .build();

大功告成!

5.写在最后

源码已托管到GitHub上,欢迎Fork,觉得还不错就Start一下吧!

GitHub传送门

点击下载源码

欢迎同学们吐槽评论,如果你觉得本篇博客对你有用,那么就留个言或者顶一下吧(^-^)

在下篇文章中将会探讨一下Retrofit封装的最佳姿势,敬请期待!

《Android 探讨一下Retrofit封装的最佳姿势》

已修改为支持RxJava2,最新代码已同步至GitHub。

compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.8.0'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
发布了65 篇原创文章 · 获赞 285 · 访问量 28万+

猜你喜欢

转载自blog.csdn.net/kong_gu_you_lan/article/details/73504345