{"id":6791,"date":"2024-03-23T10:01:01","date_gmt":"2024-03-23T02:01:01","guid":{"rendered":""},"modified":"2024-03-23T10:01:01","modified_gmt":"2024-03-23T02:01:01","slug":"Retrofit\u4f7f\u7528\u8be6\u89e3","status":"publish","type":"post","link":"https:\/\/mushiming.com\/6791.html","title":{"rendered":"Retrofit\u4f7f\u7528\u8be6\u89e3"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u4ecb\u7ecd<\/h4>\n
Retrofit\u662fSquare\u516c\u53f8\u5f00\u53d1\u7684\u4e00\u6b3e\u9488\u5bf9Android\u7f51\u7edc\u8bf7\u6c42\u7684\u6846\u67b6\uff0cRetrofit2\u5e95\u5c42\u57fa\u4e8eOkHttp\u5b9e\u73b0\u7684\n\u672c\u6587\u5168\u90e8\u662f\u5728Retrofit2.0+\u7248\u672c\u57fa\u7840\u4e0a\u8bba\u8ff0\n<\/code><\/pre>\n

\u5f15\u7528<\/h4>\n
Gradle :\ncompile 'com.squareup.retrofit:retrofit:2.0.0-beta2'\n\nMaven :\n<dependency>\n  <groupId>com.squareup.retrofit<\/groupId>\n  <artifactId>retrofit<\/artifactId>\n  <version>2.0.0-beta2<\/version>\n<\/dependency>\n<\/code><\/pre>\n

\u7528\u6cd5demo\uff08get\u65b9\u6cd5\uff09<\/h4>\n
1.\u521b\u5efa\u4e1a\u52a1\u8bf7\u6c42\u63a5\u53e3\uff0c\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a\npublic interface InterService {\n@GET(\"query?\")\nCall<Data> getPhoto(@Query(\"email\") String email,@Query(\"key\") String key);\n}\n\u8bf4\u660e\uff1a\n@GET\u6ce8\u89e3\u5c31\u8868\u793aget\u8bf7\u6c42\uff0c@Query\u8868\u793a\u8bf7\u6c42\u53c2\u6570\uff0c\u5c06\u4f1a\u4ee5key=value\u7684\u65b9\u5f0f\u62fc\u63a5\u5728url\u540e\u9762\nData \u5c31\u662f\u8fd4\u56de\u6570\u636ejson\u5c01\u88c5\u540e\u7684\u5bf9\u8c61\npublic class Data {\nprivate String reason;\nprivate int error_code;\nprivate Result result;\n}\n\npublic class Result {\nprivate int status;\nprivate String status_info;\n}\n<\/code><\/pre>\n
\n
2.\u9700\u8981\u521b\u5efa\u4e00\u4e2aRetrofit\u7684\u793a\u4f8b\uff0c\u5e76\u5b8c\u6210\u76f8\u5e94\u7684\u914d\u7f6e\n Retrofit retrofit = new Retrofit.Builder()\n            .baseUrl(\"http:\/\/v.juhe.cn\/verifyemail\/\")\n            .addConverterFactory(GsonConverterFactory.create())\n            .build();\n\nInterService service = retrofit.create(InterService.class);\n\u8bf4\u660e\uff1a\naddConverterFactory\u65b9\u6cd5\u8868\u793a\u9700\u8981\u7528\u4ec0\u4e48\u8f6c\u6362\u5668\u6765\u89e3\u6790\u8fd4\u56de\u503c\uff08json\uff09\uff0cGsonConverterFactory.create()\u8868\u793a\n\u8c03\u7528Gson\u5e93\u6765\u89e3\u6790json\u8fd4\u56de\u503c\n<\/code><\/pre>\n
\n
3.\u8c03\u7528\u8bf7\u6c42\u65b9\u6cd5\uff0c\u5e76\u5f97\u5230Call\u5b9e\u4f8b\nCall<Data> call = service.getPhoto(\"2314800549@qq.com\",\"add4069adb709c1204afb759694c767c\");\n<\/code><\/pre>\n
\n
4.\u4f7f\u7528Call\u5b9e\u4f8b\u5b8c\u6210\u540c\u6b65\u6216\u5f02\u6b65\u8bf7\u6c42\n\u540c\u6b65\u8bf7\u6c42\uff1a\n \/\/\u540c\u6b65\u8bf7\u6c42\n    try {\n        Data response = call.execute().body();\n    } catch (IOException e) {\n        e.printStackTrace();\n    }\n\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662f\u7f51\u7edc\u8bf7\u6c42\u4e00\u5b9a\u8981\u5728\u5b50\u7ebf\u7a0b\u4e2d\u5b8c\u6210\uff0c\u4e0d\u80fd\u76f4\u63a5\u5728UI\u7ebf\u7a0b\u6267\u884c\uff0c\u4e0d\u7136\u4f1acrash\n\n\u5f02\u6b65\u8bf7\u6c42\uff1a\n\/\/\u5f02\u6b65\u8bf7\u6c42\ncall.enqueue(new Callback<Data>() {\n        @Override\n        public void onResponse(Call<Data> call, Response<Data> response) {\n            Toast.makeText(MainActivity.this,response.body().toString(),Toast.LENGTH_LONG).show();\n        }\n        @Override\n        public void onFailure(Call<Data> call, Throwable t) {\n            Toast.makeText(MainActivity.this,t.toString(),Toast.LENGTH_LONG).show();\n\n        }\n    });\nresonpse\u7684body\u5c31\u662fdata\u5bf9\u8c61\n<\/code><\/pre>\n

\"Retrofit\u4f7f\u7528\u8be6\u89e3<\/p>\n

retrofit\u6ce8\u89e3<\/h4>\n
\u65b9\u6cd5\u6ce8\u89e3\uff0c\u5305\u542b@GET\u3001@POST\u3001@PUT\u3001@DELETE\u3001@PATH\u3001@HEAD\u3001@OPTIONS\u3001@HTTP\u3002\n\u6807\u8bb0\u6ce8\u89e3\uff0c\u5305\u542b@FormUrlEncoded\u3001@Multipart\u3001@Streaming\u3002\n\u53c2\u6570\u6ce8\u89e3\uff0c\u5305\u542b@Query,@QueryMap\u3001@Body\u3001@Field\uff0c@FieldMap\u3001@Part\uff0c@PartMap\u3002\n\u5176\u4ed6\u6ce8\u89e3\uff0c@Path\u3001@Header,@Headers\u3001@Url\n<\/code><\/pre>\n
\n
@HTTP\uff1a\u53ef\u4ee5\u66ff\u4ee3\u5176\u4ed6\u65b9\u6cd5\u7684\u4efb\u610f\u4e00\u79cd\n\/**\n * method \u8868\u793a\u8bf7\u7684\u65b9\u6cd5\uff0c\u4e0d\u533a\u5206\u5927\u5c0f\u5199\n * path\u8868\u793a\u8def\u5f84\n * hasBody\u8868\u793a\u662f\u5426\u6709\u8bf7\u6c42\u4f53\n *\/\n@HTTP(method = \"get\", path = \"users\/{user}\", hasBody = false)\nCall<ResponseBody> getFirstBlog(@Path(\"user\") String user);\n<\/code><\/pre>\n
\n
@Url\uff1a\u4f7f\u7528\u5168\u8def\u5f84\u590d\u5199baseUrl\uff0c\u9002\u7528\u4e8e\u975e\u7edf\u4e00baseUrl\u7684\u573a\u666f\u3002\n@GET\nCall<ResponseBody> v3(@Url String url);\n<\/code><\/pre>\n
\n
@Streaming:\u7528\u4e8e\u4e0b\u8f7d\u5927\u6587\u4ef6\n@Streaming\n@GET\nCall<ResponseBody> downloadFileWithDynamicUrlAsync(@Url String fileUrl);\n<\/code><\/pre>\n
\n
@Path\uff1aURL\u5360\u4f4d\u7b26\uff0c\u7528\u4e8e\u66ff\u6362\u548c\u52a8\u6001\u66f4\u65b0,\u76f8\u5e94\u7684\u53c2\u6570\u5fc5\u987b\u4f7f\u7528\u76f8\u540c\u7684\u5b57\u7b26\u4e32\u88ab@Path\u8fdb\u884c\u6ce8\u91ca\uff0c\u66ff\u6362\u5757\u662f{ and }\u5305\u56f4\u7684\u5b57\u6bcd\n\u6570\u5b57\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\n@GET(\"group\/{id}\/users\")\nCall<List<User>> groupList(@Path(\"id\") int groupId);\n<\/code><\/pre>\n
\n
@Query,@QueryMap:\u67e5\u8be2\u53c2\u6570\uff0c\u7528\u4e8eGET\u67e5\u8be2,\u9700\u8981\u6ce8\u610f\u7684\u662f@QueryMap\u53ef\u4ee5\u7ea6\u5b9a\u662f\u5426\u9700\u8981encode\n@GET(\"group\/users\")\nCall<List<User>> groupList(@Query(\"id\") int groupId);\nCall<List<News>> getNews((@QueryMap(encoded=true) Map<String, String> options);\n<\/code><\/pre>\n
\n
@Body:\u7528\u4e8ePOST\u8bf7\u6c42\u4f53\uff0c\u5c06\u5b9e\u4f8b\u5bf9\u8c61\u6839\u636e\u8f6c\u6362\u65b9\u5f0f\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684json\u5b57\u7b26\u4e32\u53c2\u6570\uff0c\n\u8fd9\u4e2a\u8f6c\u5316\u65b9\u5f0f\u662fGsonConverterFactory\u5b9a\u4e49\u7684\u3002\n @POST(\"add\")\n Call<List<User>> addUser(@Body User user);\n<\/code><\/pre>\n
\n
@Field\uff0c@FieldMap:Post\u65b9\u5f0f\u4f20\u9012\u7b80\u5355\u7684\u952e\u503c\u5bf9,\n\u9700\u8981\u6dfb\u52a0@FormUrlEncoded\u8868\u793a\u8868\u5355\u63d0\u4ea4\nContent-Type:application\/x-www-form-urlencoded\n@FormUrlEncoded\n@POST(\"user\/edit\")\nCall<User> updateUser(@Field(\"first_name\") String first, @Field(\"last_name\") String last);\n<\/code><\/pre>\n
\n
@Part\uff0c@PartMap\uff1a\u7528\u4e8ePOST\u6587\u4ef6\u4e0a\u4f20\n\u5176\u4e2d@Part MultipartBody.Part\u4ee3\u8868\u6587\u4ef6\uff0c@Part(\"key\") RequestBody\u4ee3\u8868\u53c2\u6570\n\u9700\u8981\u6dfb\u52a0@Multipart\u8868\u793a\u652f\u6301\u6587\u4ef6\u4e0a\u4f20\u7684\u8868\u5355\uff0cContent-Type: multipart\/form-data\n<\/code><\/pre>\n
\n
@Header\uff1aheader\u5904\u7406\uff0c\u4e0d\u80fd\u88ab\u4e92\u76f8\u8986\u76d6\uff0c\u7528\u4e8e\u4fee\u9970\u53c2\u6570\uff0c\n@GET(\"user\")\nCall<User> getUser(@Header(\"Authorization\") String authorization)\n<\/code><\/pre>\n
\n
@Headers \u7528\u4e8e\u4fee\u9970\u65b9\u6cd5,\u7528\u4e8e\u8bbe\u7f6e\u591a\u4e2aHeader\u503c\uff1a\n@Headers({\n\"Accept: application\/vnd.github.v3.full+json\",\n\"User-Agent: Retrofit-Sample-App\"\n})\n@GET(\"users\/{username}\")\nCall<User> getUser(@Path(\"username\") String username);\n<\/code><\/pre>\n

\u81ea\u5b9a\u4e49\u8f6c\u6362\u5668<\/h4>\n
\u5982\u679c\u9700\u8981\u4f7f\u7528Retrofit\u4e0d\u652f\u6301\u5f00\u7bb1\u5373\u7528\u7684\u5185\u5bb9\u683c\u5f0f(\u5982YAML\u3001txt\u3001\u81ea\u5b9a\u4e49\u683c\u5f0f)\u548cAPI\u8fdb\u884c\u901a\u4fe1\uff0c\u6216\u8005\u60f3\u8981\u4f7f\u7528\u4e0d\u540c\u7684\u5e93\u5b9e\n\u73b0\u5df2\u7ecf\u5b58\u5728\u7684\u683c\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u521b\u5efa\u81ea\u5df1\u7684\u8f6c\u6362\u5668\u3002\u521b\u5efa\u65b9\u5f0f\uff1a\u65b0\u5efa\u4e00\u4e2a\u7c7b\u7ee7\u627fConverter.Factory\u7c7b\uff0c\u5e76\u5728\u6784\u5efaRetrofit\n\u5b9e\u4f8b\u65f6\u4f20\u5165\u8f6c\u6362\u5668\u5b9e\u4f8b\u3002\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Retrofit\u4f7f\u7528\u8be6\u89e3\u4ecb\u7ecdRetrofit\u662fSquare\u516c\u53f8\u5f00\u53d1\u7684\u4e00\u6b3e\u9488\u5bf9Android\u7f51\u7edc\u8bf7\u6c42\u7684\u6846\u67b6\uff0cRetrofit2\u5e95\u5c42\u57fa\u4e8eOkHttp\u5b9e\u73b0\u7684\u672c\u6587\u5168\u90e8\u662f...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6791"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=6791"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6791\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=6791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=6791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=6791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}