Retrofit get post query filed FiledMap
直接请求型
1.如果是直接请求某一地址,写法如下:
@GET(“/record”)
Call getResult();
2.如果是组合后直接请求,如/result/{id}写法如下:
@GET(“/result/{id}”)
Call getResult(@Path(“id”) String id);
带参查询型
如12306的查询接口https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes=ADULT&queryDate=2016-03-18&from_station=BJP&to_station=CDW,写法如下:
@GET(“/otn/lcxxcx/query”)
Call query(@Query(“purpose_codes”) String codes, @Query(“queryDate”) String date,
@Query(“from_station”) String from, @Query(“to_station”) String to)
带Header型
比如要更新某个账户信息,其接口地址为/info,需要带的Header有设备信息device,系统版本version,还要带请求参数要更新账户的id,代码如下:
@POST(“/info”)
Call updateInfo(@Header(“device”) String device, @Header(“version”) int version,
@Field(“id”) String id);
HTTP请求方式:POST
请求示例为:
Request URL:http://api.duoshuo.com/posts/create.json
Request Method:POST
Post Data:short_name=official&author_email=jp.chenyang%40gmail.com&author_name=Perchouli&thread_id=1152923703638301959&author_url=http%3A%2F%2Fduoshuo.com&message=匿名发表新评论
1.Field方式实现
@FormUrlEncoded
@POST(“/posts/create.json”)
Call createCommit(@Field(“secret”) String secret,
@Field(“short_name”) String shortName,
@Field(“author_email”) String authorEmail,
@Field(“author_name”) String authorName,
@Field(“thread_key”) String threadKey,
@Field(“author_url”) String author_url,
@Field(“message”) String message);
2.Field Map实现方式
@FormUrlEncoded
@POST(“/posts/create.json”)
Call createCommit(@FieldMap Map