2

I am trying to get a response from my server using restful services, I get it back in json format, change to a string variable then am trying to change from a string variable to json object, but I am getting an error here is my class and after my class is my stacktrace and then my string variable, the error occurs when i try to convert from string to json object, thanks for your help:

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            String response = "";
            for (String url : urls) {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try {
                    HttpResponse execute = client.execute(httpGet);
                    InputStream content = execute.getEntity().getContent();

                    BufferedReader buffer = new BufferedReader(
                            new InputStreamReader(content));
                    String s = "";
                    while ((s = buffer.readLine()) != null) {
                        response += s;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            try {
                //JSONObject json = new JSONObject(response);
                JSONObject data = new JSONObject(response).getJSONObject("type");

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return response;
        }

11-14 09:53:26.192: W/System.err(560): org.json.JSONException: Value [{"type":"HumVideoMovingImage","location":"http://hummedia.byu.edu/watch/humvideo:0373889/","label":"Harry Potter und der Orden des Phonix","videoinfo":{"id":"humvideo:0373889","title":"Harry Potter und der Orden des Phonix","height":480,"coverage":"BYU","status":"allowed","subject":"Harry Potter,magic","description":"Lord Voldemort ist zuruckgekehrt, doch das Zaubereiministerium tut alles, um diese Tatsache der Geme ...","width":716,"rights":"shared","language":["de"],"runningtime":7200,"date":"2007-04-11"}},{"type":"HumVideoMovingImage","location":"http://hummedia.byu.edu/watch/humvideo:0330373/","label":"Harry Potter und der Feuerkelch","videoinfo":{"id":"humvideo:0330373","title":"Harry Potter und der Feuerkelch","height":480,"coverage":"BYU","status":"allowed","subject":"Harry Potter,magic","description":"Das grosse Abenteuer beginnt, als der Feuerkelch Harry Potters Namen freigibt und Harry damit Teilne ...","width":716,"rights":"shared","language":["de"],"runningtime":7200,"date":"2005-11-18"}},{"type":"HumVideoMovingImage","location":"http://hummedia.byu.edu/watch/humvideo:0304141/","label":"Harry Potter und der Gefangene von Askaban","videoinfo":{"id":"humvideo:0304141","title":"Harry Potter und der Gefangene von Askaban","height":480,"coverage":"BYU","status":"allowed","subject":"Harry Potter,magic","description":"Wahrend die abscheuliche Tante Magda uber den Nachthimmel schwebt, kehrt Harry nach Hogwarts zuruck, ...","width":716,"rights":"shared","language":["de"],"runningtime":7200,"date":"2003-06-04"}},{"type":"HumVideoMovingImage","location":"http://hummedia.byu.edu/watch/humvideo:0295297/","label":"Harry Potter und die Kammer Desschreckens","videoinfo":{"id":"humvideo:0295297","title":"Harry Potter und die Kammer Desschreckens","height":480,"coverage":"BYU","status":"allowed","subject":"Harry Potter,magic","description":"Fliegende Autos, gemeingefahrliche Baume und die Warnung eines mysteriosen Hauselfen - so beginnt fu ...","width":716,"rights":"shared","language":["de"],"runningtime":7200,"date":"2002-11-15"}},{"type":"HumVideoMovingImage","location":"http://hummedia.byu.edu/watch/humvideo:0241527/","label":"Harry Potter und der Stein der Weisen","videoinfo":{"id":"humvideo:0241527","title":"Harry Potter und der Stein der Weisen","height":480,"coverage":"BYU","status":"allowed","subject":"Harry Potter,magic","description":"In der zauberhaften Verfilmung J.K. Rowlings Weltbestseller erfahrt Harry Potter an seinem 11. Gebur ...","width":716,"rights":"shared","language":["de"],"runningtime":7200,"date":"2001-11-16"}}] of type org.json.JSONArray cannot be converted to JSONObject 11-14 09:53:26.232: W/System.err(560): at org.json.JSON.typeMismatch(JSON.java:111) 11-14 09:53:26.242: W/System.err(560): at org.json.JSONObject.(JSONObject.java:158) 11-14 09:53:26.252: W/System.err(560): at org.json.JSONObject.(JSONObject.java:171) 11-14 09:53:26.262: W/System.err(560): at de.vogella.android.asynctask.ReadWebpageAsyncTask$DownloadWebPageTask.doInBackground(ReadWebpageAsyncTask.java:56) 11-14 09:53:26.332: W/System.err(560): at de.vogella.android.asynctask.ReadWebpageAsyncTask$DownloadWebPageTask.doInBackground(ReadWebpageAsyncTask.java:1) 11-14 09:53:26.352: W/System.err(560): at android.os.AsyncTask$2.call(AsyncTask.java:264) 11-14 09:53:26.372: W/System.err(560): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 11-14 09:53:26.382: W/System.err(560): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 11-14 09:53:26.392: W/System.err(560): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) 11-14 09:53:26.402: W/System.err(560): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 11-14 09:53:26.412: W/System.err(560): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 11-14 09:53:26.422: W/System.err(560): at java.lang.Thread.run(Thread.java:856) 11-14 09:53:34.635: D/dalvikvm(169): GC_CONCURRENT freed 396K, 7% free 7350K/7879K, paused 21ms+9ms

string variable:

[ { "type": "HumVideoMovingImage",
"videoinfo": { "status": "allowed",
"description": "Lord Voldemort ist zuruckgekehrt, doch das Zaubereiministerium tut alles, um diese Tatsache der Geme ...",
"height": 480, "coverage": "BYU", "date": "2007-04-11", "id": "humvideo:0373889",
"subject": "Harry Potter,magic", "language": ["de"],
"rights": "shared", "title": "Harry Potter und der Orden des Phonix", "runningtime": 7200, "width": 716 }, "location": "http://hummedia.byu.edu/watch/humvideo:0373889/", "label": "Harry Potter und der Orden des Phonix" }, {
"type": "HumVideoMovingImage", "videoinfo": {
"status": "allowed", "description": "Das grosse Abenteuer beginnt, als der Feuerkelch Harry Potters Namen freigibt und Harry damit Teilne ...", "height": 480, "coverage": "BYU", "date": "2005-11-18", "id": "humvideo:0330373", "subject": "Harry Potter,magic",
"language": ["de"], "rights": "shared",
"title": "Harry Potter und der Feuerkelch",
"runningtime": 7200, "width": 716 },
"location": "http://hummedia.byu.edu/watch/humvideo:0330373/",
"label": "Harry Potter und der Feuerkelch" }, { "type": "HumVideoMovingImage", "videoinfo": {
"status": "allowed", "description": "Wahrend die abscheuliche Tante Magda uber den Nachthimmel schwebt, kehrt Harry nach Hogwarts zuruck, ...", "height": 480,
"coverage": "BYU", "date": "2003-06-04",
"id": "humvideo:0304141", "subject": "Harry Potter,magic", "language": ["de"], "rights": "shared", "title": "Harry Potter und der Gefangene von Askaban", "runningtime": 7200, "width": 716
}, "location": "http://hummedia.byu.edu/watch/humvideo:0304141/", "label": "Harry Potter und der Gefangene von Askaban" },
{ "type": "HumVideoMovingImage", "videoinfo": { "status": "allowed", "description": "Fliegende Autos, gemeingefahrliche Baume und die Warnung eines mysteriosen Hauselfen - so beginnt fu ...", "height": 480, "coverage": "BYU", "date": "2002-11-15", "id": "humvideo:0295297", "subject": "Harry Potter,magic",
"language": ["de"], "rights": "shared",
"title": "Harry Potter und die Kammer Desschreckens",
"runningtime": 7200, "width": 716 },
"location": "http://hummedia.byu.edu/watch/humvideo:0295297/",
"label": "Harry Potter und die Kammer Desschreckens" },
{ "type": "HumVideoMovingImage", "videoinfo": { "status": "allowed", "description": "In der zauberhaften Verfilmung J.K. Rowlings Weltbestseller erfahrt Harry Potter an seinem 11. Gebur ...", "height": 480, "coverage": "BYU", "date": "2001-11-16", "id": "humvideo:0241527", "subject": "Harry Potter,magic",
"language": ["de"], "rights": "shared",
"title": "Harry Potter und der Stein der Weisen",
"runningtime": 7200, "width": 716 },
"location": "http://hummedia.byu.edu/watch/humvideo:0241527/",
"label": "Harry Potter und der Stein der Weisen" }

    ]

by trying to use the following code: JSONObject jObject = new JSONObject(response);

I still get an error, here is the stack trace:

11-14 10:03:02.713: W/System.err(622): org.json.JSONException: Value [{"type":"HumVideoMovingImage","location":"http://hummedia.byu.edu/watch/humvideo:0373889/","label":"Harry Potter und der Orden des Phonix","videoinfo":{"id":"humvideo:0373889","title":"Harry Potter und der Orden des Phonix","height":480,"coverage":"BYU","status":"allowed","subject":"Harry Potter,magic","description":"Lord Voldemort ist zuruckgekehrt, doch das Zaubereiministerium tut alles, um diese Tatsache der Geme ...","width":716,"rights":"shared","language":["de"],"runningtime":7200,"date":"2007-04-11"}},{"type":"HumVideoMovingImage","location":"http://hummedia.byu.edu/watch/humvideo:0330373/","label":"Harry Potter und der Feuerkelch","videoinfo":{"id":"humvideo:0330373","title":"Harry Potter und der Feuerkelch","height":480,"coverage":"BYU","status":"allowed","subject":"Harry Potter,magic","description":"Das grosse Abenteuer beginnt, als der Feuerkelch Harry Potters Namen freigibt und Harry damit Teilne ...","width":716,"rights":"shared","language":["de"],"runningtime":7200,"date":"2005-11-18"}},{"type":"HumVideoMovingImage","location":"http://hummedia.byu.edu/watch/humvideo:0304141/","label":"Harry Potter und der Gefangene von Askaban","videoinfo":{"id":"humvideo:0304141","title":"Harry Potter und der Gefangene von Askaban","height":480,"coverage":"BYU","status":"allowed","subject":"Harry Potter,magic","description":"Wahrend die abscheuliche Tante Magda uber den Nachthimmel schwebt, kehrt Harry nach Hogwarts zuruck, ...","width":716,"rights":"shared","language":["de"],"runningtime":7200,"date":"2003-06-04"}},{"type":"HumVideoMovingImage","location":"http://hummedia.byu.edu/watch/humvideo:0295297/","label":"Harry Potter und die Kammer Desschreckens","videoinfo":{"id":"humvideo:0295297","title":"Harry Potter und die Kammer Desschreckens","height":480,"coverage":"BYU","status":"allowed","subject":"Harry Potter,magic","description":"Fliegende Autos, gemeingefahrliche Baume und die Warnung eines mysteriosen Hauselfen - so beginnt fu ...","width":716,"rights":"shared","language":["de"],"runningtime":7200,"date":"2002-11-15"}},{"type":"HumVideoMovingImage","location":"http://hummedia.byu.edu/watch/humvideo:0241527/","label":"Harry Potter und der Stein der Weisen","videoinfo":{"id":"humvideo:0241527","title":"Harry Potter und der Stein der Weisen","height":480,"coverage":"BYU","status":"allowed","subject":"Harry Potter,magic","description":"In der zauberhaften Verfilmung J.K. Rowlings Weltbestseller erfahrt Harry Potter an seinem 11. Gebur ...","width":716,"rights":"shared","language":["de"],"runningtime":7200,"date":"2001-11-16"}}] of type org.json.JSONArray cannot be converted to JSONObject 11-14 10:03:02.732: W/System.err(622): at org.json.JSON.typeMismatch(JSON.java:111) 11-14 10:03:02.752: W/System.err(622): at org.json.JSONObject.(JSONObject.java:158) 11-14 10:03:02.762: W/System.err(622): at org.json.JSONObject.(JSONObject.java:171) 11-14 10:03:02.822: W/System.err(622): at de.vogella.android.asynctask.ReadWebpageAsyncTask$DownloadWebPageTask.doInBackground(ReadWebpageAsyncTask.java:56) 11-14 10:03:02.842: W/System.err(622): at de.vogella.android.asynctask.ReadWebpageAsyncTask$DownloadWebPageTask.doInBackground(ReadWebpageAsyncTask.java:1) 11-14 10:03:02.852: W/System.err(622): at android.os.AsyncTask$2.call(AsyncTask.java:264) 11-14 10:03:02.872: W/System.err(622): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 11-14 10:03:02.892: W/System.err(622): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 11-14 10:03:02.902: W/System.err(622): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) 11-14 10:03:02.912: W/System.err(622): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 11-14 10:03:02.922: W/System.err(622): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 11-14 10:03:02.942: W/System.err(622): at java.lang.Thread.run(Thread.java:856)

thank you.

2 Answers 2

5

You simple have to use below code

import org.json.JSONObject;

JSONObject jObject = new JSONObject(response);

For rest you can process in the same way

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

3 Comments

I'm sorry I forgot to mention I do have the import: "import org.json.JSONObject; "
I tried the above solution but i still get an error, I have updated my question with new stack trace. thank you.
could you please try something like JSONArray dataArray = new JSONArray(response); JSONObject menuObject = dataArray.getJSONObject(0); menuObject.getString("type")
2

The JSON returned from the server is an Array object, and the stack trace clearly states that. Try using a org.json.JSONArray in place of an org.json.JSONObject:

org.json.JSONArray jsonArray = new org.json.JSONArray(response);

1 Comment

look in your stack trace for this line: type org.json.JSONArray cannot be converted to JSONObject 11-14 10:03:02.732: W/System.err(622): at org.json.JSON.typeMismatch(JSON.java:111)

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.