Android connect to MJPEG stream - Permission Denied Error

I'm using this code:

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;
}

I get IOExcetion:

04-09 17:27:52.350: E/MjpegInputStream - IO(5749): java.net.SocketException: Permission denied Permission denied

My URL is http://192.168.1.113/videostream.cgi And when i connect with my browser the username and password is (admin, 1234)

What am i doing wrong ?

UPDATE:

I added INTERNET permissions and now my application crashes on this line:

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

with NetworkOnMainThreadException

Answers


It seems you forgot to add

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

to your manifest file.

See also Security and Permissions


Your NetworkOnMainThreadException error is caused by running a Network operation on the main UI thread. I am having a similar issue and have yet to figure out how to properly fix it, but I know you need to get MjpegInputStream into its own thread. A temporary solution is to allow the network operating to run on the UI thread but this is bad practice and should not be shipped:

//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

Need Your Help

Being innovative (in Web Design/Development) while maintaining backward-compatibility

javascript html css html5 css3

I love building websites, and I love making them compatible with as many different platforms, devices and browsers as possible, but I am really getting sick of it nowadays to be honest.

How can I grab and use path from STDOUT in Python 2.7 (OS X)

python applescript subprocess

This question is related to those using OS X because I am returning a path from the foremost Finder window using an AppleScript. I want to get this path and list the directory contents. Ultimately ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.