@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--