我正在开发一个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 :(得分:1)
使用此
try {
okhttp3.Response response = client.newCall(request).execute();
Log.d("HOLA ADDRESSES", "ESTOY EN START:CREATE colegio=>"+response.body().string() );
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
} else {
JSONArray array = new JSONArray(response);<- 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;
}
答案 1 :(得分:1)
您的日志语句在字符串方法末尾关闭RealBufferedSource,您可以更改日志语句,如下所示。
Log.d("HOLA ADDRESSES", "ESTOY EN START:CREATE colegio=>"+response.body());
理想的解决方案是为您的响应数据创建模型类,并进行更改以从api调用返回数据数组。