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

岁酱吖の 提交于 2019-12-25 04:16:11

问题


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.


回答1:


The answer is in these two lines:

Ion...
.setJsonObjectBody(JsonObject)

or

.setStringBody(rawData)


来源:https://stackoverflow.com/questions/28964417/how-to-send-a-raw-json-using-koush-ion-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!