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

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

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:

val myBody = "my own body text that my server understands"
val request = Request.Builder()
            .url(myUrl)
            .post(myBody) //does not work.
            .build()
        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:

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

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

答案1

得分: 0

你可以使用OkHttp。

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

类似这样的代码:

private val client = OkHttpClient()

fun run() {
    val postBody = """
        |Releases
        |--------
        |
        | * _1.0_ May 6, 2013
        | * _1.1_ June 15, 2013
        | * _1.2_ August 11, 2013
        """.trimMargin()

    val request = Request.Builder()
        .url("https://api.github.com/markdown/raw")
        .post(postBody.toRequestBody(MEDIA_TYPE_MARKDOWN))
        .build()

    client.newCall(request).execute().use { response ->
        if (!response.isSuccessful) throw IOException("Unexpected code $response")

        println(response.body!!.string())
    }
}

companion object {
    val MEDIA_TYPE_MARKDOWN = "text/x-markdown; charset=utf-8".toMediaType()
}
英文:

You can use OkHttp.

You can find an example in recipes - Posting a String

Something like this:

private val client = OkHttpClient()

  fun run() {
    val postBody = """
        |Releases
        |--------
        |
        | * _1.0_ May 6, 2013
        | * _1.1_ June 15, 2013
        | * _1.2_ August 11, 2013
        |""".trimMargin()

    val request = Request.Builder()
        .url("https://api.github.com/markdown/raw")
        .post(postBody.toRequestBody(MEDIA_TYPE_MARKDOWN))
        .build()

    client.newCall(request).execute().use { response ->
      if (!response.isSuccessful) throw IOException("Unexpected code $response")

      println(response.body!!.string())
    }
  }

  companion object {
    val MEDIA_TYPE_MARKDOWN = "text/x-markdown; charset=utf-8".toMediaType()
  }

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:

确定