回调后如何避免OkHttp Dispatcher中的FATAL EXCEPTION

时间:2017-12-01 17:28:55

标签: android okhttp

我在收到来自exception的回调后执行ui操作时收到此OkHttp

  

致命异常:OkHttp Dispatcher                                                                                           处理:org.example.verificationserviceexample,PID:20421                                                                                           android.util.AndroidRuntimeException:动画师只能在Looper线程上运行                                                                                               在android.animation.ValueAnimator.cancel(ValueAnimator.java:1020)                                                                                               在android.animation.AnimatorSet.cancel(AnimatorSet.java:366)                                                                                               在android.graphics.drawable.RippleComponent.cancelSoftwareAnimations(RippleComponent.java:214)                                                                                               在android.graphics.drawable.RippleComponent.cancel(RippleComponent.java:133)                                                                                               在android.graphics.drawable.RippleComponent.enter(RippleComponent.java:100)                                                                                               在android.graphics.drawable.RippleDrawable.tryBackgroundEnter(RippleDrawable.java:552)                                                                                               在android.graphics.drawable.RippleDrawable.setBackgroundActive(RippleDrawable.java:290)                                                                                               在android.graphics.drawable.RippleDrawable.onStateChange(RippleDrawable.java:270)                                                                                               在android.graphics.drawable.Drawable.setState(Drawable.java:735)                                                                                               在android.view.View.drawableStateChanged(View.java:18046)                                                                                               在android.widget.TextView.drawableStateChanged(TextView.java:4090)                                                                                               在android.support.v7.widget.AppCompatButton.drawableStateChanged(AppCompatButton.java:155)                                                                                               在android.view.View.refreshDrawableState(View.java:18115)                                                                                               在android.view.View.setEnabled(View.java:8135)                                                                                               在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)                                                                                               在com.bssys.spitchmobilesdk.audio.SpitchAudioManager.publishAudioStateChange(SpitchAudioManager.java:123)                                                                                               在com.bssys.spitchmobilesdk.audio.SpitchAudioManager.stopAudio(SpitchAudioManager.java:83)                                                                                               在com.bssys.spitchmobilesdk.VerificationService.stopAudio(VerificationService.java:96)                                                                                               在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)                                                                                               在com.bssys.spitchmobilesdk.VerificationService $ 1.onFailure(VerificationService.java:111)                                                                                               在okhttp3.RealCall $ AsyncCall.execute(RealCall.java:161)                                                                                               在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)                                                                                               在java.lang.Thread.run(Thread.java:761)

这是我正在使用的代码:

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);

    }

我正在使用OkIo Pipe为MessageBody填充音频数据

提前致谢!

2 个答案:

答案 0 :(得分:7)

不要更改其他线程的视图状态,包括OKHTTP回调。如果需要,请在runOnUiThread块上执行此操作。通过调用setEnabled,您将导致调用无法在该线程上运行的各种UI函数。

答案 1 :(得分:3)

请在以下代码中添加您的build.gradle(:app)文件:

 compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }