0

I'm trying to retrieve data back from my database as JSON, and set that result to a JSONObject which then displays in a textview.

Here is the code:

private class ShowDBActivity3 extends AsyncTask<Void, Void, String>{

    protected String doInBackground(Void... voids){
        String s = "";
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

        nameValuePairs.add(new BasicNameValuePair("username", username));

        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("URL");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            Log.e("pass 1", "connection success ");
        } catch (Exception e) {
            Log.e("Fail 1", e.toString());
            Toast.makeText(getApplicationContext(), "Invalid IP Address",
                    Toast.LENGTH_LONG).show();
        }

        try {
            BufferedReader reader = new BufferedReader
                    (new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
            Log.e("pass 2", "connection success ");
        } catch (Exception e) {
            Log.e("Fail 2", e.toString());
        }


        try {
            Log.e("This is the result: ", result.toString());
            JSONObject jArray = new JSONObject(result);
            String title = jArray.getString("title");
            String rating = jArray.getString("rating");
            String username = jArray.getString("username");

            s = "Movie: " + title + "\n" +
                "Rating: " + rating + "\n";

            return s;


        } catch (Exception e) {
            Log.e("Fail 3", e.toString());
        }
        return s;
    }

    protected void onPostExecute(String s){
        show.setText(s);
    }
}

And here is the result that gets returned:

This is the result:﹕ [{"0":"16","id":"16","1":"The Hulk","title":"The Hulk","2":"7","rating":"7","3":"tlong3","username":"tlong3"},{"0":"19","id":"19","1":"The Incredible Hulk","title":"The Incredible Hulk","2":"3","rating":"3","3":"tlong3","username":"tlong3"},{"0":"20","id":"20","1":"Testtt","title":"Testtt","2":"6","rating":"6","3":"Tlong3","username":"Tlong3"}]

...which cannot be converted to a JSONObject for some reason. Here is the error:

04-28 13:14:25.372 20099-20830/com.android.movies E/Fail 3﹕ org.json.JSONException: Value [{"3":"tlong3","id":"16","2":"7","username":"tlong3","title":"The Hulk","1":"The Hulk","0":"16","rating":"7"},{"3":"tlong3","id":"19","2":"3","username":"tlong3","title":"The Incredible Hulk","1":"The Incredible Hulk","0":"19","rating":"3"},{"3":"Tlong3","id":"20","2":"6","username":"Tlong3","title":"Testtt","1":"Testtt","0":"20","rating":"6"}] of type org.json.JSONArray cannot be converted to JSONObject

1 Answers1

0

It is quiet clear and the logcat also displays that the JSONArray can not be converted to JSONObject. the string that you are receiving as a result is a JSONArray and not object, you are trying to parse it to JSONObjct which is causing the error.

This is for you if you don't already know. JSONArray can be identified by [ Your JSONArray ] and JSONObject can be identified by { Your JSONObject }

You can use this tools to validate you JSON response and identify what is the format of it. According to you JSONresponse it looks like [ {***} {***} {***} ] which means you have a JSONArray of JSONObjects to parse it correctly you need to first convert it to JSONArray and then apply for loop to go through each object of it. I have answered similar question here and here, you should check those and try similarly.

PS: If you are developing the server script then I would advice you to code you server side such that your response is always a JSONObject rather than JSONArray but this is only if you are the developer of both client and server side. Ask me question regarding those if you need help.

Community
  • 1
  • 1
madteapot
  • 2,208
  • 2
  • 19
  • 32
  • Thank you! For anyone that is interested, here is how I fixed it. JSONArray jArray = new JSONArray(result); s = ""; for(int i = 0; i < jArray.length(); i++){ JSONObject jObject = jArray.getJSONObject(i); String title = jObject.getString("title"); String rating = jObject.getString("rating"); s+= "Movie: " + title + "\n" + "Rating: " + rating + "\n"; } return s; – Daniel Long Apr 28 '14 at 19:34
  • Please consider up-voting or accepting the answer if it has helped you in anyway. Thank you – madteapot Apr 29 '14 at 23:40