How do I consume a Json on Android?
How do I consume a Json on Android?
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