@Get
@GET("path")
Call<String> get();
GET /path HTTP/1.1
Host: www.baidu.com
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.9.1
@Post
@POST("path")
Call<String> post();
POST /path HTTP/1.1
Content-Length: 0
Host: www.baidu.com
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.9.1
@Path
@POST("path/{path}")
Call<String> path(@Path("path") String path);
POST /path/path_value HTTP/1.1
Content-Length: 0
Host: www.baidu.com
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.9.1
@Header
@GET("path")
Call<String> head(@Header("header_key")String header_value);
GET /path HTTP/1.1
header_key: header_value
Host: www.baidu.com
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.9.1
@Query
@GET("path")
Call<String> query(@Query("query")String query);
GET /path?query=query_value HTTP/1.1
Host: www.baidu.com
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.9.1
@QueryMap
@GET("path")
Call<String> queryMap(@QueryMap Map<String,String> map);
GET /path?query_key=query_value HTTP/1.1
Host: www.baidu.com
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.9.1
@Field
@FormUrlEncoded
@POST("path")
Call<String> formUrlEncoded_filed(@Field("filed")String filed);
POST /path HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 17
Host: www.baidu.com
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.9.1
filed=filed_value
@Part##
@Multipart
@POST("path")
Call<String> part(@Part("part") String part);
POST /path HTTP/1.1
Content-Type: multipart/form-data; boundary=c15df5c5-4a97-43b4-acae-998e3a89a075
Content-Length: 245
Host: www.baidu.com
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.9.1
--c15df5c5-4a97-43b4-acae-998e3a89a075
Content-Disposition: form-data; name="part"
Content-Transfer-Encoding: binary
Content-Type: application/json; charset=UTF-8
Content-Length: 12
"part_value"
--c15df5c5-4a97-43b4-acae-998e3a89a075--
@Body
@POST("path")
Call<String> body(@Body User body);
POST /path HTTP/1.1
Content-Type: application/json; charset=UTF-8
Content-Length: 27
Host: www.baidu.com
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.9.1
{"age":25,"username":"dog"}
为什么要搞这个##
因为在使用
@Part
提交表单的时候遇到了一个问题。
请问为什么Retrofit以Mutipart上传参数时,String参数会多一对双引号
在
Post
请求中,@Part
注解默认的Content-Type
类型是“application/json”,
造成的主要原因有两个:
- 1、retrofit并不内置String的Converter,只有在Url、Header、普通表单字段相关的注解才会默认处理成String。
- 2、你注册了GsonConverter,而GsonConverter是不会判断能不能处理该类型的,全部转成json,而String在json里就是 "String"的形式,所以长度变成5,Content-Type头是application/json; charset=UTF-8
解决方法
retrofit = new Retrofit.Builder()
.baseUrl("http://www.baidu.com/")
// .addConverterFactory(GsonConverterFactory.create())
.client(client)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
我终于知道为什么以前看的框架会添加两个
ConverterFactory
并不是作者瞎搞,而是我一无所知。。
也就是在GsonConverterFactory.create()
之前,添加一个ScalarsConverterFactory.create()
这样,使用@Part
请求的报文的Content-Type
就不会是application/json
@Multipart
@POST("path")
Call<String> part(@Part("part") String part);
POST /path HTTP/1.1
Content-Type: multipart/form-data; boundary=36f57677-f08c-4a5f-8000-104fa606d580
Content-Length: 237
Host: www.baidu.com
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.9.1
--36f57677-f08c-4a5f-8000-104fa606d580
Content-Disposition: form-data; name="part"
Content-Transfer-Encoding: binary
Content-Type: text/plain; charset=UTF-8
Content-Length: 10
part_value
--36f57677-f08c-4a5f-8000-104fa606d580--