{"id":1720,"date":"2016-09-02T10:00:21","date_gmt":"2016-09-02T02:00:21","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=1720"},"modified":"2016-09-02T10:29:50","modified_gmt":"2016-09-02T02:29:50","slug":"retrofit%e5%9f%ba%e7%a1%80http%e8%af%b7%e6%b1%82","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=1720","title":{"rendered":"Retrofit\u57fa\u7840Http\u8bf7\u6c42"},"content":{"rendered":"<h2>\u7b80\u4ecb<\/h2>\n<p>Retrofit\u662fSquare\u5f00 \u53d1\u7684\u4e00\u4e2aAndroid\u548cJava\u7684REST\u5ba2\u6237\u7aef\u5e93\u3002\u8fd9\u4e2a\u5e93\u975e\u5e38\u7b80\u5355\u5e76\u4e14\u5177\u6709\u5f88\u591a\u7279\u6027\uff0c\u76f8\u6bd4\u5176\u4ed6\u7684\u7f51\u7edc\u5e93\uff0c\u66f4\u5bb9\u6613\u8ba9\u521d\u5b66\u8005\u5feb\u901f\u638c\u63e1\u3002\u5b83\u53ef\u4ee5\u5904\u7406GET\u3001 POST\u3001PUT\u3001DELETE&#8230;\u7b49\u8bf7\u6c42<\/p>\n<h2>\u6982\u8ff0<\/h2>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/09\/retrofit_bs.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-1727\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/09\/retrofit_bs.png\" alt=\"retrofit_bs\" width=\"665\" height=\"374\" \/><\/a><br \/>\n1) POJO\u6216\u6a21\u578b\u5b9e\u4f53\u7c7b : \u4ece\u670d\u52a1\u5668\u83b7\u53d6\u7684JSON\u6570\u636e\u5c06\u88ab\u586b\u5145\u5230\u8fd9\u79cd\u7c7b\u7684\u5b9e\u4f8b\u4e2d\u3002<\/p>\n<p>[java]public class StudentBean {<br \/>\n    private String name;<br \/>\n    private int age;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>2) \u63a5\u53e3 : \u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u63a5\u53e3\u6765\u7ba1\u7406\u50cfGET,POST&#8230;\u7b49\u8bf7\u6c42\u7684URL\uff0c\u8fd9\u662f\u4e00\u4e2a\u670d\u52a1\u7c7b\u3002<\/p>\n<p>[java]public interface RetrofitCall {<br \/>\n    @GET(&quot;\/student&quot;)<br \/>\n    Call&lt;StudentBean&gt; getStudent();<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>3) RestAdapter\u7c7b : \u8fd9\u662f\u4e00\u4e2aREST\u5ba2\u6237\u7aef(RestClient)\u7c7b\uff0cretrofit\u4e2d\u9ed8\u8ba4\u7528\u7684\u662fGson\u6765\u89e3\u6790JSON\u6570\u636e\uff0c\u4f60\u4e5f\u53ef\u4ee5\u8bbe\u7f6e\u81ea\u5df1\u7684JSON\u89e3\u6790\u5668\uff0c\u6bd4\u5982jackson\u3002<br \/>\n\u4e00\u822c\u91c7\u7528\u4e0b\u672f\u65b9\u6cd5\u8c03\u7528:<\/p>\n<p>[java]<br \/>\n            Retrofit retrofit = new Retrofit.Builder()<br \/>\n                    .addConverterFactory(GsonConverterFactory.create())<br \/>\n                    .baseUrl(&quot;http:\/\/www.bm.com\/&quot;)<br \/>\n                    .build();<\/p>\n<p>            RetrofitCall call = retrofit.create(RetrofitCall.class);<br \/>\n            Call&lt;StudentBean&gt; mCall = call.getStudent();<br \/>\n            mCall.enqueue(new Callback&lt;StudentBean&gt;() {<br \/>\n                @Override<br \/>\n                public void onResponse(Call&lt;StudentBean&gt; call, Response&lt;StudentBean&gt; response) {<\/p>\n<p>                }<\/p>\n<p>                @Override<br \/>\n                public void onFailure(Call&lt;StudentBean&gt; call, Throwable t) {<\/p>\n<p>                }<br \/>\n        });<br \/>\n[\/java]<\/p>\n<h3>\u6dfb\u52a0Retrofit\u5e93\u5230\u9879\u76ee\u4e2d<\/h3>\n<p>[java]Gradle :<\/p>\n<p>compile &#8216;com.squareup.retrofit2:retrofit:2.1.0&#8217;<br \/>\ncompile &#8216;com.squareup.retrofit2:converter-gson:2.1.0&#8217;<br \/>\n[\/java]<\/p>\n<p>\u76ee\u524d\uff0c2.1.0\u662f\u6700\u65b0\u7684\u7248\u672c. \u4f60\u53ef\u4ee5\u5728<a href=\"https:\/\/github.com\/square\/retrofit\">\u8fd9\u91cc<\/a>\u83b7\u53d6\u66f4\u65b0\u7684\u7248\u672c\u3002<\/p>\n<h2 id=\"retrofit-\">\u57fa\u672c\u7684HTTP\u8bf7\u6c42<\/h2>\n<p>[java]\u5728Retrofit\u4e2d\u4f7f\u7528\u6ce8\u89e3\u7684\u65b9\u5f0f\u6765\u533a\u5206\u8bf7\u6c42\u7c7b\u578b.\u6bd4\u5982@GET(&quot;&quot;)\u8868\u793a\u4e00\u4e2aGET\u8bf7\u6c42,\u62ec\u53f7\u4e2d\u7684\u5185\u5bb9\u4e3a\u8bf7\u6c42\u7684\u5730\u5740.<br \/>\n\u683c\u5f0f\t\u542b\u4e49<br \/>\n@GET\t \u8868\u793a\u8fd9\u662f\u4e00\u4e2aGET\u8bf7\u6c42<br \/>\n@POST\t \u8868\u793a\u8fd9\u4e2a\u4e00\u4e2aPOST\u8bf7\u6c42<br \/>\n@PUT\t \u8868\u793a\u8fd9\u662f\u4e00\u4e2aPUT\u8bf7\u6c42<br \/>\n@DELETE\t \u8868\u793a\u8fd9\u662f\u4e00\u4e2aDELETE\u8bf7\u6c42<br \/>\n@HEAD\t \u8868\u793a\u8fd9\u662f\u4e00\u4e2aHEAD\u8bf7\u6c42<br \/>\n@OPTIONS \u8868\u793a\u8fd9\u662f\u4e00\u4e2aOPTION\u8bf7\u6c42<br \/>\n@PATCH\t \u8868\u793a\u8fd9\u662f\u4e00\u4e2aPAT\u8bf7\u6c42[\/java]<\/p>\n<h2 id=\"-http-\"><\/h2>\n<pre>Retrofit\u53ef\u5b9e\u73b0\u57fa\u672cHTTP\u8bf7\u6c42,\u5305\u62ecGET,POST,PUT,DELETE\u7b49.<\/pre>\n<p>[java]<br \/>\n1.GET\u8bf7\u6c42<\/p>\n<p>GET(&quot;\/record&quot;)<br \/>\nCall&lt;PhoneResult&gt; getResult();<br \/>\n2.POST\u8bf7\u6c42<\/p>\n<p>@POST(&quot;\/record&quot;)<br \/>\nCall&lt;PhoneResult&gt; getResult();<br \/>\n3.PUT\u8bf7\u6c42<\/p>\n<p>@PUT(&quot;\/record&quot;)<br \/>\nCall&lt;PhoneResult&gt; getResult();<br \/>\n4.DELETE\u8bf7\u6c42<\/p>\n<p>@DELETE(&quot;\/record&quot;)<br \/>\nCall&lt;PhoneResult&gt; getResult();<br \/>\n[\/java]<\/p>\n<h2 id=\"-\">\u670d\u52a1\u5668\u63a5\u53e3\u7c7b\u578b<\/h2>\n<p>\u670d\u52a1\u5668\u63a5\u53e3\u6709\u5f88\u591a\u4e2d,\u672c\u4eba\u7ecf\u9a8c\u6709\u9650,\u76ee\u524d\u63a5\u89e6\u8f83\u591a\u4e3a\u4ee5\u4e0b\u51e0\u79cd:<\/p>\n<h3 id=\"-\">\u76f4\u63a5\u8bf7\u6c42\u578b<\/h3>\n<blockquote><p>\u5373\u76f4\u63a5\u5bf9\u67d0\u4e00\u5730\u5740\u6216\u7ec4\u5408\u67d0\u4e00\u5730\u5740\u53d1\u8d77\u8bf7\u6c42<\/p><\/blockquote>\n<p>\u5982:\u5bf9\/result\u548c\/result\/{id}\u53d1\u8d77GET\u8bf7\u6c42,\u5176\u4e2d{id}\u4e2d\u7684id\u5728\u5b9e\u9645\u4f7f\u7528\u65f6\u586b\u5199\u5b9e\u9645\u503c\u5373\u53ef.<\/p>\n<h3 id=\"-\">\u5e26\u53c2\u67e5\u8be2\u578b<\/h3>\n<blockquote><p>\u5bf9\u67d0\u4e00\u5730\u5740\u8fdb\u884c\u5e26\u53c2\u67e5\u8be2\u8bf7\u6c42<\/p><\/blockquote>\n<p>\u5982:https:\/\/www.baidu.com\/s?wd=123\u4e3a\u5bf9\u63a5\u53e3https:\/\/www.baidu.com\/s\u8fdb\u884c\u53c2\u6570\u4e3awd=123\u7684GET\u67e5\u8be2\u8bf7\u6c42.<\/p>\n<h3 id=\"-header-\">\u5e26Header\u578b<\/h3>\n<blockquote><p>\u3000\u5373\u8bf7\u6c42\u65f6\u8981\u6c42\u5e26\u4e0aHeader<\/p><\/blockquote>\n<h2 id=\"retrofit-\">Retrofit\u4e2d\u5982\u4f55\u5199?<\/h2>\n<h3 id=\"-\">\u76f4\u63a5\u8bf7\u6c42\u578b<\/h3>\n<p>1.\u5982\u679c\u662f\u76f4\u63a5\u8bf7\u6c42\u67d0\u4e00\u5730\u5740,\u5199\u6cd5\u5982\u4e0b:<\/p>\n<p>[java]@GET(&quot;\/record&quot;)<br \/>\nCall&lt;PhoneResult&gt; getResult();[\/java]<\/p>\n<p>2.\u5982\u679c\u662f\u7ec4\u5408\u540e\u76f4\u63a5\u8bf7\u6c42,\u5982<code>\/result\/{id}<\/code>\u5199\u6cd5\u5982\u4e0b:<\/p>\n<p>[java]1.\u5982\u679c\u662f\u76f4\u63a5\u8bf7\u6c42\u67d0\u4e00\u5730\u5740,\u5199\u6cd5\u5982\u4e0b:<\/p>\n<p>@GET(&quot;\/record&quot;)<br \/>\nCall&lt;PhoneResult&gt; getResult();<br \/>\n2.\u5982\u679c\u662f\u7ec4\u5408\u540e\u76f4\u63a5\u8bf7\u6c42,\u5982\/result\/{id}\u5199\u6cd5\u5982\u4e0b:<\/p>\n<p>@GET(&quot;\/result\/{id}&quot;)<br \/>\nCall&lt;PhoneResult&gt; getResult(@Path(&quot;id&quot;) String id);[\/java]<\/p>\n<h3 id=\"-\">\u5e26\u53c2\u67e5\u8be2\u578b<\/h3>\n<p>\u598212306\u7684\u67e5\u8be2\u63a5\u53e3https:\/\/kyfw.12306.cn\/otn\/lcxxcx\/query?purpose_codes=ADULT&amp;queryDate=2016-03-18&amp;from_station=BJP&amp;to_station=CDW,\u5199\u6cd5\u5982\u4e0b:<\/p>\n<p>[java]<br \/>\n@GET(&quot;\/otn\/lcxxcx\/query&quot;)<br \/>\nCall&lt;Result&gt; query(@Query(&quot;purpose_codes&quot;) String codes, @Query(&quot;queryDate&quot;) String date,<br \/>\n    @Query(&quot;from_station&quot;) String from, @Query(&quot;to_station&quot;) String to)[\/java]<\/p>\n<h3 id=\"-header-\">\u5e26Header\u578b<\/h3>\n<p>\u6bd4\u5982\u8981\u66f4\u65b0\u67d0\u4e2a\u8d26\u6237\u4fe1\u606f,\u5176\u63a5\u53e3\u5730\u5740\u4e3a<code>\/info<\/code>,\u9700\u8981\u5e26\u7684Header\u6709\u8bbe\u5907\u4fe1\u606f<code>device<\/code>,\u7cfb\u7edf\u7248\u672c<code>version<\/code>,\u8fd8\u8981\u5e26\u8bf7\u6c42\u53c2\u6570\u8981\u66f4\u65b0\u8d26\u6237\u7684<code>id<\/code>,\u4ee3\u7801\u5982\u4e0b:<br \/>\n\u6bd4\u5982\u8981\u66f4\u65b0\u67d0\u4e2a\u8d26\u6237\u4fe1\u606f,\u5176\u63a5\u53e3\u5730\u5740\u4e3a\/info,\u9700\u8981\u5e26\u7684Header\u6709\u8bbe\u5907\u4fe1\u606fdevice,\u7cfb\u7edf\u7248\u672cversion,\u8fd8\u8981\u5e26\u8bf7\u6c42\u53c2\u6570\u8981\u66f4\u65b0\u8d26\u6237\u7684id,\u4ee3\u7801\u5982\u4e0b:<\/p>\n<p>[java]<br \/>\n@POST(&quot;\/info&quot;)<br \/>\nCall&lt;Object&gt; updateInfo(@Header(&quot;device&quot;) String device, @Header(&quot;version&quot;) int version,<br \/>\n                        @Field(&quot;id&quot;) String id);[\/java]<\/p>\n<h2 id=\"-\">\u5b9e\u4f8b<\/h2>\n<p>\u627e\u4e86\u5f88\u4e45\u53d1\u73b0\u591a\u8bf4\u63d0\u4f9b\u4e86\u4e00\u4e9bPOST\u8bf7\u6c42\u63a5\u53e3,\u4e0b\u9762\u5c31\u4ee5\u591a\u8bf4\u7684\u63a5\u53e3\u4e3a\u4f8b,\u770b\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528<code>Retrofit<\/code>\u5199\u8bf7\u6c42.<\/p>\n<h3 id=\"-url\">\u57fa\u7840URL<\/h3>\n<p>\u591a\u8bf4\u7684\u63a5\u53e3\u57fa\u7840\u5730\u5740\u4e3a:http:\/\/api.duoshuo.com,\u5219\u6784\u5efaRetrofit\u5b9e\u4f8b\u4ee3\u7801\u5982\u4e0b:<\/p>\n<p>[java]Retrofit retrofit = new Retrofit.Builder()<br \/>\n        .addConverterFactory(GsonConverterFactory.create())<br \/>\n        .baseUrl(&quot;http:\/\/api.duoshuo.com&quot;)<br \/>\n        .build();[\/java]<\/p>\n<h3 id=\"-\">\u83b7\u53d6\u6587\u7ae0\u8bc4\u8bba\u3001\u8f6c\u53d1\u6570<\/h3>\n<p>\u63a5\u53e3\u5730\u5740\u4e3a:<code>\/threads\/counts<\/code><\/p>\n<p>HTTP\u8bf7\u6c42\u65b9\u5f0f:<code>GET<\/code><\/p>\n<p>\u8bf7\u6c42\u793a\u4f8b\u4e3a:<code>http:\/\/api.duoshuo.com\/threads\/counts.json?short_name=official&amp;threads=4ff1cbc43ae636b72a00001d<\/code><\/p>\n<p>\u540e\u9762\u7684<code>.json<\/code>\u4e3a\u8fd4\u56de\u6570\u636e\u7684\u683c\u5f0f,\u6b64\u5904\u6211\u4eec\u4f7f\u7528<code>json<\/code>\u683c\u5f0f.<\/p>\n<p>\u8bf7\u6c42\u4ee3\u7801\u5982\u4e0b:<\/p>\n<p>[java]@GET(&quot;\/threads\/counts.json&quot;)<br \/>\nCall&lt;Object&gt; getCommit(@Query(&quot;short_name&quot;) String shortName,<br \/>\n                       @Query(&quot;threads&quot;) String threads);[\/java]<\/p>\n<h3 id=\"-\">\u533f\u540d\u53d1\u8868\u65b0\u8bc4\u8bba<\/h3>\n<p>\u63a5\u53e3\u5730\u5740\u4e3a:<code>\/posts\/create<\/code><\/p>\n<p>HTTP\u8bf7\u6c42\u65b9\u5f0f:<code>POST<\/code><\/p>\n<p>\u8bf7\u6c42\u793a\u4f8b\u4e3a:<\/p>\n<p>[java]<br \/>\nRequest URL:http:\/\/api.duoshuo.com\/posts\/create.json<br \/>\nRequest Method:POST<br \/>\nPost Data:short_name=official&amp;author_email=jp.chenyang%40gmail.com&amp;author_name=Perchouli&amp;thread_id=1152923703638301959&amp;author_url=http%3A%2F%2Fduoshuo.com&amp;message=\u533f\u540d\u53d1\u8868\u65b0\u8bc4\u8bba[\/java]<\/p>\n<p>1.Field\u65b9\u5f0f\u5b9e\u73b0<\/p>\n<p>[java]@FormUrlEncoded<br \/>\n@POST(&quot;\/posts\/create.json&quot;)<br \/>\nCall&lt;CommitResult&gt; createCommit(@Field(&quot;secret&quot;) String secret,<br \/>\n                                @Field(&quot;short_name&quot;) String shortName,<br \/>\n                                @Field(&quot;author_email&quot;) String authorEmail,<br \/>\n                                @Field(&quot;author_name&quot;) String authorName,<br \/>\n                                @Field(&quot;thread_key&quot;) String threadKey,<br \/>\n                                @Field(&quot;author_url&quot;) String author_url,<br \/>\n                                @Field(&quot;message&quot;) String message);[\/java]<\/p>\n<p>2.Field Map\u5b9e\u73b0\u65b9\u5f0f<\/p>\n<p>[java]@FormUrlEncoded<br \/>\n@POST(&quot;\/posts\/create.json&quot;)<br \/>\nCall&lt;CommitResult&gt; createCommit(@FieldMap Map&lt;String, String&gt; map);[\/java]<\/p>\n<p>\u83b7\u53d6Map\u65b9\u5f0f\u5982\u4e0b:<\/p>\n<p>[java]public class CommitParam {<\/p>\n<p>    private String short_name;<br \/>\n    private String author_email;<br \/>\n    private String author_name;<br \/>\n    private String thread_id;<br \/>\n    private String author_url;<br \/>\n    private String message;<\/p>\n<p>    public String getShort_name() {<br \/>\n        return short_name;<br \/>\n    }<\/p>\n<p>    public void setShort_name(String short_name) {<br \/>\n        this.short_name = short_name;<br \/>\n    }<\/p>\n<p>    public String getAuthor_email() {<br \/>\n        return author_email;<br \/>\n    }<\/p>\n<p>    public void setAuthor_email(String author_email) {<br \/>\n        this.author_email = author_email;<br \/>\n    }<\/p>\n<p>    public String getAuthor_name() {<br \/>\n        return author_name;<br \/>\n    }<\/p>\n<p>    public void setAuthor_name(String author_name) {<br \/>\n        this.author_name = author_name;<br \/>\n    }<\/p>\n<p>    public String getThread_id() {<br \/>\n        return thread_id;<br \/>\n    }<\/p>\n<p>    public void setThread_id(String thread_id) {<br \/>\n        this.thread_id = thread_id;<br \/>\n    }<\/p>\n<p>    public String getAuthor_url() {<br \/>\n        return author_url;<br \/>\n    }<\/p>\n<p>    public void setAuthor_url(String author_url) {<br \/>\n        this.author_url = author_url;<br \/>\n    }<\/p>\n<p>    public String getMessage() {<br \/>\n        return message;<br \/>\n    }<\/p>\n<p>    public void setMessage(String message) {<br \/>\n        this.message = message;<br \/>\n    }<\/p>\n<p>    public Map&lt;String, String&gt; createCommitParams(){<br \/>\n        Map&lt;String, String&gt; params = new HashMap&lt;&gt;();<br \/>\n        params.put(&quot;short_name&quot;, short_name);<br \/>\n        params.put(&quot;author_email&quot;, author_email);<br \/>\n        params.put(&quot;author_name&quot;, author_name);<br \/>\n        params.put(&quot;thread_id&quot;, thread_id);<br \/>\n        params.put(&quot;author_url&quot;, author_url);<br \/>\n        params.put(&quot;message&quot;, message);<br \/>\n        return params;<br \/>\n    }<br \/>\n}[\/java]<\/p>\n<p>\u9879\u76ee\u5730\u5740\u5728\u6b64:<a href=\"https:\/\/github.com\/Dev-Wiki\/RetrofitDemo\">Dev-Wiki\/RetrofitDemo<\/a><br \/>\n\u66f4\u591a\u6587\u7ae0\u8bf7\u79fb\u6b65\u6211\u7684\u535a\u5ba2:<a href=\"http:\/\/www.devwiki.net\/\">DevWiki Blog<\/a><br \/>\n\u53c2\u8003:http:\/\/www.jcodecraeer.com\/a\/anzhuokaifa\/androidkaifa\/2015\/0418\/2748.html<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb Retrofit\u662fSquare\u5f00 \u53d1\u7684\u4e00\u4e2aAndroid\u548cJava\u7684REST\u5ba2\u6237\u7aef\u5e93\u3002\u8fd9\u4e2a\u5e93\u975e\u5e38\u7b80\u5355\u5e76\u4e14 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[45],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1720"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1720"}],"version-history":[{"count":9,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1720\/revisions"}],"predecessor-version":[{"id":1730,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1720\/revisions\/1730"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}