2
votes

I'm having some issues with JSON in Android Studio I've been watching and reading about some examples, but still can't figure out how to pass those data from database into "Movie" class, as its shown in Java code.

Here is the log where error begins, and its shown that he is reading server and getting data in log on the first line.

private void getMoviesFromDB(int id) {

    AsyncTask<Integer, Void, Void> asyncTask = new AsyncTask<Integer, Void, Void>() {
        @org.jetbrains.annotations.Nullable
        @Override
        protected Void doInBackground(Integer... movieIds) {

            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url("http://192.168.8.101/databaseproject/getMenu.php?category=" + category)
                    .build();
            try {
                Response response = client.newCall(request).execute();
                Log.d(TAG, "response: " + response.body().string());
                JSONArray array = new JSONArray(response.body().string());

                for (int i = 0; i < array.length(); i++) {

                    JSONObject object = array.getJSONObject(i);

                    Movie movie = new Movie(object.getInt("food_id"), object.getString("food_title"),
                            object.getString("food_image"), object.getString("food_description"), object.getString("food_price"));

                    MainActivity.this.movies.add(movie);
                }


            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

Log says that error is at MainActivity at line 139 and 127

139: JSONArray array = new JSONArray(response.body().string());

127: AsyncTask asyncTask = new AsyncTask() { ...

D/MILJ: response: [{"food_id":"2","food_title":"indeks","food_image":"http://indexhouse.rs/wp-content/uploads/2014/12/sendvic-prsuta.jpg","food_description":"sendvic","food_price":"200","food_category":"snacks"}]

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 Process: com.example.myapplicationccccccc, PID: 21227 java.lang.RuntimeException: An error occurred while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:353) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383) at java.util.concurrent.FutureTask.setException(FutureTask.java:252) at java.util.concurrent.FutureTask.run(FutureTask.java:271) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:764) Caused by: java.lang.IllegalStateException: closed at okhttp3.internal.http.Http1xStream$FixedLengthSource.read(Http1xStream.java:374) at okio.Buffer.writeAll(Buffer.java:993) at okio.RealBufferedSource.readByteArray(RealBufferedSource.java:106) at okhttp3.ResponseBody.bytes(ResponseBody.java:128) at okhttp3.ResponseBody.string(ResponseBody.java:154) at com.example.myapplicationccccccc.MainActivity$1.doInBackground(MainActivity.java:139) at com.example.myapplicationccccccc.MainActivity$1.doInBackground(MainActivity.java:127) at android.os.AsyncTask$2.call(AsyncTask.java:333) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)  at java.lang.Thread.run(Thread.java:764)  D/OpenGLRenderer: eglDestroySurface = 0x7250ce87b0

Can someone please help me?I don't understand how to solve this JSON particular case...

Thanks in advance.

2

2 Answers

0
votes

You probably want to put the response into a String instead of JSONArray, then create a JSONObject with that String and go from there.

Can you paste the JSON?

0
votes

1, As @miljan said, you should paste your JSON data here to let us know the structure of the data. Use online JSON beautifier like json.cn to help figure it out.

2, After knowing the structure of the data, use libraries like GSON can reduce the pain of parsing JSON data.