android-networking

Android, How to handle change in network (from GPRS to Wi-fi and vice-versa) while polling for data

淺唱寂寞╮ 提交于 2019-11-27 12:31:39
I use DefaultHttpClient and HttpGet to poll data from server. Now, say a user was in Wi-fi network and moves out of the room. So the phone automatically starts using the 3G network. Are there any call-backs for such change, and how should I handle such changes. Should I start polling again or does the OS take care to make the changes appropriately You can set up a Receiver in your manifest: <receiver android:name=".NetworkChangeReceiver" android:label="NetworkChangeReceiver"> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> <action android:name="android.net.wifi

Retrofit get String response

拟墨画扇 提交于 2019-11-27 06:38:58
问题 Is it possible to recieve only String response using Retrofit library? I have a situation where I need to add Query on my link so that link is looking like : localhost//Register?handle=SomeID SomeID is integer and when I do that I will receive response from server in string format that consist of 20 chars. How can I get that response? Can Retrofit even handle response that is not in Json format? Also how should I create this : @GET("/api/UserMainInformations") Call getUserMainInfo(); That's

Check network connection in fragment

随声附和 提交于 2019-11-27 06:31:49
问题 I tried to check the network connection in my SherlockFragment but the getSystemService() method is not recognized. Below is my code (from http://developer.android.com/training/basics/network-ops/connecting.html) ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { // fetch data } else { // display error } Thanks in advance 回答1: The

Why is HttpUrlConnection throwing an SSLException while on a mobile data connection?

ε祈祈猫儿з 提交于 2019-11-27 02:37:28
问题 When using Android's HttpUrlConnection library to make an HTTPS request, I sometimes see the following exception being thrown: javax.net.ssl.SSLException: SSL handshake aborted: ssl=0x5c1b18a0: I/O error during system call, Connection reset by peer at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method) at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:395) ... After digging into the issue a bit, I've learned that

Get response status code using Retrofit 2.0 and RxJava

≡放荡痞女 提交于 2019-11-27 02:29:55
I'm trying to upgrade to Retrofit 2.0 and add RxJava in my android project. I'm making an api call and want to retrieve the error code in case of an error response from the server. Observable<MyResponseObject> apiCall(@Body body); And in the RxJava call: myRetrofitObject.apiCall(body).subscribe(new Subscriber<MyResponseObject>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(MyResponseObject myResponseObject) { //On response from server } }); In Retrofit 1.9, the RetrofitError still existed and we could get the status by

How to stay connected through mobile network after WIFI is connected on Android?

孤者浪人 提交于 2019-11-27 02:00:36
问题 I noticed that while streaming audio from a remote server through 3G (mobile) connection and while the WIFI is disconnected or OFF, as soon as WIFI is activated and connected, connection through 3G is dropped. I want the app keep using 3G even if WIFI is connected too now. I want to do this to keep continuity. (User may opt-in/out to/from this behaviour). Is there a special flag, lock, etc.. For this purpose? 回答1: This isn't possible on devices before Android 5.0 (Lollipop). The OS only keeps

Change mobile network mode (gsm, wcdma, auto)

≡放荡痞女 提交于 2019-11-27 01:54:48
问题 I want to change the preferred network mode ie. gsm or wcdma or auto, programmatically, from code, on Android. Is this possible, and if so how ? 回答1: Answer is NO We can open directly the settings app of mobile network settings to switch between "2G" and "allow 3G" networks.A direct switch is sadly not possible. We can develop something which will show current network and allow user short-cut from the app where they can switch network. 回答2: It is possible, I did it. For this to work, your app

How do I get IP_ADDRESS in IPV4 format

[亡魂溺海] 提交于 2019-11-26 22:40:25
问题 I am trying to get the IP address of an device i.e using WIFI or 3G connection. I am getting the ip address in IPV6 format which is not understandable. I want in IPV4 format IP address.I have done google but dint found any proper solutions. here is code which I am using to get IP address of an device public String getLocalIpAddress() { try { try { for (Enumeration<NetworkInterface> en = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement()

Get my wifi ip address Android

纵然是瞬间 提交于 2019-11-26 19:49:08
How can I get the ip address of my phone when it is connected under wifi? I found a method here but it returns something like 24.182.239.255 even if I'm under wifi and I expect something like 192.168.1.10. I'd like something like: if (you are under wifi) String ip4 = getWifiIP() else String ip4 = getIPAddress with the method linked before Many thanks! If you would like to get the private IP address of your device when connected to Wi-Fi, you can try this. WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE); WifiInfo wifiInfo = wifiMgr.getConnectionInfo(); int ip = wifiInfo

Example: Android bi-directional network socket using AsyncTask

谁说我不能喝 提交于 2019-11-26 19:37:16
Most of the network socket examples I found for Android were one directional only. I needed a solution for a bi-directional data stream. I eventually learned of the AsyncTask. This example shows how to get data from a socket and send data back to it. Due to the blocking nature of a socket that is receiving data, that blocking needs to run in a thread other than the UI thread. For the sake of example, this code connects to a webserver. Pressing the "Start AsyncTask" button will open the socket. Once the socket is open, the web server waits for a request. Pressing the "Send Message" button will