Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The answer is in these two lines:

Ion...
.setJsonObjectBody(JsonObject)

or

.setStringBody(rawData)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...