I have an application which connects to a web service. I can connect to the web service any number of times without any problem using WIFI or 3G provided that I stay loyal to my connection type during the life cycle of my application. That is if I don't switch from WIFI to 3G. If I switches from WIFI to 3G, I can't get a response anymore. My connection just keeps on waiting for response.
I tried 4 scenarios below. I'm only having problem with the 3rd scenario. What could be the problem?
1st Scenario: Connection is always on WIFI (Ok)
Application connects to a web service using WIFI.
Response was received successfully.
Application connects again to a web service using WIFI.
Response was receive successfully.
2nd Scenario: Connection is always on 3G (Ok)
Application connects to a web service using WIFI.
Response was received successfully.
Application connects again to a web service using WIFI.
Response was receive successfully.
3rd Scenario: Connection switches from WIFI to 3G (No response)
Application connects to a web service using WIFI.
Response was received successfully.
Connection was switched to 3G. WIFI is disabled. 3G is enabled.
Application connects again to a web service using 3G.
No was response or error was received. Application keeps on waiting for response. Last log was displayed before getResponseCode was called.
4th Scenario: Connection switches from 3G to WIFI (Ok)
Application connects to a web service using 3G.
Response was received successfully.
Connection was switched to WIFI. 3G is disabled. WIFI is enabled.
Application connects again to a web service using WIFI.
Response was receive successfully.
My guess is by default, HttpURLConnection thinks WIFI as a better connection type compare to 3G. So when the connection is switched from WIFI to 3G, HttpURLConnection refuses to acknowledge 3G and still tries to connect using WIFI. On the other hand, HttpURLConnection allows switching from 3G to WIFI since WIFI is a better connection type. Am I correct with this? If so, how do I allow switching from WIFI to 3G?
Below is a snippet of my code: (I call it every time I connect to a web service.)
//open new connection
httpsURLConnection = (HttpURLConnection) ((new URL(url)).openConnection());
httpsURLConnection.setDoInput(isDoInput);
httpsURLConnection.setDoOutput(isDoOutput);
try
{
//supply parameters
OutputStreamWriter wr = new OutputStreamWriter(httpsURLConnection.getOutputStream());
wr.write(data);
wr.flush();
if(httpURLConnection != null)
{
if (httpsURLConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) //connection hangs here
{
//some code
}
else
{
//some code
}
}
}catch(Exception e)
{
}
I'm not sure why but adding System.setProperty("http.keepAlive", "false") on the code solves the problem.
According to the Android Developers Blog (androids-http-clients), HttpURLConnection has a bug prior to Froyo and it can be solved by disabling connection pooling just like above. However, I'm using Gingerbread so I'm not sure why HttpURLConnection is still misbehaving on my application.
To others: If you can provide more explanation, please don't hesitate to edit my post.
when you make a connection on wifi and it switches back to 3G, do you see any connection drop exceptions?
From what i know, if you open a connection on wifi the same connection can't be used for 3G. you need to teardown existing connection and reestablish new connection on 3G.
I am guessing your problem could be because you haven't set any connection timeouts for your requests and as a result when connection drops it takes a long time for HttpUrlConnection to realize ur connection is lost.
I have put together utility libraries on http connections with some default timeouts, see if this can help you anyway
https://github.com/nareshsvs/android-libraries
Related
I have a Play 2 (2.3.7) app that uses EventSource connections. Everything works fine except when I try to close the connection from the client. To do that I am using the ES.close method, but even after I disconnect Play only detects that the connection was closed (and processes the onDisconnect callback) when I send a ping to the client. Closing the connection from server side works as expected.
I tried this in Chrome/Firefox (using native implementation) and using Yaffle ES polyfill and the result is the same. Both client and server are running in a linux machine.
EDIT: After closing the connection in the browser console and inspection the network tab in dev tools it seems the connection is indeed closed by the browser. So this may be a problem of Play?
So what may be the problem here?
I am trying to connect to my android device with my mysql database, I can do it in local (from a emulation device) but when I use my IP to connect to with my mobile I get:
Communication Link failure
The last packet send succesfully to the server was 0 millisecond ago. The driver has not recived any packet from the server
my code is:
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://MYIP:3306/BD", "USER", "USER");
with localhost it works:
Connection conn=DriverManager.getConnection("jdbc:mysql://10.0.2.2:3306/BD", "USER", "USER");
Thanks for your help.
To connect your Android device to Mysql you should use webs ervices. you can find more information in this tutorial
the Tutorial use PHP to creat web services, however you can use any other server side language to connect with mysql database, this will return JSON data that you can parse it to in your android app
It seems this is a network issue, if the devices are in the same LAN don't use your internet IP. Or even if the devices are in different network your router could be blocking your connection.
I am making application in j2me, i am sending data to server
but before that i want to first check that if internet is working or not (it may be from WIFI or GPRS)
Try to use getResponseCode() and getResponseMessage() methods of HttpConnection API .
There's no reliable way to determine if 'the internet is working' other than sending a request to your server. Just because you have a connection to an ISP, it doesn't mean that the ISP has a connection to the internet, or that a packet sent via that connection can reach your server.
The phone might tell you it's not connected, but if you want to know whether you can reach your server when it is connected, send a request and see what happens.
I use Bluecove 2.1.1 and trying to restart bluetooth service, but after closing connection and reopening I can't connect to service, I got "Connection refused" exception. I found this issue on Bluecove site? I also try to change service name(also UUID) after closing connection, but it's not help. Does anyone know workaround for it? I'm simply need to restart bluetooth service.
I am not sure if I understood your question in the way it meant to be.
I assume that you are opening a Connector and let a SessionNotifier handle OBEX connections with accecptAndOpen(ServerRequestHandler).
When a clients connects to your bluetooth service the specific methods in your ServerRequestHandler are called (onGet, onPut, ..). When the client disconnects onDisconnect is triggered. Now you need to acceptAndOpen your service again to handle future connections.
I'm trying to run a small, custom HTTP server embedded in my Android application (to aid in debugging the app). However, I cannot reliably to connect to the HTTP server from my desktop web browser when the server is running on my Android 2.3.4 phone.
I know the HTTP server code works (at least the basics), because one time the connection went through fine. And the code works reliably when run on my desktop (my app is a libgdx-based, pure Java app that can be built for Windows or for Android).
I have the Android permissions correct, because I was getting the PermissionDenied fault (when setting up the socket listener) thrown before I added the android:name="android.permission.INTERNET" permission. The server-side code runs in its own thread and boils down to:
ServerSocket ss = new ServerSocket(serverPort);
Socket clientSocket = ss.accept();
I'm using port 8080. (I tried port 80 but hit the must-be-root-below-port-1024 "feature" of Linux).
The LogCat output looks fine. My app just prints its "listening on port 8080" message and no exceptions seem to be thrown.
Network connectivity seems fine (browsing the internet from the phone is working, and I can ping the phone from desktop). I'm using WiFi, not 3G.
I think I found it. I was missing a call to:
ss.setReuseAddress(true);
after creating the server socket. This means each restart of the app (pause/resume, too) was (silently?) failing to create server socket because the socket was marked as 'in use' for a bit. This explains why it was so unreliable (only the very first run, or running after a long enough wait would work ...)
I found it by staring at the code linked to from this blog entry.