URL openConnection by mobild data throws ConnectException when Device sleep - android

I create an HttpURLConnection in a Thread or AsyncTask and it works fine.
But when the Device is in sleep mode, (Display is Off...) then I get an
Exception by open the Connection -> java.net.ConnectException: Connection refused.
My idea is to wake up the Device with the PowerManager.PARTIAL_WAKE_LOCK, but that doesn't help.
The other defines from PowerManager for example ACQUIRE_CAUSES_WAKEUP are deprecated.
What can I do to create a successful connection?
The code:
URL url = new URL("myUrl...");
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.connect();
int response = urlConnection.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
// to do some work
}

I suggest you to use OkHttp. It is better than HttpClient.

Related

Android, not connecting to local php file

I'm trying to have my android app execute code where it opens a URL connection to a local php file that returns database entries in JSON format, but it's not connecting, after commenting out the other lines I can see that it throws an exception at the lines:
connection = (HttpURLConnection) url.openConnection();
connection.connect();
Heres the screen shot of android code and the error log, the returned expected json file in windows and the php file:
link
Can you change your
URL url = new URL(getListUrl);
into
URL url = new URL("http://www.google.com");
and check the incoming data.
and the following is a working example.
try {
URL url = new URL("http://www.google.com");
URLConnection urlConn = url.openConnection();
if (!(urlConn instanceof HttpURLConnection)) {
throw new IOException("URL Exception");
}
HttpURLConnection httpConn = (HttpURLConnection) urlConn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
resCode = httpConn.getResponseCode();
if (resCode == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (MalformedURLException e) {
e.printStackTrace();
}
My argument is if this code works for remote urls, it should work with localhost as well.
Hi it must be that you are using wrong local ip address in android files
it should be 10.0.2.2 not 127.0.0.1
First tell me are you testing on emulator or device
if emulator then where you define file path it should be
http://10.0.2.2/request_entries.php
just like in code
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://10.0.2.2/request_entries.php");
Managed to get the input stream using the URL:
URL url = new URL("http://10.0.2.2/request_entries.php");
result
Thanks a bunch guys :)

HttpURLConnection getResponseCode() deos not return if there is no Internet connection

I am using a HttpURLConnection to check whether the server URL is available or not by using the following code:
try {
boolean connectionFailed = false;
URL knownURL = new URL("http://www.google.com");
httpConnection = (HttpURLConnection) knownURL.openConnection();
httpConnection.setConnectTimeout(5000);
responseCode = httpConnection.getResponseCode();
if (responseCode != 200) {
status = ConnectionStatus.NOT_CONNECTED;
}
}
catch(Exception e) {
connctionFailed = true;
}
This code is working fine under normal conditions. But when there is no Internet connection (because either the router is disconnected or not the hotspot), httpConnection.getResponseCode() is not executed (the function does not return). How can I fix this?
httpConnection.setConnectTimeout(5000)
is a timeout for connection.
This is not a timeout for httpConnection.getResponseCode().
If you add httpConnection.setReadTimeout(2000), httpConnection.getResponseCode()should throw an exception when no connection is available.
You may be having a try catch block at higher layer which is catching the sockettimeout exception.

How to check internet accessibility in hotspot (Android)

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?

Android HttpURLConnection not working with cgi-bin?

I have simple code :
URL url;
BufferedReader in = null;
HttpURLConnection connection;
InputStream is = null;
InputStreamReader br = null;
setProgressTitle(progress, context.getString(R.string.loading));
setProgressMessage(progress, context.getString(R.string.loading_from_internet));
try {
url = new URL(urlStr);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(Const.TIMEOUT);
is = connection.getInputStream();
...
If I have urlStr = "http://samlib.ru/w/waliduda_a_a/molochnischituran1.shtml" - all is work fine.
If I use urls like urlStr = "http://samlib.ru/cgi-bin/areader?q=jlist" - I got a error in connection.getInputStream();
**
03-04 15:37:52.459: ERROR/DataReader::loadDataFromInet(17281): Failed loading http://samlib.ru/cgi-bin/areader?q=jlist
03-04 15:37:52.459: ERROR/DataReader::loadDataFromInet(17281): java.io.FileNotFoundException: http://samlib.ru/cgi-bin/areader?q=jlist
03-04 15:37:52.459: ERROR/DataReader::loadDataFromInet(17281): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521)
**
How can I upload data to a similar url?
There are a couple of reasons this might happen. Where I've personally seen it is when I've put in a URL that was subsequently redirected, HttpURLConnection doesn't handle that. I got the same response as you, I hit it with FF and it works fine. Its also possible that some sort of browser sniffing might be done on the recieving side.
Good Luck!
It looks like your cgi-bin/areader is not found. Getting an HTTP/404 response code:
wget http://samlib.ru/cgi-bin/areader?q=jlist
--2011-03-04 09:03:17-- http://samlib.ru/cgi-bin/areader?q=jlist
Resolving samlib.ru... 81.176.66.171
Connecting to samlib.ru|81.176.66.171|:80... connected.
HTTP request sent, awaiting response... 404 Not Found
2011-03-04 09:03:17 ERROR 404: Not Found.
Correct that then try again.

can't use Http Post method in android application

HI,
I need to use http post method for web-service in android application .But in android shows error when use this method .How to use this method in application or any other equal method for replacing this http post method.
thanks,
Lakshmanan.
URL url = new URL(my_ip_address);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("METHOD", "POST");
HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
//do your stuff
}

Categories

Resources