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

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.WIFI_STATE_CHANGED” /> </intent-filter> </receiver> And implement the Receiver with something like this: public class NetworkChangeReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, final Intent intent) { final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); final android.net.NetworkInfo wifi = … Read more

Get response status code using Retrofit 2.0 and RxJava

Instead of declaring the API call like you did: Observable<MyResponseObject> apiCall(@Body body); You can also declare it like this: Observable<Response<MyResponseObject>> apiCall(@Body body); You will then have a Subscriber like the following: new Subscriber<Response<StartupResponse>>() { @Override public void onCompleted() {} @Override public void onError(Throwable e) { Timber.e(e, “onError: %”, e.toString()); // network errors, e. g. UnknownHostException, … Read more

How can I fix ‘android.os.NetworkOnMainThreadException’?

NOTE : AsyncTask was deprecated in API level 30. AsyncTask | Android Developers This exception is thrown when an application attempts to perform a networking operation on its main thread. Run your code in AsyncTask: class RetrieveFeedTask extends AsyncTask<String, Void, RSSFeed> { private Exception exception; protected RSSFeed doInBackground(String… urls) { try { URL url = … Read more

Example: Android bi-directional network socket using AsyncTask

The SendDataToNetwork task runs in the main ui thread, meaning it will crash a Honeycomb or higher app due to NetworkOnMainThreadException Fatal exception. Here’s what my SendDataToNetwork looks like to avoid this issue: public boolean sendDataToNetwork(final byte[] cmd) { if (_nsocket.isConnected()) { Log.i(TAG, “SendDataToNetwork: Writing received message to socket”); new Thread(new Runnable() { public void … Read more

Trusting all certificates with okHttp

Just in case anyone falls here, the (only) solution that worked for me is creating the OkHttpClient like explained here. Here is the code: private static OkHttpClient getUnsafeOkHttpClient() { try { // Create a trust manager that does not validate certificate chains final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @Override public void … Read more

How to determine if network type is 2G, 3G or 4G

You can put this following method directly in your Utility class: Kotlin: /** Usage: `networkTypeClass(telephonyManager.networkType)` */ fun networkTypeClass(networkType: Int): String { when (networkType) { TelephonyManager.NETWORK_TYPE_GPRS, TelephonyManager.NETWORK_TYPE_EDGE, TelephonyManager.NETWORK_TYPE_CDMA, TelephonyManager.NETWORK_TYPE_1xRTT, TelephonyManager.NETWORK_TYPE_IDEN, TelephonyManager.NETWORK_TYPE_GSM -> return “2G” TelephonyManager.NETWORK_TYPE_UMTS, TelephonyManager.NETWORK_TYPE_EVDO_0, TelephonyManager.NETWORK_TYPE_EVDO_A, TelephonyManager.NETWORK_TYPE_HSDPA, TelephonyManager.NETWORK_TYPE_HSUPA, TelephonyManager.NETWORK_TYPE_HSPA, TelephonyManager.NETWORK_TYPE_EVDO_B, TelephonyManager.NETWORK_TYPE_EHRPD, TelephonyManager.NETWORK_TYPE_HSPAP, TelephonyManager.NETWORK_TYPE_TD_SCDMA -> return “3G” TelephonyManager.NETWORK_TYPE_LTE -> return “4G” TelephonyManager.NETWORK_TYPE_NR -> return “5G” else … Read more

Intent action for network events in android sdk

Here’s a working example: <uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” /> <receiver android:name=”.receiver.ConnectivityReceiver”> <intent-filter> <action android:name=”android.net.conn.CONNECTIVITY_CHANGE” /> </intent-filter> </receiver> . public class ConnectivityReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d(ConnectivityReceiver.class.getSimpleName(), “action: ” + intent.getAction()); } }