1

我正在使用这段代码:

public static MjpegInputStream read(String url) {
    HttpResponse res;
    DefaultHttpClient httpclient = new DefaultHttpClient();   
    httpclient.getCredentialsProvider().setCredentials(
            new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials("admin", "1234"));
    try {
        res = httpclient.execute(new HttpGet(URI.create(url)));
        return new MjpegInputStream(res.getEntity().getContent());              
    } catch (ClientProtocolException e) { Log.e("MjpegInputStream - CP", e.getMessage()); } 
    catch (IllegalArgumentException e) { Log.e("MjpegInputStream - IA", e.getMessage()); } 
    catch (IOException e) { Log.e("MjpegInputStream - IO", e.toString() + " " + e.getMessage()); } 
    return null;
}

我得到IOExcetion:

04-09 17:27:52.350:E/MjpegInputStream - IO(5749):java.net.SocketException:权限被拒绝 权限被拒绝

我的网址是http://192.168.1.113/videostream.cgi当我用浏览器连接时,用户名和密码是(管理员1234

我究竟做错了什么 ?

更新:

我添加了 INTERNET 权限,现在我的应用程序在此行崩溃:

res = httpclient.execute(new HttpGet(URI.create(url)));

NetworkOnMainThreadException

4

2 回答 2

1

好像忘记添加了

<uses-permission android:name="android.permission.INTERNET"/>

到您的清单文件。

另请参阅安全性和权限

于 2012-04-09T14:41:49.800 回答
1

您的NetworkOnMainThreadException错误是由在主 UI 线程上运行网络操作引起的。我有一个类似的问题,还没有弄清楚如何正确修复它,但我知道你需要让 MjpegInputStream 进入它自己的线程。一个临时解决方案是允许网络在 UI 线程上运行,但这是不好的做法,不应该发布:

//DO NOT LEAVE THIS IN PUBLISHED CODE: http://stackoverflow.com/a/9984437/1233435
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll()
.build();
StrictMode.setThreadPolicy(policy);
//END of DO NOT LEAVE THIS IN PUBLISHED CODE
于 2012-05-10T20:40:15.843 回答