Android : Android koush/ion lib unable to post and receive JSON response

on Tuesday, August 5, 2014


I am using ION to GET/POST for REST and also to get Images into ListView but I am unable to use POST method and receive response as a JSON The ION version that I am using is 1.3.7. Tested on emulator and also on real devices 4.1.1, 4.2.2 and 4.4



Ion.with(getActivity())
.load(URLStr)
.addQuery("action", "dummyAction")
.addHeader("Content-Type", "application/json")
.setLogging("ION_VERBOSE_LOGGING", Log.VERBOSE)
.setJsonObjectBody(jsonStr)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
if (null != e) {
if (null != result) {

Log.d(Constants.LOG_TAG, "JSON Result User basic Info: " + result);
} else {
Log.e(Constants.LOG_TAG, "error in posting User basic Info");
}
} else {
Log.e(Constants.LOG_TAG, "error in posting User basic Info", e);
}
}
});


I am getting NullPointer at result. The request JSON is also valid, and I am using GSON to construct it. The response JSON is valid as I checked it using POSTMAN chrome app.


However, below code works perfectly fine, but it's depricated ION.with(getActivity(), URL) So I dont know, where am I making mistake.



Ion.with(getActivity(), URLStr)
.addQuery("action", "dummyAction")
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
if (e != null) {

e.printStackTrace();
Toast.makeText(getActivity(), "Error loading user data", Toast.LENGTH_LONG).show();
return;
}
Log.d(Constants.LOG_TAG, "DATA: " + result.toString());
String data = result.get("status").getAsString();
Log.d(Constants.LOG_TAG, "DATA: " + data);
}
});

0 comments:

Post a Comment