2

I'm trying to create a JSONArray in a file that is in a linked Folder to a Android Project, and eclipse is suddenly (didn't do this before) giving me the error : Call requires API level 19 (current min is 14): new org.json.JSONArray

the code where it happens is as following:

String[] s = (save_val.trim().equals(""))?new String[]{}:save_val.split("\n");
JSONObject o = null;
if (!Arrays.equals(s, save_names)){

    new JSONArray(s); // this is where the error is shown

EDIT: SOLUTION the method in fact isn't introduced before API 19, but if you use (as in my case) String array or other promitives you can simply do:

    JSONArray s_values = new JSONArray();
    for (int i = 0; i < s.length; i++){
        s_values.put(yourarray[i]);
    }

this shouldn't be any slower

1
  • Seems like your SDK version is off. Commented Mar 13, 2014 at 17:23

3 Answers 3

7

Looks like the constructor JSONArray(Object array) was not introduced until API level 19.

http://developer.android.com/reference/org/json/JSONArray.html#JSONArray(java.lang.Object)

You can change your min API version in your manifest to be 19 or

you can convert your Object array to a Collection array like ArrayList and use the Collection array constructor of JSONArray which is available from API 1.

Good Luck!

Sign up to request clarification or add additional context in comments.

1 Comment

DejanRistic is right. Just bear in mind that jsonObject.put("xyz", new JSONArray(Arrays.asList(floatArray))) will give you an array containing your e.g. float array ({ "xyz": [[1.0, 2.0, 3.0]] }), while JSONArray arr = new JSONArray(); for (float value : floatArray) { arr.put(value); }; jsonObject.put("xyz", arr) would in turn provide you just with an array, as you'd expect ({ "xyz": [1.0, 2.0, 3.0] }).
0

You're constructing the JSONArray with an array of strings, which according to the documentation is only available in API level 19 or higher. You'll either need to use an alternative constructor or change the minimum API level to 19 in your manifest to make the constructor available.

2 Comments

where can i see the level in which this was introduced (in the documentation)? (just found it, small on the right side of the grey method declaration)
If you look in the grey title bar with the method name in it, the API level it was added in is on the right-hand side :)
0

You can use the alternative constructor by providing a collection. This will create an JSONArray by copying all values given in the collection.

JSONArray arr = new JSONArray(collection);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.