Приложение аварийно завершает работу AsyncTask при загрузке JSON с удаленного сервера

mvasco спросил: 07 октября 2018 в 12:59 в: android

Я работаю над приложением для Android и загружаю строку JSON с удаленного сервера. Это моя функция:

private void getAddressesFromDB() {    spinner.setVisibility(View.VISIBLE);    calendarios.clear();   this is line 218->  AsyncTask<Integer, Void, Void> asyncTask = new AsyncTask<Integer, Void, Void>() {
        @Override
        protected Void doInBackground(Integer... addressesIds) {            OkHttpClient client = new OkHttpClient();            Request request = new Request.Builder().addHeader("Cache-Control", "no-cache")
                    .url("https://...cargar_calendario.php?colegio="+colegio)
                    .build();            try {
                okhttp3.Response response = client.newCall(request).execute();
                Log.d("HOLA ADDRESSES", "ESTOY EN START:CREATE colegio=>"+response.body().string() );                JSONArray array = new JSONArray(response.body().string());<- this is line 237                for (int i = 0; i < array.length(); i++) {                    JSONObject object = array.getJSONObject(i);                    Calendario calendario = new Calendario(object.getString("holidayDescr"),object.getString("dt"), object.getString("lectivo"));                }            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }        @Override
        protected void onPostExecute(Void aVoid) {            spinner.setVisibility(View.GONE);
        }
    };    asyncTask.execute();
}

Это полученная строка JSON:

[{"dt":"2018-01-01","holidayDescr":"Lectivo","lectivo":"1"},{"dt":"2018-01-02","holidayDescr":"Lectivo","lectivo":"1"},{"dt":"2018-01-03","holidayDescr":"Lectivo","lectivo":"1"},{"dt":"2018-01-04","holidayDescr":"Lectivo","lectivo":"1"},{"dt":"2018-01-05","holidayDescr":"Lectivo","lectivo":"1"},{"dt":"2018-01-06","holidayDescr":"","lectivo":"0"},{"dt":"2018-01-07","holidayDescr":"","lectivo":"0"},{"dt":"2018-01-08","holidayDescr":"Lectivo","lectivo":"1"},{"dt":"2018-01-09","holidayDescr":"Lectivo","lectivo":"1"},{"dt":"2018-01-10","holidayDescr":"Lectivo","lectivo":"1"}]

И это сообщение об исключении:

    --------- beginning of crash
2018-11-01 03:31:21.028 32425-592/com.juarezserver.pupilam E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    Process: com.juarezserver.pupilam, PID: 32425
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:325)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at java.lang.Thread.run(Thread.java:761)
     Caused by: java.lang.IllegalStateException: closed
        at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:408)
        at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:402)
        at okhttp3.internal.Util.bomAwareCharset(Util.java:432)
        at okhttp3.ResponseBody.string(ResponseBody.java:174)
        at com.juarezserver.pupilam.fragment.CalendarioFragment$1.doInBackground(CalendarioFragment.java:237)
        at com.juarezserver.pupilam.fragment.CalendarioFragment$1.doInBackground(CalendarioFragment.java:218)
        at android.os.AsyncTask$2.call(AsyncTask.java:305)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
        at java.lang.Thread.run(Thread.java:761) 

Что не так в моем коде?

0 ответов