我正在使用此代码:
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:Permission denied Permission denied
我的网址是 http://192.168.1.113/videostream.cgi 当我用浏览器连接时,用户名和密码为( admin , 1234 )
我做错了什么?
更新:
我添加了INTERNET权限,现在我的应用程序在此行崩溃了:
res = httpclient.execute(new HttpGet(URI.create(url)));
NetworkOnMainThreadException
答案 0 :(得分:1)
好像你忘了添加
<uses-permission android:name="android.permission.INTERNET"/>
到你的清单文件。
答案 1 :(得分: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