摘要:上一篇文章講述了的簡(jiǎn)單使用這次我們學(xué)習(xí)一下的各種請(qǐng)求基礎(chǔ)在中使用注解的方式來(lái)區(qū)分請(qǐng)求類型比如表示一個(gè)請(qǐng)求括號(hào)中的內(nèi)容為請(qǐng)求的地址格式含義表示這是一個(gè)請(qǐng)求表示這個(gè)一個(gè)請(qǐng)求表示這是一個(gè)請(qǐng)求表示這是一個(gè)請(qǐng)求表示這是一個(gè)請(qǐng)求表示這是一個(gè)請(qǐng)求表示這是
上一篇文章講述了Retrofit的簡(jiǎn)單使用,這次我們學(xué)習(xí)一下Retrofit的各種HTTP請(qǐng)求.
Retrofit基礎(chǔ)在Retrofit中使用注解的方式來(lái)區(qū)分請(qǐng)求類型.比如@GET("")表示一個(gè)GET請(qǐng)求,括號(hào)中的內(nèi)容為請(qǐng)求的地址.
格式 | 含義 |
---|---|
@GET | 表示這是一個(gè)GET請(qǐng)求 |
@POST | 表示這個(gè)一個(gè)POST請(qǐng)求 |
@PUT | 表示這是一個(gè)PUT請(qǐng)求 |
@DELETE | 表示這是一個(gè)DELETE請(qǐng)求 |
@HEAD | 表示這是一個(gè)HEAD請(qǐng)求 |
@OPTIONS | 表示這是一個(gè)OPTION請(qǐng)求 |
@PATCH | 表示這是一個(gè)PAT請(qǐng)求 |
Retrofit可實(shí)現(xiàn)基本HTTP請(qǐng)求,包括GET,POST,PUT,DELETE等.
1.GET請(qǐng)求
@GET("/record") CallgetResult();
2.POST請(qǐng)求
@POST("/record") CallgetResult();
3.PUT請(qǐng)求
@PUT("/record") CallgetResult();
4.DELETE請(qǐng)求
@DELETE("/record") Call服務(wù)器接口類型getResult();
服務(wù)器接口有很多中,本人經(jīng)驗(yàn)有限,目前接觸較多為以下幾種:
直接請(qǐng)求型即直接對(duì)某一地址或組合某一地址發(fā)起請(qǐng)求
如:對(duì)/result和/result/{id}發(fā)起GET請(qǐng)求,其中{id}中的id在實(shí)際使用時(shí)填寫實(shí)際值即可.
帶參查詢型對(duì)某一地址進(jìn)行帶參查詢請(qǐng)求
如:https://www.baidu.com/s?wd=123為對(duì)接口https://www.baidu.com/s進(jìn)行參數(shù)為wd=123的GET查詢請(qǐng)求.
帶Header型Retrofit中如何寫? 直接請(qǐng)求型即請(qǐng)求時(shí)要求帶上Header
1.如果是直接請(qǐng)求某一地址,寫法如下:
@GET("/record") CallgetResult();
2.如果是組合后直接請(qǐng)求,如/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帶Header型query(@Query("purpose_codes") String codes, @Query("queryDate") String date, @Query("from_station") String from, @Query("to_station") String to)
比如要更新某個(gè)賬戶信息,其接口地址為/info,需要帶的Header有設(shè)備信息device,系統(tǒng)版本version,還要帶請(qǐng)求參數(shù)要更新賬戶的id,代碼如下:
@POST("/info") Call
注:本想給每一種請(qǐng)求添加一個(gè)請(qǐng)求實(shí)例,但是確實(shí)不太好找.
實(shí)例找了很久發(fā)現(xiàn)多說(shuō)提供了一些POST請(qǐng)求接口,下面就以多說(shuō)的接口為例,看一下如何使用Retrofit寫請(qǐng)求.
基礎(chǔ)URL多說(shuō)的接口基礎(chǔ)地址為:http://api.duoshuo.com,則構(gòu)建Retrofit實(shí)例代碼如下:
Retrofit retrofit = new Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create()) .baseUrl("http://api.duoshuo.com") .build();獲取文章評(píng)論、轉(zhuǎn)發(fā)數(shù)
接口地址為:/threads/counts
HTTP請(qǐng)求方式:GET
請(qǐng)求示例為:http://api.duoshuo.com/threads/counts.json?short_name=official&threads=4ff1cbc43ae636b72a00001d
后面的.json為返回?cái)?shù)據(jù)的格式,此處我們使用json格式.
請(qǐng)求代碼如下:
@GET("/threads/counts.json") Call匿名發(fā)表新評(píng)論
接口地址為:/posts/create
HTTP請(qǐng)求方式:POST
請(qǐng)求示例為:
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=匿名發(fā)表新評(píng)論
1.Field方式實(shí)現(xiàn)
@FormUrlEncoded @POST("/posts/create.json") CallcreateCommit(@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實(shí)現(xiàn)方式
@FormUrlEncoded @POST("/posts/create.json") CallcreateCommit(@FieldMap Map map);
獲取Map方式如下:
public class CommitParam { private String short_name; private String author_email; private String author_name; private String thread_id; private String author_url; private String message; public String getShort_name() { return short_name; } public void setShort_name(String short_name) { this.short_name = short_name; } public String getAuthor_email() { return author_email; } public void setAuthor_email(String author_email) { this.author_email = author_email; } public String getAuthor_name() { return author_name; } public void setAuthor_name(String author_name) { this.author_name = author_name; } public String getThread_id() { return thread_id; } public void setThread_id(String thread_id) { this.thread_id = thread_id; } public String getAuthor_url() { return author_url; } public void setAuthor_url(String author_url) { this.author_url = author_url; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public MapcreateCommitParams(){ Map params = new HashMap<>(); params.put("short_name", short_name); params.put("author_email", author_email); params.put("author_name", author_name); params.put("thread_id", thread_id); params.put("author_url", author_url); params.put("message", message); return params; } }
項(xiàng)目地址在此:Dev-Wiki/RetrofitDemo
更多文章請(qǐng)移步我的博客:DevWiki Blog
重要說(shuō)明
想隨時(shí)獲取最新博客文章更新,請(qǐng)關(guān)注公共賬號(hào)DevWiki,或掃描下面的二維碼:
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/65648.html
摘要:公司開源了許多優(yōu)秀的庫(kù),就是其中之一。是用來(lái)簡(jiǎn)化訪問服務(wù)器,如果你的服務(wù)器使用的使,那么趕緊使用吧。官方的文檔是用的說(shuō)明使用過程的,有的童鞋可能從沒用過的比如我,為了簡(jiǎn)單易懂,這里我使用一個(gè)查詢手機(jī)歸屬地的來(lái)說(shuō)明的使用過程。 Square公司開源了許多優(yōu)秀的庫(kù),Retrofit就是其中之一。 Retrofit是用來(lái)簡(jiǎn)化APP訪問服務(wù)器API,如果你的服務(wù)器使用的使RESTAPI,那么趕...
閱讀 1993·2021-11-25 09:43
閱讀 1477·2021-11-22 14:56
閱讀 3339·2021-11-22 09:34
閱讀 2105·2021-11-15 11:37
閱讀 2375·2021-09-01 10:46
閱讀 1464·2019-08-30 15:44
閱讀 2353·2019-08-30 13:15
閱讀 2450·2019-08-29 13:07