如何在Android中使用okhttp发送纯文本请求主体?

huangapple go评论97阅读模式
英文:

How to send plain text as request body using okhttp in Android?

问题

I need to send a plain text as the request payload to my web app. But I can't make it to work with okhttp. I don't need complicated body builder that okhttp comes with such as FormBody.Builder() or MultipartBody.Builder(). I just need a simple text body attached to a POST http request header. Something like this:

  1. val myBody = "my own body text that my server understands"
  2. val request = Request.Builder()
  3. .url(myUrl)
  4. .post(myBody) //does not work.
  5. .build()
  6. okHttpClient.newCall(request)...

If okhttp can't do it, is there an alternative to use?

英文:

I need to send a plain text as the request payload to my web app. But I can't make it to work with okhttp. I don't need complicated body builder that okhttp comes with such as FormBody.Builder() or MultipartBody.Builder(). I just need a simple text body attached to a POST http request header. Something like this:

  1. val myBody = "my own body text that my server understands"
  2. val request = Request.Builder()
  3. .url(myUrl)
  4. .post(myBody) //does not work.
  5. .build()
  6. okHttpClient.newCall(request)...

If okhttp can't do it, is there an alternative to use?

答案1

得分: 0

你可以使用OkHttp。

你可以在示例中找到一个例子 - 发布一个字符串

类似这样的代码:

  1. private val client = OkHttpClient()
  2. fun run() {
  3. val postBody = """
  4. |Releases
  5. |--------
  6. |
  7. | * _1.0_ May 6, 2013
  8. | * _1.1_ June 15, 2013
  9. | * _1.2_ August 11, 2013
  10. """.trimMargin()
  11. val request = Request.Builder()
  12. .url("https://api.github.com/markdown/raw")
  13. .post(postBody.toRequestBody(MEDIA_TYPE_MARKDOWN))
  14. .build()
  15. client.newCall(request).execute().use { response ->
  16. if (!response.isSuccessful) throw IOException("Unexpected code $response")
  17. println(response.body!!.string())
  18. }
  19. }
  20. companion object {
  21. val MEDIA_TYPE_MARKDOWN = "text/x-markdown; charset=utf-8".toMediaType()
  22. }
英文:

You can use OkHttp.

You can find an example in recipes - Posting a String

Something like this:

  1. private val client = OkHttpClient()
  2. fun run() {
  3. val postBody = """
  4. |Releases
  5. |--------
  6. |
  7. | * _1.0_ May 6, 2013
  8. | * _1.1_ June 15, 2013
  9. | * _1.2_ August 11, 2013
  10. |""".trimMargin()
  11. val request = Request.Builder()
  12. .url("https://api.github.com/markdown/raw")
  13. .post(postBody.toRequestBody(MEDIA_TYPE_MARKDOWN))
  14. .build()
  15. client.newCall(request).execute().use { response ->
  16. if (!response.isSuccessful) throw IOException("Unexpected code $response")
  17. println(response.body!!.string())
  18. }
  19. }
  20. companion object {
  21. val MEDIA_TYPE_MARKDOWN = "text/x-markdown; charset=utf-8".toMediaType()
  22. }

huangapple
  • 本文由 发表于 2023年5月14日 22:50:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248093.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定