0

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);
                }
        });

    }

}
6
  • You've posted a lot of code, but I suspect most of it isn't relevant. The value 401 doesn't occur in the JSON you've posted, either. If you could post a short but complete program demonstrating the problem, that would make it easier to help... Commented Jul 25, 2015 at 20:15
  • Thanks Jon , I'll try to explain the issue clearer, I always get that 401 even if I use a different JSON it's confusing. Commented Jul 25, 2015 at 20:18
  • And where exactly is the exception being thrown? (Again, you've got a lot of post - if you could show a lot less code, just the relevant code, it would be easier to help you.) Commented Jul 25, 2015 at 20:19
  • I've a sneaking suspicion the 401 is actually an HTTP Unauthorized status code, btw... Commented Jul 25, 2015 at 20:20
  • The error occurs while executing in doInBackground(), I felt I needed to include more code to explain more clearly what Im trying to achieve, sorry if it's too much. Commented Jul 25, 2015 at 20:24

1 Answer 1

1

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.

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

8 Comments

Thanks Victory that crossed my mind a few minutes ago, The url is viewable on my browser, but mmmm that's interesting.
Did you try in a private window? or if you are using linux did you try wget http://url.com/blahblahblah
I tired both, seems ok :/
Can you run the code in debugger mode, put a breakpoint at the makeHttpRequest and step into the function to try to see exactly what is returned by the server? It could also be that the remote server doesn't like your user-agent string or something.
The server seems to be blocking any makeHttpRequests, not sure how i'd get around that
|

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.