Sarah Boland
Sarah Boland

Reputation: 87

Parsing JSON - JSONException: Integer cannot be converted to JSONObject

I'm trying to display data from a JSON file into a Listview, but I'm getting the following parsing error.

Here is my error:

An error occured while executing doInBackground()

07-25 16:01:00.754: E/JSON Parser(775): Error parsing data org.json.JSONException: Value 401 of type java.lang.Integer cannot be converted to JSONObject

Here is my JSON file:

{
    "people": [
        {
            "place": "1",
            "person": "Lisa",
            "mark": "10"
        },
        {
            "place": "2",
            "person": "Danny",
            "mark": "9"
        },
        {
            "place": "a3",
            "person": "Marc",
            "thur": "13",
            "mark": "8",
            "total": "7"
        },
        {
            "place": "b3",
            " person": "Paul ",
            "mark": "8"
        },
       {
           "place": "c5",
           "person": "Eddie",
           "thur": "8",
        "mark": "7",
        "total": "5"
       }

    ],
   "game": "First Game",
  "date": "Jul 30 2015"
 }

Here is my code:

public class InboxActivity extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;

// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();

ArrayList<HashMap<String, String>> inboxList;

// products JSONArray
JSONArray inbox = null;

// Inbox JSON url
private static final String INBOX_URL = "https://www.url.com/api/v2/projects/xxx/last_ready_run/data?api_key=xxx";

// ALL JSON node names
private static final String TAG_MESSAGES = "people";
private static final String TAG_ID = "place";
private static final String TAG_FROM = "person";
private static final String TAG_EMAIL = "thur";
private static final String TAG_SUBJECT = "mark";
private static final String TAG_DATE = "total";


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.inbox_list);

    // Hashmap for ListView
    inboxList = new ArrayList<HashMap<String, String>>();

    // Loading INBOX in Background Thread
    new LoadInbox().execute();
}

/**
 * Background Async Task to Load all INBOX messages by making HTTP Request
 * */
class LoadInbox extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(InboxActivity.this);
        pDialog.setMessage("Loading Inbox ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting Inbox JSON
     * */
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        // getting JSON string from URL
        JSONObject json = jsonParser.makeHttpRequest(INBOX_URL, "GET",
                params);



        // Check your log cat for JSON reponse
        Log.d("Inbox JSON: ", json.toString());

        try {
            inbox = json.getJSONArray(TAG_MESSAGES);

            inbox.toString();

            // looping through All messages
            for (int i = 0; i < inbox.length(); i++) {
                JSONObject c = inbox.getJSONObject(i);

                // Storing each json item in variable
                String id = c.getString(TAG_ID);
                String from = c.getString(TAG_FROM);
            String subject = c.getString(TAG_SUBJECT);
            String date = c.getString(TAG_DATE);

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_ID, id);
                map.put(TAG_FROM, from);
            map.put(TAG_SUBJECT, subject);
            map.put(TAG_DATE, date);

                // adding HashList to ArrayList
                inboxList.add(map);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
             public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            InboxActivity.this, inboxList,
                            R.layout.inbox_list_item, new String[] { TAG_FROM, TAG_SUBJECT, TAG_DATE },
                            new int[] { R.id.from, R.id.subject, R.id.date });
                    // updating listview
                    setListAdapter(adapter);
                }
        });

    }

}

Upvotes: 0

Views: 1129

Answers (1)

Victory
Victory

Reputation: 5890

You have not show the exact url that you are parsing form, but the problem is not the parser it is that you need to have Authorization (need to be logged in) to get the real JSON from that page.

You are likely getting a 401 Authorization Required page at that url. The jsonParser is then trying to parse the string "401 Authorization Required" string which is not valid JSON. You can verify this by opening a private browser window and visiting the url directly.

Upvotes: 1

Related Questions