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

Get my wifi ip address Android

So something to consider is that Formatter.formatIpAddress(int) is being deprecated: This method was deprecated in API level 12. Use getHostAddress(), which supports both IPv4 and IPv6 addresses. This method does not support IPv6 addresses. So using formatIpAddress(int) is likely not a good long term solution, although it will work. Here is a potential solution if … 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()); } }

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

Network listener Android

New java class: public class ConnectionChangeReceiver extends BroadcastReceiver { @Override public void onReceive( Context context, Intent intent ) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE ); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo( ConnectivityManager.TYPE_MOBILE ); if ( activeNetInfo != null ) { Toast.makeText( context, “Active Network Type : ” + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show(); } … Read more