0

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?

George Thomas
  • 4,566
  • 5
  • 30
  • 65
Valera Valerianov
  • 247
  • 1
  • 3
  • 14

2 Answers2

0

as it said in the README.md :

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

in onCompleted you have a JsonObject
imagine your json is like this ( you dont tell how is your josn):

{

    "menu":{
        "id":"file",
        "value":"File",
    }

}

you can parse this json like this :

String menu = result.getString("menu"); 
JSONObject jsonMenu = new JSONObject("menu");// get menue as another json object
String id = jsonMenu.getLong("id"); // get a long value from a josn object
Long value = data.getString("value"); // get a string value from josn

its a good Tutorial about json in android :
Android JSON Parser Tutorial

YFeizi
  • 1,498
  • 3
  • 28
  • 50
0

for connection with server and get(or send) json data: its better to use most popular libraies like volley library or okhttp v...(also u can use them together) for compare this libraries see below link: Comparison of Android networking libraries: OkHTTP, Retrofit, and Volley

and u can read about volley library in :

http://developer.android.com/training/volley/index.html

http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

(im work with volley and it support json request)

before send json object to server and after get response from server that is a json object in your case you might want to work with json objects and convert them to your objects and vice-versa you can use JSON library or Jackson library for this according to https://github.com/futurice/android-best-practices :

Jackson is a Java library for converting Objects into JSON and vice-versa. Gson is a popular choice for solving this problem, however we find Jackson to be more performant since it supports alternative ways of processing JSON: streaming, in-memory tree model, and traditional JSON-POJO data binding. Keep in mind, though, that Jackson is a larger library than GSON, so depending on your case, you might prefer GSON to avoid 65k methods limitation. Other alternatives: Json-smart and Boon JSON

you can get JSON library from below link: http://code.google.com/p/google-gson/

but if you want exaclty work with ion library say to edit my answer;)

and sorry for my bad english.

Community
  • 1
  • 1
mk72
  • 1,307
  • 2
  • 8
  • 15