android Retrofit

RESTful POJO <-> JSON

github retrofit
怪盗kidou:你真的会用Retrofit2吗?Retrofit2完全教程
鸿洋_:Retrofit2 完全解析 探索与okhttp之间的关系

配置


dependencies {
    compile 'com.google.code.gson:gson:2.3'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'  
    compile 'com.squareup.okhttp:okhttp:2.4.0'
    
    compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
}


//解析库配置
Gson	    com.squareup.retrofit:converter-gson:2.0.0-beta2
Jackson	    com.squareup.retrofit:converter-jackson:2.0.0-beta1
Moshi	    com.squareup.retrofit:converter-moshi:2.0.0-beta1
Protobuf	com.squareup.retrofit:converter-protobuf:2.0.0-beta1
Wire	    com.squareup.retrofit:converter-wire:2.0.0-beta1
Simple XML	com.squareup.retrofit:converter-simplexml:2.0.0-beta1

//PROGUARD
# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on RoboVM on iOS. Will not be used at runtime.
-dontnote retrofit2.Platform$IOS$MainThreadExecutor
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions

使用


//1,定义业务请求接口
public interface GitHubService {
  @GET("users/{user}")
  Call<ResponseBody> repo(@Path("user") String user);
}

//2,需要创建Retrofit实例,并完成相应的配置
Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.github.com/")  //baseUlr必须以/(斜线)结束
    .addConverterFactory(GsonConverterFactory.create())
    //.addCallAdapterFactory(RxJavaCallAdapterFactory.create())  //RxJava
    .build();

GitHubService service = retrofit.create(GitHubService.class);

//3,调用请求方法,并得到Call实例
Call<ResponseBody> call = service.repo("octocat");


//同步
ResponseBody repo = call.execute();
//异步
call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    System.out.println(response.body().string());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                t.printStackTrace();
            }
        });

//取消请求
call.cancel();

+RxJava


//1,接口设计:

public interface BlogService {
  @POST("/blog")
  Observable<Result<List<Blog>>> getBlogs();
}



//
//2,通过RxJavaCallAdapterFactory为Retrofit添加RxJava支持:

Retrofit retrofit = new Retrofit.Builder()
      .baseUrl("http://localhost:4567/")
      .addConverterFactory(GsonConverterFactory.create())
      .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
      .build();


//
//3,使用:

BlogService service = retrofit.create(BlogService.class);
service.getBlogs()
  .subscribeOn(Schedulers.io())
  .subscribe(new Subscriber<Result<List<Blog>>>() {
      @Override
      public void onCompleted() {
        System.out.println("onCompleted");
      }

      @Override
      public void onError(Throwable e) {
        System.err.println("onError");
      }

      @Override
      public void onNext(Result<List<Blog>> blogsResult) {
        System.out.println(blogsResult);
      }
  });
    

方法级注解

标记注解

参数级注解

Retrofit.Builder

Converter

Retrofit的Url组合规则