Issue
I'm using the Ion Library (Android Asynchronous Networking and Image Loading) on Android. The typical code to do a generic HTTP petition is similar to this:
Ion.with(context)
.load("http://example.com/thing.json")
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
// do stuff with the result or error
}
});
I don't know what to do when there is no internet connection. I'm debugging on my phone with wifi and 3G disabled, and I'm not receiving any exception on the callback. I expect a exception inmediately, I think that it must fail instantly.
How I must manage this scenario? Thanks in advance
Solution
The getActiveNetworkInfo() method of ConnectivityManager returns a NetworkInfo instance representing the first connected network interface it can find or null if none of the interfaces are connected. Checking if this method returns null should be enough to tell if an internet connection is available or not.
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
You will also need:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
in your android manifest.
Answered By - Vinay Jayaram
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.