Stefan Maimon
Stefan Maimon

Reputation: 233

Connecting to a host address with Android

I have a server address that I want to connect my app to.

This is his address: "http://54.148.194.246:8080/".

I try to connect to it by this code:

clientSocket = new Socket("http://54.148.194.246/", 8080);

But my app gives me this error :

java.net.UnknownHostException: Unable to resolve host "http://54.148.194.246/": No address associated with hostname.

I added Internet permission and my wireless is on (those were the answers that I saw for this problem).

Any ideas?

Thanks.

Upvotes: 1

Views: 486

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596352

You need to remove http://from the IP/hostname when passing it to the Socket constructor:

clientSocket = new Socket("54.148.194.246", 8080);

Alternatively, use the URL class for sending HTTP requests specifically:

URL url = new URL("http://54.148.194.246:8080/");
InputStream strm = (InputStream) url.getContent();
// use strm as needed...

Or:

URL url = new URL("http://54.148.194.246:8080/");
URLConnection conn = url.openConnection();
// use conn as needed...

Upvotes: 2

Related Questions