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.
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...
Related
how to connect ejabbered server to android
ConnectionConfiguration config = new ConnectionConfiguration("http://localhost:5280/admin");
XMPPConnection connection = new XMPPConnection(config);
connection.connect();
connection.login("Test", "Test");// Log into the server
You may use Smack Android Clientfor that.
By using Smack creating a connection is as simple like
// Create the configuration for this new connection
XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
configBuilder.setUsernameAndPassword("username", "password");
configBuilder.setResource("SomeResource");
configBuilder.setXmppDomain("jabber.org");
AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build());
// Connect to the server
connection.connect();
// Log into the server
connection.login();
...
// Disconnect from the server
connection.disconnect();
Smack Documention
Smak 4.1.3 ServerName is the name of your server. ServerIp is the ip address of your server and mPort is port for xmpp server which is usually 5222.
And Make sure you have registered users for example in your case user "Test" should be registered.
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
.setServiceName(serverName).setHost(serverIp)
.setPort(mport)
.setCompressionEnabled(false).build();
XMPPTCPConnectionconn conn= new XMPPTCPConnection(config);
conn.connect();
conn.login(username,password);
I am making an android test app that just sends a String to server which is an instance on AWS EC2. However I am seeing 404 even though the IP is correct. What else can be wrong here?
URL serverUrl = new URL("http://aa.bb.cc.dd");
connection = (HttpURLConnection) serverUrl.openConnection();
connection.setRequestMethod("GET");
OutputStream stream = connection.getOutputStream();
String testString = "test123";
byte[] bytes = testString.getBytes();
stream.write(bytes);
stream.flush();
int retcode = connection.getResponseCode();
retcode is 404.
On the server side I have all sources allowed for the following in the security group being used:
All TCP, port 0 - 65535
HTTP, port 80
In my android Application in the first screen user authenticates and servlet replies with a session id ...
in the second screen the user again calls the servlet with the session id added in the url as ;jsession="sessionid" along with the parameters...
but the servlet is not get the session id and is responding as a new session without authentication...
where is the problem?
i am using this code for the connection
URL u = new URL(aurl[0]);
url=aurl[0];
publishProgress(""+20,"Connecting...");
HttpURLConnection c = (HttpURLConnection) u.openConnection();
publishProgress(""+40,"Connecting...");
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
publishProgress(""+45,"Connecting...");
publishProgress(""+40,aurl[0]);
int lenghtOfFile = c.getContentLength();
System.out.println(lenghtOfFile+"lenghtOfFile");
is = c.getInputStream();
I was facing the same problem and found a simple solution.
// First set the default cookie manager.
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
// All the following subsequent URLConnections will use the same cookie manager.
URLConnection connection = new URL(url).openConnection();
// ...
connection = new URL(url).openConnection();
// ...
connection = new URL(url).openConnection();
// ...
This is the simplest solution I found. We need to set default CookieManager.
Using java.net.URLConnection to fire and handle HTTP requests is the great blog.
Thanks
adding jseesioid in the url never worked ..
the code worked using this solution
httpsessionmanagement
especially these two...
// Gather all cookies on the first request.
URLConnection connection = new URL(url).openConnection();
List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
// ...
// Then use the same cookies on all subsequent requests.
connection = new URL(url).openConnection();
for (String cookie : cookies) {
connection.addRequestProperty("Cookie", cookie.split(";", 2)[0]);
}
// ...
I have a code to perform POST Requests with HttpsUrlConnection, the code works fine, but some of my Users have SIM Cards with a closed Usergroup and they need to set a proxy in the settings of their apn. If they set the proxy, i need to modify my code. I Tryed this:
HttpsURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String urlServer = "https://xxx";
String boundary = "*****";
try {
URL url = new URL(urlServer);
SocketAddress sa = new InetSocketAddress("[MY PROXY HOST]",[My PROXY PORT]);
Proxy mProxy = new Proxy(Proxy.Type.HTTP, sa);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;boundary=" + boundary);
//this is supposed to open the connection via proxy
//if i use url.openConnection() instead, the code works
connection = (HttpsURLConnection) url.openConnection(mProxy);
//the following line will fail
outputStream = new DataOutputStream(connection.getOutputStream());
// [...]
} catch (Exception ex) {
ret = ex.getMessage();
}
now i receive the error:
javax.net.ssl.SSLException: Connection closed by peer
If i use url.OpenConnection() wuithout Proxy and without Proxysettings in the apn, the code works, what might be the Problem?
You could try this alternative way of registering a proxy server:
Properties systemSettings=System.getProperties();
systemSettings.put("http.proxyHost", "your.proxy.host.here");
systemSettings.put("http.proxyPort", "8080"); // use actual proxy port
You can use the NetCipher library to get easy proxy setting and a modern TLS config when using Android's HttpsURLConnection. Call NetCipher.setProxy() to set the app-global proxy. NetCipher also configures the HttpsURLConnection instance to use the best supported TLS version, removes SSLv3 support, and configures the best suite of ciphers for that TLS version. First, add it to your build.gradle:
compile 'info.guardianproject.netcipher:netcipher:1.2'
Or you can download the netcipher-1.2.jar and include it directly in your app. Then instead of calling:
HttpURLConnection connection = (HttpURLConnection) sourceUrl.openConnection(mProxy);
Call this:
NetCipher.setProxy(mProxy);
HttpURLConnection connection = NetCipher.getHttpURLConnection(sourceUrl);
I know how to check internet connectivity (NetworkInfo::isAvailable, isConnected) but I've got problems with hotspots.
When you're connected to a hotspot but you didn't enter the password, you are connected to the internet (so the regular check will return true), but you can't connect to sites.
I there a way to confirm that I didn't pass the hotspot's password check? maybe ping to some server?
Would checking a HTTP response code work for you?
String urlFeed = "your_url.com";
URL url = new URL(urlFeed);
URLConnection urlConnection;
urlConnection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) urlConnection;
int responseCode = httpConnection.getResponseCode();
And then check the response code for a negative response?