I decided to use the new Projects for data Json. Before that I used XML(library Jsoup). I found a library ion
With it I can easily get the data in the form String or Json.
Now the question. As I continue to process this data? When working with XML I created a class bean with the same fields in XML. And then worked with an array of these classes. But I saw that there is an android org.json.JSONObject;
I can To work directly with these objects or do I still need to put all the data into classes? And if I can what is it right?
In other words correct this code?:
Ion.with(getActivity())
.load(my link)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
// do stuff with the result or error
Toast.makeText(getActivity(), result.getAsJsonObject("person").get("1").toString(), Toast.LENGTH_LONG).show();
name.setText(result.getAsJsonObject("person").getAsJsonObject("1").get("name").toString());
age.setText(result.getAsJsonObject("person").getAsJsonObject("1").get("age").toString());
}
});
EDIT:
This library I found not to write manually downloading data from the server. As I understand the process is divided into two stages. 1. Get the data from the server. 2. Parsing the data obtained. In the first case is usually manually write data acquisition. Example:
private String readString(HttpEntity entity) throws IOException {
// open streams
InputStream in = null;
boolean success = true;
IOException readException = null;
in = entity.getContent();
ByteArrayOutputStream out = new ByteArrayOutputStream();
// progress vars
int total = (int)entity.getContentLength();
int current = 0;
// fetch response
byte[] buff = new byte[1024];
int read;
while ((read = in.read(buff)) != -1) {
out.write(buff, 0, read);
current += read;
if(progressListener != null) progressListener.onProgress(total, current);
}
out.flush();
String result = new String(out.toByteArray(), "UTF-8").trim();
in.close();
out.close();
return result;
}
Library simplifies this. The second stage - parsing. But the library is already giving me the data in the format Json. So whether you want to do something else, or you can use the resulting object to work?