【问题标题】:Android connect to MJPEG stream - Permission Denied ErrorAndroid 连接到 MJPEG 流 - 权限被拒绝错误
【发布时间】:2012-04-09 14:31:43
【问题描述】:

我正在使用此代码:

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 当我用浏览器连接时,用户名和密码是 (admin, 1234 )

我做错了什么?

更新:

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

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

带有 NetworkOnMainThreadException

【问题讨论】:

标签: java android sockets mjpeg


【解决方案1】:

好像忘记加了

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

到您的清单文件。

另见Security and Permissions

【讨论】:

【解决方案2】:

您的 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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-20
  • 1970-01-01
  • 2015-07-04
  • 2014-06-17
  • 2012-11-02
  • 1970-01-01
  • 2014-05-13
相关资源
最近更新 更多