android-networking
Workaround mapping between NetworkInfo and NetworkInterface
Since API 21, you can use LinkProperties to get a NetworkInterface object out of a Network network object: ConnectivityManager manager = getSystemService(ConnectivityManager.class); LinkProperties prop = manager.getLinkProperties(network); NetworkInterface iface = NetworkInterface.getByName(prop.getInterfaceName()); However, although getAllNetworks() is supposed to replace getAllNetworkInfo() (the former, introduced in API 21, has deprecated the latter in API 23), as of Android 6.0.1, …
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 …
Volley and AsyncTask
You don’t need to run Volley request on AsyncTask. Why: They manage all network related task on separate thread. If you look closely at library project they did not picture the AsyncTask. But they intelligently handle all network related task efficiently. Check RequestQueue.java class in Volley’s main package here I am pasting java doc. /** …
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(); } …
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 …
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, …
Volley Android Networking Library
$ git clone https://android.googlesource.com/platform/frameworks/volley $ cd volley $ android update project -p . $ ant jar Then, copy bin/volley.jar into your libs/ folder and off you go! source
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 …
Broadcast receiver for checking internet connection in android app
Answer to your first question: Your broadcast receiver is being called two times because You have added two <intent-filter> Change in network connection : <action android:name=”android.net.conn.CONNECTIVITY_CHANGE” /> Change in WiFi state: <action android:name=”android.net.wifi.WIFI_STATE_CHANGED” /> Just use one: <action android:name=”android.net.conn.CONNECTIVITY_CHANGE” />. It will respond to only one action instead of two. See here for more information. …