如何使用 Android 中的 Koush/Ion 发送 Raw Json

How to send a Raw Json using Koush/Ion in Android

我正在尝试将 raw json 从我的 Android 应用程序发送到后端服务。该调用在 PostMan 中的工作方式如下:

    POST /MyController/MyMethod HTTP/1.1
    Host: mysite.com
    Accept: application/json
    Content-Type: application/json
    Cache-Control: no-cache
    Postman-Token: 19b74eb1-3d21-5fd4-35e9-fa7b51aef1ad

    {
      "my_json" : "values",
    }

我正在这样尝试:

    public static NameValuePair accept_json = new BasicNameValuePair("Accept", "application/json");
    public static NameValuePair content_type_json = new BasicNameValuePair("Content-Type", "application/json")
    ...
    Ion.with(context)
            .load(a_correct_url)
            .setHeader(WebserviceHome.MyHeaders.accept_json)
            .setHeader(WebserviceHome.MyHeaders.content_type_json)
            .setMultipartContentType(data)
            .asString()
            .setCallback(callbackHandler);
    ...
    FutureCallback<String> syncDataCallbackHandler = new FutureCallback<String>() {
        @Override
        public void onCompleted(Exception e, String s) {
            [do work]
        }
    };

来自 Ion 库的控制台日志是:

    (0 ms) https://example.com//MyController/MyMethod: preparing request
    (0 ms) https://example.com//MyController/MyMethod: Using loader: com.koushikdutta.ion.loader.HttpLoader@42f0a728
    (0 ms) https://example.com//MyController/MyMethod: Executing request.

并且回调不会触发,后端服务也不会做出反应。 如果有人有一些经验,请分享。

答案就在这两行中:

Ion...
.setJsonObjectBody(JsonObject)

.setStringBody(rawData)