依赖
"com.squareup.retrofit2:retrofit:$retorfit_version"
"com.squareup.retrofit2:converter-gson:$retorfit_jackson_version"
import android.database.Observable; import java.util.Map; import okhttp3.ResponseBody; import retrofit2.Call; import retrofit2.http.Body; import retrofit2.http.DELETE; import retrofit2.http.Field; import retrofit2.http.FieldMap; import retrofit2.http.FormUrlEncoded; import retrofit2.http.GET; import retrofit2.http.POST; import retrofit2.http.PUT; import retrofit2.http.Path; import retrofit2.http.Query; import retrofit2.http.QueryMap; /** * by li 2020/7/27 * 接口书拼接类 * 该处的 ResponseBody 可以写成自己的封装类 * 使用RetorfitJackson 直接转化成自己的封装类 */ public interface ApiService { //GET 相关请求 ********************************************************************************* /** * 原Url (不含 & 和 ? 参数) 单纯请求数据极少使用 * eg: http://test.orangelive.tv/v1/getUserGoodsInfo * @return */ @GET("getUserGoodsInfo") Call<ResponseBody> getMovieList(); /** * 原Url (含 / 参数) 如那一页的数据请求 * eg: http://test.orangelive.tv/v1/getUserGoodsInfo/11111 * 获取 getUserGoodsInfo 的 11111 页数据 * eg: http://test.orangelive.tv/v1/getUserGoodsInfo/11111/5 * @GET("getUserGoodsInfo{page}/{type}") * 叠加 getMovieList(@Path("page") String page,@Path("type") String type ); * @param page * @return */ @GET("getUserGoodsInfo{page}") Call<ResponseBody> getMovieList(@Path("page") String page ); /** * 原Url (含 ? 参数) * eg: http://test.orangelive.tv/v1/getUserGoodsInfo?movieId=1 * 原Url (含 ? 和 & 参数) * eg: http://test.orangelive.tv/v1/getUserGoodsInfo?movieId=1&type=2 * 叠加 getMovieList(@Query("movieId") String movieId, @Query("type") int type); * @param movieId * @return */ @GET("getUserGoodsInfo") Call<ResponseBody> getMovieListT(@Query("movieId") String movieId); /** * 原Url (含多个 & 参数) * eg: http://test.orangelive.tv/v1/getUserGoodsInfo? clientID=xx&mac=xxx&sn=xx&time=xxx" + * "&sign=xxx&ckey=xxx"; * 多参不确定类型 * @param map * @return */ @GET("getUserGoodsInfo?") Call<ResponseBody> getMovieList(@QueryMap Map<String ,Object> map); //post 相关请求********************************************************************************* /** * 原Url (不含 & 和 ? 参数) 单纯请求数据极少使用 表单不进行url拼接 只左数据传递 * eg: http://test.orangelive.tv/v1/getUserGoodsInfo/11111 * 由于表单是不拼接在连接的 只是负责传递内容 * 使用@FieId 传入对应的键值对即可 单个参数表单 * 由于本连接还需要带一个拼接参 所以需要多一个@Path 属性传入拼接值 * @return */ @FormUrlEncoded @POST("getUserGoodsInfo/{page}") Call<ResponseBody> reportComment(@Path("page") String newsId,@Field("category")String session); /** * 原Url (不含 & 和 ? 参数) 单纯请求数据极少使用 * eg: http://test.orangelive.tv/v1/getUserGoodsInfo/ * 多参表单传递 * @param map * @return */ @POST("getUserGoodsInfo") @FormUrlEncoded Call<ResponseBody> testFormUrlEncoded2(@FieldMap Map<String, Object> map); /** * 原Url (含? 拼接参数) * eg: http://test.orangelive.tv/v1/getUserGoodsInfo?access_token="134566" * @param access_token * @param reason * @return */ @FormUrlEncoded @POST("getUserGoodsInfo") Call<ResponseBody> reportQueryComment(@Query("access_token") String access_token,@Field("reason") String reason); /** * 原Url (含? 拼接参数) @Body传递类对象 * @param commentId * @param access_token * @param bean * @return */ @POST("getUserGoodsInfo/{newsId}") Call<ResponseBody> reportComment(@Path("newsId") String commentId, @Query("access_token") String access_token,@Body HttpFromBean bean); //相关总结 如果链接拼接出现? 使用@Query 出现/ 使用@Path //DELECT 相关请求****************************************************************************** /** * 原Url (含/ 拼接参数) * @param commentId * @return */ @DELETE("getUserGoodsInfo/{commentId}") Call<ResponseBody> deleteNewsCommentFromAccount(@Path("commentId") String commentId); /** * 原Url (含/ 和 ? 拼接参数) * @param accountId * @param access_token * @return */ @DELETE("getUserGoodsInfo/{commentId}") Call<ResponseBody> deleteNewsCommentFromAccount(@Path("accountId")String accountId,@Query("access_token")String access_token); //PUT 相关请求********************************************************************************* /** * 基本使用方法跟上面的类似 * @param accountId * @param access_token * @param bean * @return */ @PUT("getUserGoodsInfo/{accountId}") Call updateExtras(@Path("accountId")String accountId, @Query("access_token")String access_token, @Body HttpFromBean bean); }
相关调用(其他的调用也类似,仿着写就行)
ApiService service = new Retrofit.Builder() .baseUrl(apkurlpath) .build() .create(ApiService.class); Map<String, Object> urlMap = new HashMap<>(); urlMap.put("clientID","100d855909423719841"); service.getMovieList(urlMap).enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { try { requestJson = response.body().string(); Log.w("data","request json ="+requestJson); } catch (IOException e) { Log.w("data","IOException "); e.printStackTrace(); } } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { Log.w("data","onFailure"); t.printStackTrace(); } });