美文网首页
Android - Retrofit注解

Android - Retrofit注解

作者: xlq | 来源:发表于2018-12-30 17:22 被阅读0次

本文将Retrofit注解完全解析一下,好记性不如烂笔头。

注解分类:

Retrofit注解分为三类。分别是Http请求方法注解、标记类注解和参数类注解。

  1. Http请求方式注解8种:GET 、POST、 PUT、 DELETE、 HEAD、 PATCH、 OPTIONS 和HTTP。前7中分别代表Http的请求方法,Http则可以替换以上7中,也可以拓展请求方法。

2.标记类注解3种:FormUrlEncode、Multipart和 Streaming。

  1. 参数类注解10种:Header、Headers、Body、Path、Field、FieldMap、Part、PartMap、Query、QueryMap和URL。

Retrofit初始化:

String url = "http://102.10.10.132/api/";
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

GET请求:

  1. 未带参数的get请求
    http://102.10.10.132/api/news
public interface RetrofitService {
    @GET("news")
    Call<T> getDate();
}
  1. 带一个或多个参数
    http://102.10.10.132/api/news/name
public interface RetrofitService {
    @GET("news")
    Call<T> getDate(@Path("name") String name);
}

http://102.10.10.132/api/news/name/type

public interface RetrofitService {
    @GET("news")
    Call<T> getDate(@Path("name") String name, @Path("type")String type);
}
  1. 一个或多个参数在问号之后

(一个参数)http://102.10.10.132/api/news?name=nameStr

public interface RetrofitService {
    @GET("news")
    Call<T> getDate(@Query("name") String nameStr);
}

(两个参数)http://102.10.10.132/api/news?name=nameStr&type=typeStr

public interface RetrofitService {
    @GET("news")
    Call<T> getDate(@Query("name") String nameStr, @Query("type")String typeStr);
}

(多个参数)http://102.10.10.132/api/news?name=nameStr&type=typeStr...

public interface RetrofitService {
    @GET("news")
    Call<T> getDate(@QueryMap Map<String,String> params);
}

POST请求:

  1. 补全Url,post的数据只有一条reason
    http://102.10.10.132/api/Comment/CommentId
public interface RetrofitService {
    @FormUrlEncoded
    @POST("Comment/{id}")
    Call<T> getDate(@Path("id") String CommentId, @Field("reason") String reason);
}
  1. 补全Url,问号后面加入token,post的数据只有一条reason
    http://102.10.10.132/api/Comment/CommentId?token=token
public interface RetrofitService {
    @FormUrlEncoded
    @POST("Comment/{id}")
    Call<T> getDate(@Path("id") String CommentId,@Query("token") String token, @Field("reason") String reason);
}
  1. 需要补全URL,问号后加入access_token,post一个body(对象)
    http://102.10.10.132/api/Comment/CommentId?token=token
public interface RetrofitService {
    @FormUrlEncoded
    @POST("Comment/{id}")
    Call<T> getDate(@Path("id") String CommentId,@Query("token") String token, @Body T t);
}
  1. 单个文件上传(@Part)
    http://102.10.10.132/api/Comment/
public interface RetrofitService {
    @Multipart
    @POST("Comment")
    Call<T> getDate(@Part MultipartBody.Part photo,@Part("description")RequestBody description);
}

DELETE请求:

  1. 补全Url
    http://102.10.10.132/api/Delete/DeleteId
public interface RetrofitService {
    @DELETE("Delete/{DeleteId}")
    Call<T> getDate(@Path("DeleteId") String CommentId);
}
  1. 补全Url,并在问号后加入token
    http://102.10.10.132/api/Delete/DeleteId?token=token
public interface RetrofitService {
    @DELETE("Delete/{DeleteId}")
    Call<T> getDate(@Path("DeleteId") String CommentId,@Query("token") String token);
}
  1. 带body
    http://102.10.10.132/api/Delete
public interface RetrofitService {
    @HTTP(method = "DELETE",path = "Comments",hasBody = true)
    Call<T> getDate(@Body T t);
}

用自己的理解总结:

  1. Path相当于参数替换
  2. Query相当于Url中问号后面的一个参数,写法:
    http://102.10.10.132/api?CommentId=id
  3. QueryMap相当于Url中问号后面的多个参数,用&连接,写法:
    http://102.10.10.132/api?CommentId=id&CommentName=name&CommentType=type
  4. Field一般用于POST请求中,后面跟要提交的表单,并且添加@ FormUrlEncoded搭配使用。
  5. Body 一般用于POST请求中,相当于多个Field,以对象的方式提交。

相关文章

网友评论

      本文标题:Android - Retrofit注解

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