【问题标题】:Android App OKHTTP Websocket onFailure() connection close when Switch.setChecked(true) called调用 Switch.setChecked(true) 时 Android App OKHTTP Websocket onFailure() 连接关闭
【发布时间】:2020-10-03 03:49:24
【问题描述】:

我正在 android studio 模拟器中开发一个 websocket 应用程序,显然一切正常,直到程序执行“Switch.setChecked(true)”。 当程序执行“Switch.setChecked(true)”时,会调用“public void onFailure(WebSocket webSocket, Throwable t, Response response)”方法并关闭连接。

我以为是监听器的递归问题,但是如果监听器内部的“onCheckedChanged()”函数没有任何代码,问题仍然存在。

注意: 我正在使用 https/wss。 我在 esp32 中编写的安全 websocket 服务器。

感谢您的帮助。

private Switch lampSwitch;

protected void onCreate(Bundle savedInstanceState)
{  
    ...

    lampSwitch = (Switch) findViewById(R.id.lampSwitch);
    lampSwitch.setOnCheckedChangeListener(new lampSwitchListener());

    ...
}


private static final String TAG = "FragmentActivity";
private String sendValue;

private class lampSwitchListener implements CompoundButton.OnCheckedChangeListener
{
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        Log.d(TAG, "onCheckedChanged: test");

        if (isChecked)
        {
            sendValue = "{\"lamp\":\"ON\"}";
        }
        else
        {
            sendValue = "{\"lamp\":\"OFF\"}";
        }
        send_Ws_Message(2, sendValue);
    }
}


private void process_Message( ByteString input )    
{
    ByteString b = input.substring(1);    // offset 1 position.
    String message = b.utf8();    // convert from ByteString to String.
    

    Log.d("MSG: ", message);
    Log.d("length: ", String.valueOf(message.length() ) );
    
    
    JSONObject jObject = null;
    try
    {
        jObject = new JSONObject(message);
    } catch (JSONException e) {
        e.printStackTrace();
    }


    String lamp = null;
    try
    {
        lamp = jObject.getString("lamp");
    } catch (JSONException e) {
        e.printStackTrace();
    }


    if ( lamp.equals("ON") )
    {
        Log.d("lamp: ", lamp);
        Log.d("lamp.length: ", String.valueOf(lamp.length() ));


        lampSwitch.setChecked(true);    // this code line close client websocket connection
    }
}

Logcat:

2020-10-03 00:37:44.898 6218-6218/? I/mple.myfirstap: Not late-enabling -Xcheck:jni (already on)
2020-10-03 00:37:44.914 6218-6218/? I/mple.myfirstap: Unquickening 12 vdex files!
2020-10-03 00:37:44.915 6218-6218/? W/mple.myfirstap: Unexpected CPU variant for X86 using defaults: x86
2020-10-03 00:37:45.070 6218-6218/com.example.myfirstapp D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
2020-10-03 00:37:45.072 6218-6218/com.example.myfirstapp D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
2020-10-03 00:37:45.082 6218-6243/com.example.myfirstapp D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
2020-10-03 00:37:45.088 6218-6243/com.example.myfirstapp D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
2020-10-03 00:37:45.092 6218-6243/com.example.myfirstapp D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
2020-10-03 00:37:45.201 6218-6218/com.example.myfirstapp W/mple.myfirstap: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2020-10-03 00:37:45.201 6218-6218/com.example.myfirstapp W/mple.myfirstap: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2020-10-03 00:37:45.295 6218-6241/com.example.myfirstapp D/HostConnection: HostConnection::get() New Host Connection established 0xee254710, tid 6241
2020-10-03 00:37:45.299 6218-6241/com.example.myfirstapp D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_vulkan_free_memory_sync GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_3_0 
2020-10-03 00:37:45.301 6218-6241/com.example.myfirstapp W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2020-10-03 00:37:45.306 6218-6241/com.example.myfirstapp D/EGL_emulation: eglCreateContext: 0xee064eb0: maj 3 min 0 rcv 3
2020-10-03 00:37:45.323 6218-6241/com.example.myfirstapp D/EGL_emulation: eglMakeCurrent: 0xee064eb0: ver 3 0 (tinfo 0xee3b5630) (first time)
2020-10-03 00:37:45.343 6218-6241/com.example.myfirstapp I/Gralloc4: mapper 4.x is not supported
2020-10-03 00:37:45.344 6218-6241/com.example.myfirstapp D/HostConnection: createUnique: call
2020-10-03 00:37:45.344 6218-6241/com.example.myfirstapp D/HostConnection: HostConnection::get() New Host Connection established 0xee253ed0, tid 6241
2020-10-03 00:37:45.369 6218-6241/com.example.myfirstapp D/goldfish-address-space: allocate: Ask for block of size 0x100
2020-10-03 00:37:45.369 6218-6241/com.example.myfirstapp D/goldfish-address-space: allocate: ioctl allocate returned offset 0x3fc7ba000 size 0x2000
2020-10-03 00:37:45.375 6218-6241/com.example.myfirstapp D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_vulkan_free_memory_sync GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_3_0 
2020-10-03 00:37:47.795 6218-6218/com.example.myfirstapp W/mple.myfirstap: Accessing hidden method Landroid/graphics/FontFamily;-><init>()V (greylist-max-q, reflection, denied)
2020-10-03 00:37:47.797 6218-6218/com.example.myfirstapp E/TypefaceCompatApi26Impl: Unable to collect necessary methods for class java.lang.NoSuchMethodException
    java.lang.NoSuchMethodException: android.graphics.FontFamily.<init> []
        at java.lang.Class.getConstructor0(Class.java:2332)
        at java.lang.Class.getConstructor(Class.java:1728)
        at androidx.core.graphics.TypefaceCompatApi26Impl.obtainFontFamilyCtor(TypefaceCompatApi26Impl.java:321)
        at androidx.core.graphics.TypefaceCompatApi26Impl.<init>(TypefaceCompatApi26Impl.java:84)
        at androidx.core.graphics.TypefaceCompatApi28Impl.<init>(TypefaceCompatApi28Impl.java:36)
        at androidx.core.graphics.TypefaceCompat.<clinit>(TypefaceCompat.java:47)
        at androidx.core.graphics.TypefaceCompat.create(TypefaceCompat.java:190)
        at androidx.appcompat.widget.AppCompatTextView.setTypeface(AppCompatTextView.java:705)
        at android.widget.TextView.resolveStyleAndSetTypeface(TextView.java:2183)
        at android.widget.TextView.setTypefaceFromAttrs(TextView.java:2154)
        at android.widget.TextView.applyTextAppearance(TextView.java:4105)
        at android.widget.TextView.<init>(TextView.java:1630)
        at android.widget.TextView.<init>(TextView.java:990)
        at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:99)
        at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:95)
        at androidx.appcompat.app.AppCompatViewInflater.createTextView(AppCompatViewInflater.java:182)
        at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
        at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1407)
        at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1457)
        at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1059)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:995)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
        at com.example.myfirstapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:163)
        at android.app.Activity.performCreate(Activity.java:7995)
        at android.app.Activity.performCreate(Activity.java:7979)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2020-10-03 00:37:49.599 6218-6251/com.example.myfirstapp D/FragmentActivity: onFailure: caca

【问题讨论】:

  • 它被表述为一个引用 okhttp 的 websocket 问题,但带有一个看起来非常致命的字体相关错误的 logcat。
  • 通过我所做的测试,问题与 OkHttp 库无关。看我的回答。

标签: android android-layout https websocket


【解决方案1】:

通过我所做的测试,问题与 OkHttp 库无关。

我的应用程序上也有一个 seekbBar,我只用 seekBar 进行了测试并且工作正常。

slider = (SeekBar) findViewById(R.id.slider);
slider.setOnSeekBarChangeListener(new sliderListener());

我发现Switch这种方式可以正常工作,即:改变屏幕上的Switch状态。

@Override
protected void onCreate(Bundle savedInstanceState)
{
    ...

    lampSwitch = (Switch) findViewById(R.id.lampSwitch);
    lampSwitch.setChecked(true);
    lampSwitch.setOnCheckedChangeListener(new lampSwitchListener());

    ...

我发现Switch以这种方式使应用崩溃,即:应用关闭。

@Override
protected void onCreate(Bundle savedInstanceState)
{
    ...

    lampSwitch = (Switch) findViewById(R.id.lampSwitch);
    lampSwitch.setOnCheckedChangeListener(new lampSwitchListener());
    lampSwitch.setChecked(true);    

    ...

我已经尝试过下面的代码,但也没有用。 这里 websocket 连接关闭并且屏幕上的 Switch 状态没有改变。 我在应用程序中使用另一个 Switch 进行了测试,并且行为相同。

if ( lamp.equals("ON") )
{
    Log.d("lamp: ", lamp);
    Log.d("lamp.length: ", String.valueOf(lamp.length() ) );

    lampSwitch.setOnCheckedChangeListener( null );
    lampSwitch.setChecked(true);    
    lampSwitch.setOnCheckedChangeListener(new lampSwitchListener());
}

非常欢迎任何建议。

【讨论】:

    【解决方案2】:

    问题已解决:

    runOnUiThread(new Runnable()
    {
        @Override
        public void run()
        {
            lampSwitch.setOnCheckedChangeListener( null );
            lampSwitch.setChecked(true);
            lampSwitch.setOnCheckedChangeListener(new lampSwitchListener());
        }
    });
    

    奇怪的是,只有 Switch 小部件需要它,SeekBar 小部件不需要,但我在 SeekBar 中也使用了“runOnUiThread”。

    【讨论】:

      猜你喜欢
      • 2017-07-03
      • 1970-01-01
      • 2021-10-30
      • 2014-09-03
      • 2016-12-08
      • 2019-05-01
      • 2017-06-27
      • 2015-10-24
      • 2019-10-22
      相关资源
      最近更新 更多