Retrofit2.0参数请求、图文上传

发布时间:2024-01-13 02:35:01
图文上传

RequestBody

 @POST("uploadFile")
Observable<BaseResponseBean<UploadFileBean>> uploadFile(@Body RequestBody body);

File file = new File(path);
MultipartBody uploadBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file))
                .addFormDataPart("description", "这是一张生活照")
                .addFormDataPart("source", "mobile")
                .build();
apiService.uploadFile(uploadBody)
                .compose(RxSchedulersHelper.io_main());               

Multipart

@Multipart
@POST(ConstantsNetInterface.Url.PIC_SINGLE_UPLOAD)
Observable<BaseResponseBean<UploadFileBean>> uploadFile1(
				@Part("description") RequestBody descriptionBody,
                @Part("source") RequestBody sourceBody,
                @Part() MultipartBody.Part filePart);  

File file = new File(path);
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
RequestBody descriptionBody= RequestBody.create(MediaType.parse("text/plain"), "这是一张生活照");
RequestBody sourceBody = RequestBody.create(MediaType.parse("text/plain"), "mobile");

apiService.uploadFile1(descriptionBody, sourceBody,  body)
                .compose(RxSchedulersHelper.io_main());

@Query、@QueryMap 一般是 @Get标记的,URL后面跟的参数请求 ?key=value&key1=value1

@Field、@FieldMap 一般是@Post 、@FormUrlEncoded标记的,表单请求

@Part、@PartMap 一般是@Post、 @Multipart标记的,带文件的表单请求

@Body标记的请求体,支持自定义Bean类,但不能标记为 @FormUrlEncoded / @Multipart

public interface NetService {
 
 
    @GET("?")
    Call<WeatherBeans> requestWeather(@Query("app") String app, @Query("weaid") int weaid, @Query("appkey") int appkey, @Query("sign") String sign, @Query("format") String format);
 
 
    @GET("?")
    Call<WeatherBeans> requestWeather(@QueryMap Map<String, String> paramas);
 
    @GET
    Call<Movies> requestMovies(@Url String url);
 
    @POST("/")
    @FormUrlEncoded
    Call<WeatherBeans> requestWeatherBeans(
            @Field("app") String app,
            @Field("weaid") String weaid,
            @Field("appkey") String appkey,
            @Field("sign") String sign,
            @Field("format") String format);
 
    @POST("/")
    @FormUrlEncoded
    Call<WeatherBeans> requestWeatherBeans(@FieldMap Map<String, String> fields);
 
 
 
    @POST("/")
    Call<WeatherBeans> requestWeatherBeans(@Body RequestParams parama);
 
    //任意定义的bean类
    class RequestParams {
        public String app;
        public int weaid;
        public int appkey;
        public String sign;
        public String format;
    }
 
    @Headers({"Content-Type: application/json", "Cache-Control: max-age=360000"})
    @HTTP(method = "GET", path = "?app=weather.future&weaid=1&&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json", hasBody = false)
    Call<WeatherBeans> requestWeatherBeansByHeaders();
 
    @GET("login")
    Call<UserInfo> login(@Header("Authorization") String authorization);
 
    @HTTP(method = "POST", path = "?", hasBody = true)
    @FormUrlEncoded
    Call<WeatherBeans> requestWeatherBeansByHttpPost(@FieldMap Map<String, String> paramas);
 
 	//上传图文
    @Multipart
    @POST("web/shrink")
    Call<ResponseBody> uploadFile(@Part("description") RequestBody description, @Part MultipartBody.Part file);
 
    @Multipart
    @POST("web/shrink")
    Call<ResponseBody> uploadFile(@PartMap Map<String, RequestBody> params);
 
    @GET("back_pic/03/70/72/5257b6c12d89875.jpg!r850/fw/{id}")
    Call<ResponseBody> getImage(@Path("id") int id);
 
}

参考:
Retrofit 注解学习 @GET @POST @Query @QueryMap @Field @FieldMap @Body …

文章来源:https://blog.csdn.net/ZHENZHEN9310/article/details/89181552
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。