1.API 声明 接口中注解方法和和参数表示一条请求如何被处理 2.请求方式 每个请求一定有一个HTTP注解,那个是提供请求方式和相关的URL,一共创建5个注解 get post put delete 和 head。注解中相关的URL资源已经被详细说明 import retrofit2.http.Body; import retrofit2.http.Field; import retrofit2.http.FormUrlEncoded; import retrofit2.http.GET; import retrofit2.http.Header; import retrofit2.http.Headers; import retrofit2.http.Multipart; import retrofit2.http.POST; import retrofit2.http.PUT; import retrofit2.http.Part; import retrofit2.http.Path; import retrofit2.http.Query; import retrofit2.http.QueryMap; @Get("users/list") 在url中,你也可以指明请求的参数 @GET("users/list?sort=desc") URL操作 在方法中,请求URL能被动态更新,用替换blocks和参数。替换块是被{}包围的字母-数字的字符 串,对应的参数一定要使用@Path加相同的参数标志 @GET("group/{id}/users") Call<List<User>> groupList(@Path(id) int groupId); 查询的参数也可以被加添 @GET("group/{id}/user") Call<List<User>> getList(@Path(id) int groupId,@Query("sort") String sort); 对于复杂的查询参数整合成Map使用 @GET("group/{id}/users") Call<List<User>> groupList(@Path("id") int groupId,@QueryMap(Map<String,String> options)); 查询体(Body) 使用@Body注解,一个对象可以被指定用于HTTP请求的body,使用retrofit实 例上的指定转换器可以将对象转换,如果转换器没有被添加,仅 requestBody能被使用 @POST("users/new") Call<User> createUser(@Body User user); 表单编码和多媒体格式 API也可以声明去发送表单编码和多媒体数据 当方法中出现@FormUrlEncoded,Form-encoded数据可以被发送 每个key-vaule对用@Field被注解 @FormUrlEncoded @POST("user/edit") Call<User> updateUser(@Field("first_name") String first ,@Field("last_name") String last); 当@Multipart注解在方法中出现,多媒体请求被使用,Parts使用@part注解声明 @Multipart @PUT("user/photo") Call<User> updateUser(@Part("photo") RequestBody photo,@Part("description" ) RequestBody description); Header操作 使用@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); 注意信息头不能相互覆盖,所有的信息头,所有的信息name都将在请求中被包含 一个请求头能被动态更新使用@Header注解,对应的参数必须提供给@Header,如果值为null 信息头将被省略,否则,值将被toString回调,结果被使用 @GET("user") Call<User> getUser(@Header("Authorization") String authorization) 使用OKhttp拦截器 ,Headers 需要被添加到每个被声明的请求 同步VS异步 Call实例可以被同步、异步执行,每个实例仅能被使用一次,但是调用 clone() 将创建一个新的实例可以被使用 在Android系统,在主线程执行callbacks,在jvm,callbacks将出现在httpRequest相同的线程 Retrofit配置 Retrofit 是通过API接口的类转化为可调用对象,默认方式,retrofit将给予你清晰的默认方式为你平台,也可以自定义 Converters 默认方式,retrofit仅能并行化HTTPbodies到OKhttp responsebody类型 @Body 它也支持 requestbody 类型 转换器可以添加支持其他类型,为你提供有六种流行模式序列化jar 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 MAVEN <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>retrofit</artifactId> <version>(insert latest version)</version> </dependency> GRADLE compile 'com.squareup.retrofit2:retrofit:(insert latest version)' 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