How to consume Json on Android? [closed]

-2

How do I consume a Json on Android?

    
asked by anonymous 15.03.2016 / 21:52

1 answer

1

Hello,

Use the IOn (REST) lib that provides a JSONObject and you can make POST, GET, PUT, and more.

First add dependency in app

dependencies {
    compile 'com.koushikdutta.ion:ion:2.+'
}

To read and send a json:

JsonObject json = new JsonObject();
json.addProperty("foo", "bar");

    Ion.with(context)
.load("http://example.com/post")
.setJsonObjectBody(json)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
   @Override
    public void onCompleted(Exception e, JsonObject result) {
        // do stuff with the result or error
    }
});

Using callback, ie it will not be blocked until the response arrives, and its return has to be treated asynchronously.

JsonObject json = new JsonObject();
json.addProperty("foo", "bar");

 JsonObject json = Ion.with(context)
.load("http://example.com/post")
.setJsonObjectBody(json)
.asJsonObject()//aqui voce define o tipo do retorno
.get();

Using the get method will be locked until it receives the response and can be handled synchronously.

To handle the return on a JsonObject:

json.get("nomeDaKey");

Link to more Lib info: link

    
15.03.2016 / 22:10