How can I use a 3G internet connection in my Android App?
I have made an App that can connect to a program that I am running on a server, but it only seems开发者_StackOverflow to work with WiFi. When I try to use 3G it doesn't make the connection. Is there something special that I have to turn on or set in order to use 3G?
At the application level you (normally) don't have to worry about what data connection is available to the phone.
Have you looked into problems such as when you use WiFi you web server is accessible since you are hitting it from within your network. Whereas, it may not be accessible at all from the public internet which is how the 3G connection will be trying to connect to the server.
What error / exception do you get on 3G?
The problem was the port I was using. I was using 4444, but it turns out that many networks block that port. Port 1024 seems to work for most.
For what it's worth I was having the same exact problem. My app would work perfect over WiFi but not over 3G. I am on Verizon. I was also using port 4444 because I thought it was a "random" port I had chosen. Thanks to Verizon Wireless blocking this port I spent over an hour trying to diagnose my error. I switched to port 444 and it works fine now. Very frustrating. Hopefully this will help someone else.
There shouldn't be anything additional between Wifi, and 3G. All I've included in my Manifest Permissions is android.permission.INTERNET, and that works fine with either Wifi or 3G, so assuming INTERNET is required for Wifi as well (which I'm not entirely sure about, since I haven't tried JUST Wifi) and you've already included it, I don't know of anything else.
If by some chance Wifi does NOT require INTERNET, and it happens to work without it, the Manifest should include a line like:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
which should grant internet (and 3G) access for your phone. Sorry if that doesn't answer the question, but I can't think of any other reason 3G would be an issue when Wifi works fine. Additionally, make sure you can access the network from another outside location--as said before me, it may be a matter of security preventing outside access somehow. (Not sure what you're using 3G to connect to)
The exception that is getting thrown when the socket is made is an IOException, and the timeout (4000) gets reached. When the phone is in Wi-Fi it works fine but when it uses 3G the socket cannot connect to the host/port.
You'll have to check with your phone company (and good luck in finding someone who can answer!!) that the 3G network doesn't block ports that you're expecting to be open (and are when in WiFi mode). Some telcos block almost everything you'd expect (and your app stack assumes to be available) to be standard on 3G nets (ping, telnet, RDP etc., etc.).
精彩评论