1

I'm now trying to get json array with ion (https://github.com/koush/ion) But I get the error: Unhandled exception: org.json.JSONException :(

My json:

[{"title":"Hello world","text":"This is the text"}]

My code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_json);

        Ion.with(MyActivity.this)
                .load(getString(R.string.json_url))
                .asJsonArray()
                .setCallback(new FutureCallback<JSONArray>() {
                    @Override
                    public void onCompleted(Exception e, JSONArray result) {
                        if (e != null) {
                            Toast.makeText(MyJson.this, "Error loading strings", Toast.LENGTH_LONG).show();
                            return;
                        }
                        JSONObject c = result.getJSONObject(0);
                        if (c.has("title")) title = c.getString("title");
                        if (c.has("text")) text = c.getString("text");


                    }
                });

    }

Can someone tell me what I did wrong, please? :(

EDIT:

Error:(48, 17) error: method setCallback in interface Future<T> cannot be applied to given types;
required: FutureCallback<JsonArray>
found: <anonymous FutureCallback<JSONArray>>
reason: actual argument <anonymous FutureCallback<JSONArray>> cannot be converted to FutureCallback<JsonArray> by method invocation conversion
where T is a type-variable:
T extends Object declared in interface Future
Arnaldo
  • 673
  • 6
  • 22
  • Full stacktrace will be helpful: on what line exactly this exception happens? Also may want to add `try/catch` block to handle it anyway. – solar Aug 16 '15 at 18:20
  • `JSONObject c = result.getJSONObject(0);`, `title = c.getString("title");`, `text = c.getString("text");` I'm working with Android Studio. (red underline appears) – Arnaldo Aug 16 '15 at 18:26
  • Well, are you sure that the `result` array is consistent and not empty? Seems to me that something goes wrong while you're trying to do `getJSONObject(0)`. Take a look [here](http://stackoverflow.com/questions/30289189/androidunhandled-exception-org-json-jsonexception) to handle your exception, as I recommend earlier. – solar Aug 16 '15 at 18:34
  • Not solve the problem ) : I get " Unknown class : e " – Arnaldo Aug 16 '15 at 18:42
  • As I said earlier, add full stacktrace to your question, it's hard to help without seeing the whole picture. – solar Aug 16 '15 at 18:44
  • That, right? @solar Thank you for your patience. I'm not very good with English. – Arnaldo Aug 16 '15 at 18:51

1 Answers1

1

I solved the problem guys :)!

I changed: JSONArray (org.json.JSONArray) to: JsonArray (com.google.gson.JsonArray)

And I added this:

JsonObject c = result.get(0).getAsJsonObject();

This is the code now:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_json);

        Ion.with(MyActivity.this)
                .load(getString(R.string.json_url))
                .asJsonArray()
                .setCallback(new FutureCallback<JsonArray>() {
                    @Override
                    public void onCompleted(Exception e, JsonArray result) {
                        if (e != null) {
                            Toast.makeText(MyJson.this, "Error loading strings", Toast.LENGTH_LONG).show();
                            return;
                        }
                        JsonObject c = result.get(0).getAsJsonObject();
                        if (c.has("title")) title = c.get("title").getAsString();
                        if (c.has("text")) text = c.get("text").getAsString();


                    }
                });

    }

This solved the problem!

Arnaldo
  • 673
  • 6
  • 22