Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android, httpurlconnection error

I'm running the following test Java script from the Android HttpURLConnection docs:

URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection)
url.openConnection();
urlConnection.connect();

Eclipse (on my Mac) is telling me there's a system error when I run this in the Android Emulator:

01-13 13:44:32.767: WARN/System.err(1382): java.net.SocketException: Permission denied

(Incidentally when I do the equivalent in Objective-C/Cocoa there's no problem at all.)

What is the problem?

like image 694
SK9 Avatar asked Jan 13 '11 04:01

SK9


People also ask

How do I get HttpURLConnection response?

The getResponseMessage is a method of Java HttpURLConnection class. This method is used to get response code from HTTP response message. For example, if the response code from a server is HTTP/1.0 200 OK or HTTP/1.0 404 Not Found, it extracts the string OK and Not Found else returns null if HTTP is not valid.

Is HttpURLConnection thread safe?

Each instance of HttpURLConnection may be used for one request/response pair. Instances of this class are not thread safe.

Can I use HttpURLConnection for https?

HttpsURLConnection extends HttpURLConnection , and your connection is an instance of both. When you call openConnection() the function actually returns an HttpsURLConnection . However, because the https object extends the http one, your connection is still an instance of an HttpURLConnection .

How do I turn off HttpURLConnection in Java?

To close the connection, invoke the close() method on either the InputStream or OutputStream object. Doing that may free the network resources associated with the URLConnection instance.


1 Answers

Your application is not allowed to access the network unless it has declared that its going to in the AndroidManifest.xml file, add this inside the <manifest> element.

<uses-permission android:name="android.permission.INTERNET" />
like image 122
superfell Avatar answered Oct 26 '22 20:10

superfell