1

I'm attempting to parse a string that contains an array of JSON objects, but the org.json.JSONArray is not supported until the API 19 (Kit-Kat) operating system. For obvious reasons I need to figure out a way around this. Is there a better alternative to this? Or am I using this method incorrectly?

Here is the code that keeps telling me I need API 19 or higher:

    protected void onPostExecute(JSONArray result) {
        pDialog.dismiss();
        try {
            // Getting JSON Array from URL
            info = new JSONArray(result);
            for(int i = 0; i < info.length(); i++){
                JSONObject c = info.getJSONObject(i);

                // Storing  JSON item in a Variable
                String title = c.getString(TAG_TITLE);
                String article = c.getString(TAG_ARTICLE);
                String timestamp = c.getString(TAG_TIMESTAMP);
                String datestring = c.getString(TAG_DATESTRING);

                // Adding value HashMap key => value
                HashMap<String, String> map = new HashMap<String, String>();
                map.put(TAG_TITLE, title);
                map.put(TAG_ARTICLE, article);
                map.put(TAG_TIMESTAMP, timestamp);
                map.put(TAG_DATESTRING, datestring);

                oslist.add(map);
                list=(ListView)findViewById(R.id.list);

                ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
                        R.layout.list_v,
                        new String[] { TAG_TITLE,TAG_ARTICLE, TAG_TIMESTAMP,TAG_DATESTRING }, new int[] {
                        R.id.title,R.id.article, R.id.timestamp,R.id.date_string});

                list.setAdapter(adapter);
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
                        Toast.makeText(MainActivity.this, "You Clicked at "+oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

btw I am using an AsyncTask to put the information into a ListView. I have another class to fetch the result of the webpage. Thanks!

3
  • What makes you think that org.json.JSONArray is only supported in API 19? Commented Nov 14, 2013 at 18:13
  • What @SteveBenett basically is saying: JSONArray has been around since API level 1. Commented Nov 14, 2013 at 18:31
  • Eclipse is telling me that JSONArray is only supported in API 19 :( Commented Nov 14, 2013 at 23:00

3 Answers 3

4

The new API 19 function you are using is:

info = new JSONArray(result);

Since result is already an JSONArray, why do you need to create another?

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

1 Comment

WOWOWOWOWOWOWOWOWOW!!!!!!!!!!!!!! You are right, I had no reason to create a JSONArray from (what already was) a JSONArray! I'm such a noob :) btw it works now
0

Dude try this framework is much better

https://code.google.com/p/google-gson/

or

http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html

Comments

0

Here is a solution under 19API lvl:

First of all. Make a Gson obj. --> Gson gson = new Gson();

Second step is get your jsonObj as String with StringRequest(instead of JsonObjectRequest)

The last step to get JsonArray...

YoursObjArray[] yoursObjArray = gson.fromJson(response, YoursObjArray[].class);

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.