如何发布原整JSON的改造请求的主体? [英] How to POST raw whole JSON in the body of a Retrofit request?

查看:178
本文介绍了如何发布原整JSON的改造请求的主体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这问题可能已经问过,但没有它并没有明确回答。究竟如何做了改造要求的身体里面一个职位鲜全脂JSON?

This question may have been asked before but no it was not definitively answered. How exactly does one post raw whole JSON inside the body of a Retrofit request?

请参阅类似的问题这里。或者是这个答案正确的,它<一个href="http://stackoverflow.com/questions/18908175/retrofit-body-showing-up-as-parameter-in-http-request?rq=1">must是形式的URL连接codeD,并通过为外地?我真的希望不会,因为我连接到只是希望生JSON在后体内的服务。他们都没有设立找了JSON数据中的特定领域。

See similar question here. Or is this answer correct that it must be form url encoded and passed as a field? I really hope not, as the services I am connecting to are just expecting raw JSON in the body of the post. They are not set up to look for a particular field for the JSON data.

我只是想澄清与此的 restperts 的一劳永逸。一个人回答不使用改造。其他不特定的语法。另一种认为是这是可以做到,但只有当它的形式的URL连接codeD,并放置在一个领域(这不是我的情况下,可以接受)。不,我不能重新code中所有的服务我的Andr​​oid客户端。是的,它是在重大项目非常普遍张贴,而不是越过JSON内容字段属性值的原始JSON。让我们得到它的权利,继续前进。有人能指出来,显示如何做到这一点的文档或例子吗?或者提供一个有效的原因,它可以/不应该做的。

I just want to clarify this with the restperts once and for all. One person answered not to use Retrofit. The other was not certain of the syntax. Another thinks yes it can be done but only if its form url-encoded and placed in a field (that's not acceptable in my case). No, I can't re-code all the services for my Android client. And yes, it's very common in major projects to post raw JSON instead of passing over JSON content as field property values. Let's get it right and move on. Can someone point to the documentation or example that shows how this is done? Or provide a valid reason why it can/should not be done.

更新:有一件事我可以100%肯定地说。您可以在谷歌的凌空做到这一点。它的建成权,我们能做到这一点的改造?

UPDATE: One thing I can say with 100% certainty. You CAN do this in Google's Volley. It's built right in. Can we do this in Retrofit?

推荐答案

@Body 注解定义一个请求主体。

The @Body annotation defines a single request body.

interface Foo {
  @POST("/jayson")
  FooResponse postJson(@Body FooRequest body);
}

由于改装使用GSON默认情况下, FooRequest 实例将被序列化为JSON作为请求的唯一机构。

Since Retrofit uses Gson by default, the FooRequest instances will be serialized as JSON as the sole body of the request.

public class FooRequest {
  final String foo;
  final String bar;

  FooRequest(String foo, String bar) {
    this.foo = foo;
    this.bar = bar;
  }
}

与调用:

FooResponse = foo.postJson(new FooRequest("kit", "kat"));

将产生以下机身:

Will yield the following body:

{"foo":"kit","bar":"kat"}

借助 GSON文档对如何对象序列化的工作更多。

The Gson docs have much more on how object serialization works.

现在,如果你真的想送原始的JSON作为人体自己(但请使用GSON这个!),你仍然可以使用 TypedInput

Now, if you really really want to send "raw" JSON as the body yourself (but please use Gson for this!) you still can using TypedInput:

interface Foo {
  @POST("/jayson")
  FooResponse postRawJson(@Body TypedInput body);
}

TypedInput 是一个定义为具有关联MIME类型的二进制数据。 。有轻松发送原始数据与上述声明两种方式:

TypedInput is a defined as "Binary data with an associated mime type.". There's two ways to easily send raw data with the above declaration:

  1. 使用 TypedByteArray 发送原始字节和JSON MIME类型:

  1. Use TypedByteArray to send raw bytes and the JSON mime type:

String json = "{\"foo\":\"kit\",\"bar\":\"kat\"}";
TypedInput in = new TypedByteArray("application/json", json.getBytes("UTF-8"));
FooResponse response = foo.postRawJson(in);

  • 子类 TypedString 创建一个 TypedJsonString 类:

  • Subclass TypedString to create a TypedJsonString class:

    public class TypedJsonString extends TypedString {
      public TypedJsonString(String body) {
        super(body);
      }
    
      @Override public String mimeType() {
        return "application/json";
      }
    }
    

    然后使用这个类类似于#1的一个实例。

    And then use an instance of that class similar to #1.

    这篇关于如何发布原整JSON的改造请求的主体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆