使用 Retrofit 2 发送原始 JSON 数据。

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

Post raw JSON using Retrofit 2

问题

我从另一个应用程序收到了一个大的 JSON 对象,我需要使用 Retrofit 2 发起一个 POST 请求到一个 API。我在这个主题上找到的所有示例或讨论都使用 JSONObject 或者 Map。这意味着我必须解析我的字符串(JSON)以创建 JSONObject 或者 Map。

但是我想直接将我的字符串放入请求体中。

我尝试了以下方式,但是不起作用:

  1. @Headers("Content-Type: application/json")
  2. @POST("transactions")
  3. Call<Void> submitTransaction(@Body String body);

这样我发送了一个作为请求体的转义字符串,导致我的 JSON 看起来很奇怪。

英文:

I have a big JSON object received from another application and I have to make a post call to an API using Retrofit 2.
All the examples or topics that I find on this topic use an JSONObject or a Map. That means I have to parse my string (JSON) in order to create the JSONObject or Map.

But I want to put directly my string in the body somehow.

I tried this way, but it doesn't work:

  1. @Headers(&quot;Content-Type: application/json&quot;)
  2. @POST(&quot;transactions&quot;)
  3. Call&lt;Void&gt; submitTransaction(@Body String body);

This way I am sending a string that is escaped as a body and my JSON looks strange:
screenshot

答案1

得分: 1

你需要将Scalars Converter添加到Gradle依赖列表中,它会负责将java.lang.String对象转换为text/plain请求体,因为Retrofit默认使用Gson converter。查看此链接获取最新版本。

  1. dependencies {
  2. implementation "com.squareup.retrofit2:converter-scalars:2.4.0"
  3. ..
  4. }

然后,您需要向Retrofit构建器传递正确的转换器工厂。它将告诉Retrofit如何转换传递给服务的@Body参数。

  1. val retrofit = Retrofit.Builder()
  2. .client(okHttpClient)
  3. .baseUrl("/")
  4. .addConverterFactory(ScalarsConverterFactory.create())
  5. .build()

在此之后,是时候为具有String类型参数的Retrofit服务进行设置了。

  1. @Headers("Content-Type: application/json")
  2. @POST("transactions")
  3. Call<Void> submitTransaction(@Body String body);
英文:

You need to add a Scalars Converter to the list of your Gradle dependencies, which would take care of converting java.lang.String objects to text/plain request bodies, beacause Retrofit uses Gson converter by default. Check this link for the latest version.

https://search.maven.org/search?q=g:com.squareup.retrofit2%20a:converter-scalars

  1. dependencies {
  2. implementation com.squareup.retrofit2:converter-scalars:2.4.0
  3. ..
  4. }

Then, you need to pass a correct converter factory to your Retrofit builder. It will later tell Retrofit how to convert the @Body parameter passed to the service.

  1. val retrofit = Retrofit.Builder()
  2. .client(okHttpClient)
  3. .baseUrl(&quot;/&quot;)
  4. .addConverterFactory(ScalarsConverterFactory.create())
  5. .build()

After this, it is time for setting up a Retrofit service with a String body parameter.

  1. @Headers(&quot;Content-Type: application/json&quot;)
  2. @POST(&quot;transactions&quot;)
  3. Call&lt;Void&gt; submitTransaction(@Body String body);

huangapple
  • 本文由 发表于 2020年8月17日 22:52:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63453359.html
匿名

发表评论

匿名网友

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

确定