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
