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

2019-09-02 12:22发布

I am trying to send a raw json from my Android App to a backend service. The call works in PostMan like so:

    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",
    }

And I am trying like so:

    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]
        }
    };

And the console logs from the Ion library are:

    (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.

And the callback doesn't fire nor does the backend service react. If someone has some experience, please share.

标签: android json ion
1条回答
Emotional °昔
2楼-- · 2019-09-02 12:49

The answer is in these two lines:

Ion...
.setJsonObjectBody(JsonObject)

or

.setStringBody(rawData)
查看更多
登录 后发表回答