简体   繁体   中英

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

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.

The answer is in these two lines:

Ion...
.setJsonObjectBody(JsonObject)

or

.setStringBody(rawData)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM