1

我刚开始学习android编程,我对java也没有那么有经验。我正在尝试在android中制作一个应用程序,从树莓派流式传输mjpeg(使用mjpeg-streamer)。地址模式是:

http://ip_address:8080/?action=stream

树莓派部分就完成了。问题出在Android部分..我尝试了这个代码: Android and MJPEG

然后我得到一个黑屏并且它崩溃了..从那个页面上的评论中,我发现我需要获得http证书或类似的..

我尝试了几种解决方案来信任所有证书,但我遇到了一些语法错误。例如,从这个页面:https ://stackoverflow.com/a/12742591

我在它说的那一行(appx line 83)上遇到了一个错误:

super(null, null, null, null, null, (X509HostnameVerifier)null);

错误是:

The constructor SSLSocketFactory(null, null, null, null, null, X509HostnameVerifier) is undefined

而且我不知道如何解决它,因为我是这个主题的新手。Eclipse 建议将 (X509HostnameVerifier) 更改为 (HostNameResolver)。但后来我对 mjpeg 部分的编辑位置感到困惑。

我想我应该改变(appx line 29):

DefaultHttpClient httpclient = new DefaultHttpClient();  

DefaultHttpClient httpclient = HttpsClientBuilder.getBelieverHttpsClient(); 

它仍然崩溃。除了提到的两行之外,我没有更改上面页面中的任何代码。有什么建议吗?

我只想信任所有证书,因为当树莓派重新启动时,有时它会更改 IP 地址。如果IP地址发生变化,我必须获得新的证书吗?

编辑

我忘了添加互联网使用权限..这是新的logcat:

01-21 18:02:48.825: W/dalvikvm(27150): threadid=1: thread exiting with uncaught exception (group=0x40c4d1f8)
01-21 18:02:48.830: E/AndroidRuntime(27150): FATAL EXCEPTION: main
01-21 18:02:48.830: E/AndroidRuntime(27150): java.lang.RuntimeException: Unable to start activity : android.os.NetworkOnMainThreadException
01-21 18:02:48.830: E/AndroidRuntime(27150):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1973)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1999)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at android.os.Looper.loop(Looper.java:137)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at android.app.ActivityThread.main(ActivityThread.java:4513)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at java.lang.reflect.Method.invokeNative(Native Method)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at java.lang.reflect.Method.invoke(Method.java:511)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:974)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:741)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at dalvik.system.NativeStart.main(Native Method)
01-21 18:02:48.830: E/AndroidRuntime(27150): Caused by: android.os.NetworkOnMainThreadException
01-21 18:02:48.830: E/AndroidRuntime(27150):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at libcore.io.IoBridge.connectErrno(IoBridge.java:138)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at libcore.io.IoBridge.connect(IoBridge.java:112)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at java.net.Socket.connect(Socket.java:842)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at com.example.testviewer.MjpegInputStream.read(MjpegInputStream.java:31)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at com.example.testviewer.MjpegSample.onCreate(MjpegSample.java:40)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at android.app.Activity.performCreate(Activity.java:4465)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
01-21 18:02:48.830: E/AndroidRuntime(27150):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
01-21 18:02:48.830: E/AndroidRuntime(27150):    ... 11 more
01-21 18:02:57.795: I/Process(27150): Sending signal. PID: 27150 SIG: 9
4

1 回答 1

0

我对 http 证书一无所知,但您的日志显示您的应用程序由于以下原因而崩溃Caused by: android.os.NetworkOnMainThreadException. 这意味着您在保留用于处理 UI 交互的应用程序主线程上运行网络操作。

将您的网络调用移动到另一个线程或使用AsyncTask类在后台运行网络调用以克服此异常。

于 2013-01-22T11:04:09.513 回答