{"id":183,"date":"2020-03-17T12:53:08","date_gmt":"2020-03-17T04:53:08","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=183"},"modified":"2021-04-04T18:52:47","modified_gmt":"2021-04-04T10:52:47","slug":"retrofit-%e5%af%a6%e7%8f%be%e5%a4%9a%e5%9c%96%e7%89%87-%e6%aa%94%e6%a1%88%e3%80%81%e5%9c%96%e6%96%87%e4%b8%8a%e5%82%b3%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/retrofit-%e5%af%a6%e7%8f%be%e5%a4%9a%e5%9c%96%e7%89%87-%e6%aa%94%e6%a1%88%e3%80%81%e5%9c%96%e6%96%87%e4%b8%8a%e5%82%b3%e5%8a%9f%e8%83%bd\/","title":{"rendered":"Android Retrofit Coroutine \u5be6\u73fe\u591a\u5716\u7247\/\u6a94\u6848\u4e0a\u50b3\u529f\u80fd"},"content":{"rendered":"<h1>Android Retrofit Coroutine \u5be6\u73fe\u591a\u5716\u7247\/\u6a94\u6848\u4e0a\u50b3\u529f\u80fd<\/h1>\n<h4>\u6587\u7ae0\u76ee\u9304<\/h4>\n<ol>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/retrofit-\u5be6\u73fe\u591a\u5716\u7247-\u6a94\u6848\u3001\u5716\u6587\u4e0a\u50b3\u529f\u80fd\/fly\/retrofit\/#a\">\u7db2\u8def\u6b0a\u9650&#038;\u5c0e\u5165Library<\/a><\/li>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/retrofit-\u5be6\u73fe\u591a\u5716\u7247-\u6a94\u6848\u3001\u5716\u6587\u4e0a\u50b3\u529f\u80fd\/fly\/retrofit\/#b\">\u5275\u5efaAPI\u7684interface<\/a><\/li>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/retrofit-\u5be6\u73fe\u591a\u5716\u7247-\u6a94\u6848\u3001\u5716\u6587\u4e0a\u50b3\u529f\u80fd\/fly\/retrofit\/#c\">\u5275\u5efaRetrofitUtil<\/a><\/li>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/retrofit-\u5be6\u73fe\u591a\u5716\u7247-\u6a94\u6848\u3001\u5716\u6587\u4e0a\u50b3\u529f\u80fd\/fly\/retrofit\/#d\">\u5275\u5efaRetrofitHttpUpload(\u53ef\u65b9\u4fbf\u7528\u65bc\u6dfb\u52a0key\u8207value)<\/a><\/li>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/retrofit-\u5be6\u73fe\u591a\u5716\u7247-\u6a94\u6848\u3001\u5716\u6587\u4e0a\u50b3\u529f\u80fd\/fly\/retrofit\/#e\">\u7a0b\u5f0f\u78bc\u7bc4\u4f8b<\/a><\/li>\n<\/ol>\n<hr \/>\n<p><a id=\"a\"><\/a><\/p>\n<h4>1.\u7db2\u8def\u6b0a\u9650&amp;\u5c0e\u5165Library<\/h4>\n<h5>Manifest<\/h5>\n<pre><code class=\"language-XML line-numbers\">&lt;uses-permission android:name=\"android.permission.INTERNET\"\/&gt;\n<\/code><\/pre>\n<h5>Gradle(Module)<\/h5>\n<pre><code class=\"language-Gradle line-numbers\">dependencies {\n    implementation 'com.squareup.retrofit2:retrofit:2.9.0'\n    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'\n    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3'\n}\n<\/code><\/pre>\n<p><a id=\"b\"><\/a><\/p>\n<h4>2.\u5275\u5efaAPI\u7684interface<\/h4>\n<h5>ApiService<\/h5>\n<pre><code class=\"language-Kotlin line-numbers\">@JvmSuppressWildcards\ninterface ApiService {\n    @Multipart\n    @POST(\"insert_image.php\")\n    suspend fun uploadImg(@PartMap params: Map&lt;String, RequestBody&gt;) : HttpResult\n}\n<\/code><\/pre>\n<h5>HttpResult<\/h5>\n<pre><code class=\"language-Kotlin line-numbers\">data class HttpResult(val status: String, val userMessage: String)\n<\/code><\/pre>\n<p><a id=\"c\"><\/a><\/p>\n<h4>3.\u5275\u5efaRetrofitUtil<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">class RetrofitUtils {\n    private var retrofit: Retrofit = Retrofit.Builder()\n        .addConverterFactory(GsonConverterFactory.create())\n        .client(getClient())\n        .baseUrl(\"https:\/\/aa\/bb\/\")\n        .build()\n\n    companion object {\n        val instance: RetrofitUtils by lazy { RetrofitUtils() }\n    }\n\n    fun &lt;T&gt; getService(clazz: Class&lt;T&gt;): T {\n        return retrofit.create(clazz)\n    }\n\n    private fun getClient(): OkHttpClient {\n        val builder = OkHttpClient.Builder()\n        val logInterceptor = HttpLoggingInterceptor()\n        logInterceptor.level = HttpLoggingInterceptor.Level.BODY\n        builder.addInterceptor(logInterceptor)\n        return builder.build()\n    }\n}\n<\/code><\/pre>\n<p><a id=\"d\"><\/a><\/p>\n<h4>4.\u5275\u5efaRetrofitHttpUpload(\u53ef\u65b9\u4fbf\u7528\u65bc\u6dfb\u52a0key\u8207value)<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">class RetrofitHttpUpload {\n    private val params = mutableMapOf&lt;String, RequestBody&gt;()\n\n    fun addParameter(key: String, o: Any): RetrofitHttpUpload {\n        if (o is String) {\n            val body = RequestBody.create(MediaType.parse(\"text\/plain;charset=UTF-8\"), o)\n            params[key] = body\n        } else if (o is File) {\n            val body = RequestBody.create(MediaType.parse(\"multipart\/form-data;charset=UTF-8\"), o)\n            params[key + \"\\\"; filename=\\\"\" + o.name] = body\n        }\n        return this\n    }\n\n    fun builder() = params\n}\n<\/code><\/pre>\n<p><a id=\"e\"><\/a><\/p>\n<h4>5.\u7a0b\u5f0f\u78bc\u7bc4\u4f8b<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">\/\/\u53d6\u5f97\u5716\u7247\u6a94\u6848\nval bitmap = BitmapFactory.decodeResource(resources, R.drawable.girl)\nval file = File(externalCacheDir, \"girl.jpg\")\nval ops = FileOutputStream(file)\nbitmap.compress(Bitmap.CompressFormat.JPEG, 100, ops)\nops.close()\n\n\/\/\u5c07\u8cc7\u6599\u52a0\u5165RequestBody\nval params = RetrofitHttpUpload().addParameter(\"file\", file).builder()\n\nCoroutineScope(Dispatchers.IO).launch {\n    val httpResult = RetrofitUtils.instance.getService(ApiService::class.java).uploadImg(params)\n    Log.e(\"GGG\", httpResult.userMessage)\n}\n<\/code><\/pre>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2021\/02\/wp_editor_md_bf3ecdcc9a24773a27e2152ea7ab650e.jpg\"><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2021\/02\/wp_editor_md_bf3ecdcc9a24773a27e2152ea7ab650e.jpg\" alt=\"\" \/><\/a><\/p>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Android Retrofit Coroutine \u5be6\u73fe\u591a\u5716\u7247\/\u6a94\u6848\u4e0a\u50b3\u529f\u80fd \u6587\u7ae0\u76ee\u9304 \u7db2\u8def\u6b0a\u9650&#038; &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[10],"tags":[13,14,38],"class_list":["post-183","post","type-post","status-publish","format-standard","hentry","category-retrofit","tag-android","tag-java","tag-retrofit"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=183"}],"version-history":[{"count":8,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/183\/revisions"}],"predecessor-version":[{"id":1314,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/183\/revisions\/1314"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}