Retrofit使用总结

Retrofit配置:

在module的build.xml中配置 compile ‘com.squareup.retrofit2:retrofit:2.0.2’

使用方法:
可以使用GET、POST请求。

1、GET请求json方式:

@GET(“User/userlogin.ashx”)
Call< ResponseBody> queryUserBean(@Query(“acc”) String acc, @Query(“pwd”) String pwd, @Query(“client”) String client);

2、POST请求json方式:

@POST(“User/userlogin.ashx”)
Call < ResponseBody> queryUserBean(@Query(“acc”) String acc, @Query(“pwd”) String pwd, @Query(“client”) String client);

3、另外请求的路径拼接根据不同情况有多种方法:

// @Path注解可以动态替换url中的部分拼接字符
@GET(“users/{user}/repos”)
Call < List< Repo>> listRepos(@Path(“user”) String user);

参数也可以直接写到URL中:

@GET(“users/list?sort=desc”)

用表单形式提交参数:

@FormUrlEncoded
@POST(“user/Feedback.ashx”)
Call< ResponseBody> submitFeedBack(@Field(“uid”) String uid, @Field(“feedback”) String feedback, @Field(“client”) String client);

上传图片和参数:

Map< String, RequestBody> map = new HashMap<>();
RequestBody requestBody1 = >RequestBody.create(MediaType.parse(“text/plain”), (id + “”));
map.put(“uid”, requestBody1);
RequestBody requestBody2 = RequestBody.create(MediaType.parse(“text/plain”), “2”);
map.put(“client”, requestBody2);
RequestBody requestBody3 = ?>RequestBody.create(MediaType.parse(“image/png”), file);
map.put(“photo\”; filename=\”” + file.getName() + “”, requestBody3);
在service中调用
@Multipart
@POST(“user/SetAvatar.ashx”)
Call< ResponseBody> uploadImage(@PartMap Map< String, RequestBody> pamams);
上传图片
@Multipart
@PUT(“user/photo”)
Call< ResponseBody> updateUser(@Part(“photo”) RequestBody photo, @Part(“description”) RequestBody description);

更多用法参考
http://square.github.io/retrofit/#api-declaration

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值