How to avoid FATAL EXCEPTION in OkHttp Dispatcher after callback Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to avoid FATAL EXCEPTION in OkHttp Dispatcher after callback without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m getting this exception when performing ui actions after receiving callback from OkHttp.

FATAL EXCEPTION: OkHttp Dispatcher Process: org.example.verificationserviceexample, PID: 20421 android.util.AndroidRuntimeException: Animators may only be run on Looper threads at android.animation.ValueAnimator.cancel(ValueAnimator.java:1020) at android.animation.AnimatorSet.cancel(AnimatorSet.java:366) at android.graphics.drawable.RippleComponent.cancelSoftwareAnimations(RippleComponent.java:214) at android.graphics.drawable.RippleComponent.cancel(RippleComponent.java:133) at android.graphics.drawable.RippleComponent.enter(RippleComponent.java:100) at android.graphics.drawable.RippleDrawable.tryBackgroundEnter(RippleDrawable.java:552) at android.graphics.drawable.RippleDrawable.setBackgroundActive(RippleDrawable.java:290) at android.graphics.drawable.RippleDrawable.onStateChange(RippleDrawable.java:270) at android.graphics.drawable.Drawable.setState(Drawable.java:735) at android.view.View.drawableStateChanged(View.java:18046) at android.widget.TextView.drawableStateChanged(TextView.java:4090) at android.support.v7.widget.AppCompatButton.drawableStateChanged(AppCompatButton.java:155) at android.view.View.refreshDrawableState(View.java:18115) at android.view.View.setEnabled(View.java:8135) at android.widget.TextView.setEnabled(TextView.java:1609) at org.example.verificationserviceexample.MainActivity.renderButtons(MainActivity.java:146) at org.example.verificationserviceexample.MainActivity.onAudioStateChanged(MainActivity.java:170) at com.bssys.spitchmobilesdk.audio.SpitchAudioManager.publishAudioStateChange(SpitchAudioManager.java:123) at com.bssys.spitchmobilesdk.audio.SpitchAudioManager.stopAudio(SpitchAudioManager.java:83) at com.bssys.spitchmobilesdk.VerificationService.stopAudio(VerificationService.java:96) at com.bssys.spitchmobilesdk.VerificationService.stopEnrollment(VerificationService.java:57) at org.example.verificationserviceexample.MainActivity.lambda$null$0$MainActivity(MainActivity.java:60) at org.example.verificationserviceexample.MainActivity$$Lambda$3.onSpitchResult(Unknown Source) at com.bssys.spitchmobilesdk.VerificationService$1.onFailure(VerificationService.java:111) at okhttp3.RealCall$AsyncCall.execute(RealCall.java:161) at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 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)

And this is the code that I’m using:

public void addVoicePrintFromSource(Pipe source, SpitchConfig config, @NonNull String speakerId, Callback callback){

        RequestBody requestBody = new RequestBody() {
            @Override
            public MediaType contentType() {
                return MediaType.parse("audio/x-pcm; rate="+config.getRate());
            }

            @Override
            public void writeTo(BufferedSink sink) throws IOException {
               sink.writeAll(source.source());
            }
        };
        Request request = new Request.Builder()
       .url(config.getServerURL())
                .addHeader("Accept","*/*")
                .addHeader("Connection","Keep-Alive") 
                .post(requestBody)
                .build();

        okHttpClient.newCall(request).enqueue(callback);

    }

I’m using an OkIo Pipe to populate RequestBody with audio data

Thanks in advance!

Answer

Don’t change the state of views from other threads, which includes OKHTTP callbacks. If you need to, do it on a runOnUiThread block. By calling setEnabled, you’re causing various UI functions to be called that can’t run on that thread.

We are here to answer your question about How to avoid FATAL EXCEPTION in OkHttp Dispatcher after callback - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji