Android Thrift客户端 - IllegalStateException

时间:2015-11-11 01:22:10

标签: java android rpc thrift

我正在尝试从同一网络上运行的Android应用程序(测试设备在Android 5.0.2上,如果重要)连接到运行在Ubuntu 14.04上的Thrift服务器。

我为此生成了两组代码:一组用于在我的笔记本电脑上运行服务器,另一组用于在android项目中使用。

因此对于相同的.thrift,我运行thrift编译器(0.9.3)两次以生成两组代码。

thrift --gen java example.thrift
thrift --gen java:android example.thrift

我可以启动我的应用程序,但是当它尝试连接时(使用Android应用程序主要活动中的按钮完成),我在Android Studio IDE中的logcat中获得以下错误流:

11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime: FATAL EXCEPTION: main
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime: Process: yak.thriftexample, PID: 10706
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime: java.lang.IllegalStateException: Could not execute method for android:onClick
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:278)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at android.view.View.performClick(View.java:5217)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:20983)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:6117)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:  Caused by: java.lang.reflect.InvocationTargetException
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:273)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at android.view.View.performClick(View.java:5217) 
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:20983) 
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739) 
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95) 
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145) 
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:6117) 
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:  Caused by: android.os.NetworkOnMainThreadException
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at libcore.io.IoBridge.connect(IoBridge.java:122)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at java.net.Socket.connect(Socket.java:882)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at org.apache.thrift.transport.TSocket.open(TSocket.java:221)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at yak.thriftexample.ThriftExampleActivity.connectToServer(ThriftExampleActivity.java:42)
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:273) 
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at android.view.View.performClick(View.java:5217) 
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:20983) 
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739) 
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95) 
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145) 
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:6117) 
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
11-10 20:05:11.030 10706-10706/yak.thriftexample E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

以下是尝试从Android应用主要活动进行连接的代码:

public void connectToServer(View view) {
    try {
        TTransport transport = new TSocket("192.168.0.109", PORT);
        TProtocol protocol = new TBinaryProtocol(transport);
        ServiceExample.Client client = new ServiceExample.Client(protocol);
        transport.open();
        BeanExample bean = client.getBean(1, "string");
        transport.close();
        System.out.println(bean.getStringObject());
    } catch (TTransportException e) {
        e.printStackTrace();
    } catch (TException e) {
        e.printStackTrace();
    }
}

如果我使用没有':android'选项的生成代码来生成我在笔记本电脑上运行的Java客户端,那么它可以工作,所以问题似乎在Android和Thrift之间。

我发现,通过以下this question的链接发现,在Android上运行的Thrift库似乎存在一些围绕套接字的问题,但我能找到的只是Jira票据似乎表明他们解决了这些问题,我正在使用更高版本,因此我假设将包含此类修复程序。那么,我错过了什么?

0 个答案:

没有答案