0

I am using okhttp to do http get requests. I have to retrieve body of a url and use it as a string for a TextView

My code -

new Thread(new Runnable() {
    @Override
    public void run() {
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(myURL)
                .build();

        try {
            try (final Response response = client.newCall(request).execute()) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            t.setText(response.body().string());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}).start();

And after launching activity app is crashing

error -

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.vedang.sadhguru, PID: 8677
    java.lang.IllegalStateException: Inflater has been closed
        at java.util.zip.Inflater.ensureOpen(Inflater.java:397)
        at java.util.zip.Inflater.inflate(Inflater.java:258)
        at okio.InflaterSource.readOrInflate(InflaterSource.kt:79)
        at okio.InflaterSource.read(InflaterSource.kt:49)
        at okio.GzipSource.read(GzipSource.kt:69)
        at okio.Buffer.writeAll(Buffer.kt:1642)
        at okio.RealBufferedSource.readString(RealBufferedSource.kt:95)
        at okhttp3.ResponseBody.string(ResponseBody.kt:187)
        at com.vedang.sadhguru.VideoList$1$1.run(VideoList.java:48)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
4
  • 1
    Did you check this question ? Commented Apr 13, 2021 at 11:27
  • I solved it myself but I am unable to post solution here. Commented Apr 13, 2021 at 12:09
  • 1
    Now you should be able to answer your own question. More details Commented Apr 13, 2021 at 12:13
  • Thanks @code_mechanic I am sharing solution. Commented Apr 13, 2021 at 12:16

1 Answer 1

1

I have solved it.

    new Thread(new Runnable() {
        @Override
        public void run() {
            OkHttpClient client = new OkHttpClient();

            Request request = new Request.Builder()
                    .url(myURL)
                    .build();
            try {
                try (final Response response = client.newCall(request).execute()) {
                    ChangeText(response.body().string());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }).start();    
}

public void ChangeText(final String text) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            t.setText(text);
        }
    });
}

created ChangeText void and ran it on ui thread

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.