I have a txt file on the site from which I read information.
With this class, I read this text and break it into two lines:
/**---------------------КЛАСС ДЛЯ СЧИТЫВАНИЕ ТЕКСТА ИЗ ФАЙЛА-----------**/ public final class AsynchronousGet { private final OkHttpClient client = new OkHttpClient(); public void run() throws Exception { Request request = new Request.Builder() .url("http://s***itus.com/hodite/text.txt") .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Override public void onResponse(Call call, Response response) throws IOException { try (ResponseBody responseBody = response.body()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); Headers responseHeaders = response.headers(); /* for (int i = 0, size = responseHeaders.size(); i < size; i++) { System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i)); }*/ Log.i("Good",responseBody.string()); // System.out.print(responseBody.string().toString());//СЧИТАННЫЙ ТЕКСТ String string=responseBody.string(); int count=0; String[] strings=new String[1]; for (String str : string.split("\n")) { Log.i("str",str); strings[count]=str; count++; } //pushText(strings[0],strings[1]); //(responseBody.string()); } } }); } } /**------------------------------------------------------------------**/ This is the text that I read:
But I constantly get a mistake:
11-29 17:24:31.316 6980-6980/com.example.com.shcherbuk I/Good: good 11-29 17:24:31.486 6980-7013/com.example.com.shcherbuk I/Good: Привет! google.com 11-29 17:24:31.488 6980-7013/com.example.com.shcherbuk E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher Process: com.example.com.shcherbuk, PID: 6980 java.lang.IllegalStateException: closed at okhttp3.internal.http.Http1xStream$FixedLengthSource.read(Http1xStream.java:379) at okio.Buffer.writeAll(Buffer.java:956) at okio.RealBufferedSource.readByteArray(RealBufferedSource.java:92) at okhttp3.ResponseBody.bytes(ResponseBody.java:83) at okhttp3.ResponseBody.string(ResponseBody.java:109) at com.example.com.shcherbuk.MainActivity$AsynchronousGet$1.onResponse(MainActivity.java:173) at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133) at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:33) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818) Swears at this line:
String string=responseBody.string(); Help me fix the code, what am I doing wrong ?!
