Ошибка flush() [java]
Возникла проблема. Не получается передать JSON, так как происходит исключение java.io.IOException: closed
Ошибка:
2020-08-06 01:45:26.261 1340-1457/com.example.cargarage W/System.err: java.io.IOException: closed
2020-08-06 01:45:26.262 1340-1457/com.example.cargarage W/System.err: at com.android.okhttp.okio.RealBufferedSink$1.write(RealBufferedSink.java:196)
2020-08-06 01:45:26.262 1340-1457/com.example.cargarage W/System.err: at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
2020-08-06 01:45:26.262 1340-1457/com.example.cargarage W/System.err: at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
2020-08-06 01:45:26.262 1340-1457/com.example.cargarage W/System.err: at java.io.FilterOutputStream.close(FilterOutputStream.java:158)
2020-08-06 01:45:26.262 1340-1457/com.example.cargarage W/System.err: at com.example.cargarage.NewsActivity$AsyncRequest.doInBackground(NewsActivity.java:123)
2020-08-06 01:45:26.262 1340-1457/com.example.cargarage W/System.err: at com.example.cargarage.NewsActivity$AsyncRequest.doInBackground(NewsActivity.java:86)
2020-08-06 01:45:26.262 1340-1457/com.example.cargarage W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:333)
2020-08-06 01:45:26.262 1340-1457/com.example.cargarage W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2020-08-06 01:45:26.262 1340-1457/com.example.cargarage W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
2020-08-06 01:45:26.262 1340-1457/com.example.cargarage W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2020-08-06 01:45:26.262 1340-1457/com.example.cargarage W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2020-08-06 01:45:26.262 1340-1457/com.example.cargarage W/System.err: at java.lang.Thread.run(Thread.java:764)
Код:
class AsyncRequest extends AsyncTask<String, Integer, String> {
HttpURLConnection urlConnection;
BufferedOutputStream bos;
protected String doInBackground(String... arg) {
try {
URL url = new URL(GlobalVariable.server + "news");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.connect();
JSONObject jo = new JSONObject();
jo.put("User_id", GlobalVariable.ID);
bos = new BufferedOutputStream(urlConnection.getOutputStream());
bos.write(jo.toString().getBytes());
String result = urlConnection.getResponseMessage();
Log.d("", "server response: " + result); //проверить, что вернет сервер
} catch (JSONException | IOException e) {
e.printStackTrace();
} finally {
try {
bos.flush(); //очищает поток output-a
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
//urlConnection.disconnect();
}
return null;
}
}