使用POST改造身体JSON身体、POST、JSON

2023-09-06 02:18:15 作者:我陪着你走

我想使用改装库来发布JSONObject的,但是当我看到要求在接收端,内容长度 0

I'm trying to POST a JSONObject using the Retrofit library, but when I see the request at the receiving end, the content-length is 0.

在RestService接口:

In the RestService interface:

@Headers({
        "Content-type: application/json"
})
@POST("/api/v1/user/controller")
void registerController( 
     @Body JSONObject registrationBundle, 
     @Header("x-company-device-token") String companyDeviceToken, 
     @Header("x-company-device-guid") String companyDeviceGuid, 
     Callback<JSONObject> cb);

和它被调用,

mRestService.registerController(
    registrationBundle, 
    mApplication.mSession.getCredentials().getDeviceToken(), 
    mApplication.mSession.getCredentials().getDeviceGuid(),
    new Callback<JSONObject>() {
        // ...
    }
)

和我敢肯定,在 registrationBundle ,这是一个的JSONObject 不为空或空(其他领域当然是罚款)。此刻的请求时,它登出如下: {"zip":19312,"useAccountZip":false,"controllerName":"mine","registration$c$c":"GLD94Q"}.

And I'm certain that the registrationBundle, which is a JSONObject isn't null or empty (the other fields are certainly fine). At the moment the request is made, it logs out as: {"zip":19312,"useAccountZip":false,"controllerName":"mine","registrationCode":"GLD94Q"}.

在请求的接收端,我看到,请求有内容类型:应用程序/ JSON 内容长度:0

On the receiving end of the request, I see that the request has Content-type: application/json but has Content-length: 0.

有,为什么在人体内像这样发送JSON是行不通的任何原因?我失去了一些东西简单使用改装?

Is there any reason why sending JSON in the body like this isn't working? Am I missing something simple in using Retrofit?

推荐答案

在默认情况下,你并不需要,如果你想有一个JSON请求体设置任何头。当你测试改装code,我建议您RestAdapter实例设置 .setLogLevel(RestAdapter.LogLevel.FULL)。这将显示您完整的请求头和身体,以及完整的响应头和身体。

By default, you don't need to set any headers if you want a JSON request body. Whenever you test Retrofit code, I recommend setting .setLogLevel(RestAdapter.LogLevel.FULL) on your instance of RestAdapter. This will show you the full request headers and body as well as the full response headers and body.

什么是发生的是,你要设置的内容类型的两倍。然后你传递一个JSONObject的,正在穿过GsonConverter和错位的样子 {namevaluepairs中:YOURJSONSTRING} ,其中 YO​​URJSONSTRING 包含您完整的,打算JSON输出。由于显而易见的原因,这不会与大多数的REST API很好地工作。

What's occurring is that you are setting the Content-type twice. Then you're passing a JSONObject, which is being passed through the GsonConverter and mangled to look like {"nameValuePairs":YOURJSONSTRING} where YOURJSONSTRING contains your complete, intended JSON output. For obvious reasons, this won't work well with most REST APIs.

您应该跳过与已经被设置为JSON使用UTF-8默认情况下,Content-type头搞乱。另外,不要传递的JSONObject来GSON。传递一个Java对象GSON转换。

You should skip messing with the Content-type header which is already being set to JSON with UTF-8 by default. Also, don't pass a JSONObject to GSON. Pass a Java object for GSON to convert.

如果您使用的是回调试试这个:

Try this if you're using callbacks:

@POST("/api/v1/user/controller")
void registerController(
    @Body MyBundleObject registrationBundle,
    @Header("x-company-device-token") String companyDeviceToken,
    @Header("x-company-device-guid") String companyDeviceGuid,
    Callback<ResponseObject> cb);

我没有测试过这个确切的语法。

I haven't tested this exact syntax.

同步例如:

@POST("/api/v1/user/controller")
ResponseObject registerController(
    @Body MyBundleObject registrationBundle,
    @Header("x-company-device-token") String companyDeviceToken,
    @Header("x-company-device-guid") String companyDeviceGuid);