美文网首页
Retrofit 基本用法

Retrofit 基本用法

作者: shpunishment | 来源:发表于2018-03-03 22:54 被阅读0次

app/build.gradle添加依赖

    implementation 'com.squareup.retrofit2:retrofit:2.3.0'
    implementation 'com.squareup.okhttp3:okhttp:3.10.0'
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
  1. 根据json文件创建 用于接收服务器返回数据 的类
  2. 创建 用于描述网络请求 的接口
public interface BookSearch_interface {
    // @Query 用于 @GET 方法的查询参数(Query = Url 中 ‘?’ 后面的 key-value)
    @GET("book/search")
    Call<BookSearchResponse> getSearchBooks(@Query("q") String name,
          @Query("tag") String tag, @Query("start") int start,
          @Query("count") int count);

}
  1. 完成请求操作
private void requestBooks(){

        // 创建Retrofit对象
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("https://api.douban.com/v2/")
                //设置使用Gson解析
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        // 创建 网络请求接口 的实例
        BookSearch_interface service=retrofit.create(BookSearch_interface.class);

        //对 发送请求 进行封装
        Call<BookSearchResponse> call=service.getSearchBooks("小王子", "", 0, 3);

        // 异步 发送网络请求
        call.enqueue(new Callback<BookSearchResponse>() {
            @Override
            public void onResponse(Call<BookSearchResponse> call, Response<BookSearchResponse> response) {
                Log.d(TAG, "onResponse");
                //Log.d(TAG,response.body().toString());

                for(int i=0;i<response.body().books.size();i++){
                    if(i==0){
                        Log.d(TAG,"count : "+response.body().count);
                        Log.d(TAG,"start : "+response.body().start);
                        Log.d(TAG,"total : "+response.body().total);
                    }
                    Log.d(TAG,"books author : "+response.body().books.get(i).author[0]);
                    Log.d(TAG,"books origin_title : "+response.body().books.get(i).origin_title);
                    Log.d(TAG,"books pubdate : "+response.body().books.get(i).pubdate);
                }

            }

            @Override
            public void onFailure(Call<BookSearchResponse> call, Throwable t) {
                Log.d(TAG,"onFailure");
                t.printStackTrace();
            }
        });
    }
  1. 查看结果
03-03 14:38:18.046 9985-9985/com.shpun.retrofitokhttprxjavatest D/MainActivity: onResponse
03-03 14:38:18.046 9985-9985/com.shpun.retrofitokhttprxjavatest D/MainActivity: count : 3
03-03 14:38:18.046 9985-9985/com.shpun.retrofitokhttprxjavatest D/MainActivity: start : 0
03-03 14:38:18.046 9985-9985/com.shpun.retrofitokhttprxjavatest D/MainActivity: total : 849
03-03 14:38:18.046 9985-9985/com.shpun.retrofitokhttprxjavatest D/MainActivity: books author : [法] 圣埃克苏佩里
03-03 14:38:18.046 9985-9985/com.shpun.retrofitokhttprxjavatest D/MainActivity: books origin_title : Le Petit Prince
03-03 14:38:18.046 9985-9985/com.shpun.retrofitokhttprxjavatest D/MainActivity: books pubdate : 2003-8
03-03 14:38:18.046 9985-9985/com.shpun.retrofitokhttprxjavatest D/MainActivity: books author : (法)圣埃克苏佩里
03-03 14:38:18.046 9985-9985/com.shpun.retrofitokhttprxjavatest D/MainActivity: books origin_title : 
03-03 14:38:18.046 9985-9985/com.shpun.retrofitokhttprxjavatest D/MainActivity: books pubdate : 2000-9-1
03-03 14:38:18.046 9985-9985/com.shpun.retrofitokhttprxjavatest D/MainActivity: books author : 圣艾修伯里
03-03 14:38:18.047 9985-9985/com.shpun.retrofitokhttprxjavatest D/MainActivity: books origin_title : 
03-03 14:38:18.047 9985-9985/com.shpun.retrofitokhttprxjavatest D/MainActivity: books pubdate : 2006-1

以上是GET的用法,POST的要在定义接口时使用注解@FormUrlEncoded
@POST("")

@Field
其他使用方法则类似


参考博客
Retrofit 2.0 使用教程
Retrofit用法详解

相关文章

网友评论

      本文标题:Retrofit 基本用法

      本文链接:https://www.haomeiwen.com/subject/wtvxfftx.html