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:

enter image description here

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 ?!

  • The separator used in the file may vary. It may be, for example, '\ n', '\ r \ n', '\ r' or some Unicode characters ( ru.wikipedia.org/wiki/… ). I think that my own solution would be to use regular expressions. - Mikhail Ionkin Nov.
  • @MikhailIonkin that is, do you propose to put some separator, format "/" and split it using split? Exactly) - shcherbuk
  • "1 \ n 2 \ r \ n 3 \ r 4 \\ u000A 5" .split ("\\ R"). Length returns 4. That is, the \ u000A symbol is poorly handled here - Mikhail Ionkin Nov.
  • one
    I have never worked with this liby, but there is an assumption that the string method does not just return a string, but reads data from somewhere else. And judging by the source code it is. Accordingly, your problem is that you called this method twice. The first time when logging, the second time already when writing to a variable. - Uraty
  • one
    Not. Data is read from the stream, and after reading the stream is closed. Do not read from it again. You must memorize the string after calling the .string () method - Mikhail Ionkin

0