美文网首页
retrofit 网络请求的service

retrofit 网络请求的service

作者: BridgeXD | 来源:发表于2017-04-24 15:51 被阅读0次
    • @Path:所有在网址中的参数(URL的问号前面)请求的相对地址
      请求的相对地址也是需要调用方传递,通过Path注解可以在具体的
      调用场景中动态传递
    • @Query:URL问号后面的参数
    • @QueryMap: 相当于多个@Query
    • @Field:Post请求需要把请求参数放置在请求体中,而非拼接在url
      后面 (使用@Field时记得添加@FormUrlEncoded)
    • @Body:相当于多个@Field,以对象的形式提交

    两种requestBody,一个是FormBody,一个是MultipartBody,前者以表单的方式传递简单的键值对,后者以POST表单的方式上传文件可以携带参数。

    • @FormUrlEncoded:表单的方式传递键值对
      public interface IUserBiz
      {
      @POST("login")
      @FormUrlEncoded
      Call<User> login(@Field("username") String username, @Field("password") String password);
      }

      /省略retrofit的构建代码
      Call<User> call = userBiz.login("zhy", "123");
      //省略call执行相关代码
      
    • @MultipartBody:单文件上传
      public interface IUserBiz
      {
      @Multipart
      @POST("register")
      Call<User> registerUser(@Part MultipartBody.Part photo, @Part("username") RequestBody username, @Part("password") RequestBody password);
      }

      File file = new File(Environment.getExternalStorageDirectory(), "icon.png");
      RequestBody photoRequestBody =RequestBody.create(MediaType.parse("image/png"), file);
      MultipartBody.Part photo =  MultipartBody.Part.createFormData("photos", "icon.png", photoRequestBody);
      Call<User> call = userBiz.registerUser(photo, RequestBody.create(null, "abc"), RequestBody.create(null, "123"));
      

    这里@MultiPart的意思就是允许多个@Part了,我们这里使用了3个@Part,第一个我们准备上传个文件,使用了MultipartBody.Part类型,其余两个均为简单的键值对。

    • PartMap 多文件上传
      public interface IUserBiz
      {
      @Multipart
      @POST("register")
      Call<User> registerUser(@PartMap Map<String, RequestBody> params, @Part("password") RequestBody password);
      }

      File file = new File(Environment.getExternalStorageDirectory(), "messenger_01.png");
      RequestBody photo = RequestBody.create(MediaType.parse("image/png", file);
      Map<String,RequestBody> photos = new HashMap<>();
      photos.put("photos\"; filename=\"icon.png", photo);
      photos.put("username",  RequestBody.create(null, "abc"));
      
      Call<User> call = userBiz.registerUser(photos, RequestBody.create(null, "123"));
      

    GET请求

    -样式4、多个参数在URL问号之后,且个数不确定
    http://102.10.10.132/api/News?newsId={资讯id}&type={类型}...

        @GET("News")
        Call<NewsBean> getItem(@QueryMap Map<String, String> map);
    

    也可以:

      @GET("News")
      Call<NewsBean> getItem(
              @Query("newsId") String newsId,
              @QueryMap Map<String, String> map);
    

    POST请求

    • 样式1、需要补全URL,post的数据只有一条reason
      http://102.10.10.132/api/Comments/{newsId}
      @FormUrlEncoded
      @POST("Comments/{newsId}")
      Call<Comment> reportComment(
      @Path("newsId") String commentId,
      @Field("reason") String reason);

    • 样式2、需要补全URL,问号后加入access_token,post的数据只有一条reason
      http://102.10.10.132/api/Comments/{newsId}?access_token={access_token}
      @FormUrlEncoded
      @POST("Comments/{newsId}")
      Call<Comment> reportComment(
      @Path("newsId") String commentId,
      @Query("access_token") String access_token,
      @Field("reason") String reason);

    • 样式3、需要补全URL,问号后加入access_token,post一个body(对象)
      @POST("Comments/{newsId}")
      Call<Comment> reportComment(
      @Path("newsId") String commentId,
      @Query("access_token") String access_token,
      @Body CommentBean bean);

    参考:http://www.jianshu.com/p/7687365aa946

    相关文章

      网友评论

          本文标题:retrofit 网络请求的service

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