Skip to content

Commit

Permalink
Fix NetworkOnMainThreadException caused by dnsjava
Browse files Browse the repository at this point in the history
refresh().

java.lang.ExceptionInInitializerError
at org.jivesoftware.smack.SmackAndroid$1.onReceive(SmackAndroid.java:26)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:763)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5306)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getByName(InetAddress.java:289)
at org.xbill.DNS.SimpleResolver.<init>(SimpleResolver.java:56)
at org.xbill.DNS.SimpleResolver.<init>(SimpleResolver.java:68)
at org.xbill.DNS.ExtendedResolver.<init>(ExtendedResolver.java:266)
at org.xbill.DNS.Lookup.refreshDefault(Lookup.java:86)
at org.xbill.DNS.Lookup.<clinit>(Lookup.java:97)
... 11 more
  • Loading branch information
Flowdalic committed Jun 6, 2014
1 parent 2c6eee7 commit 7d4c109
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions static-src/custom/org/jivesoftware/smack/SmackAndroid.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,29 @@ public class SmackAndroid {
private BroadcastReceiver mConnectivityChangedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
LOGGER.fine("ConnectivityChange received, calling ResolverConfig.refresh() and Lookup.refreshDefault()");
ResolverConfig.refresh();
Lookup.refreshDefault();
LOGGER.fine("ConnectivityChange received, calling ResolverConfig.refresh() and Lookup.refreshDefault() in new Thread");
// Lookup.refreshDefault may cause network I/O. So in order to prevent a NetworkOnMainThreadException,
// we refresh dnsjava in a new thread.
// Full stacktrace of the offending call:
// Caused by: android.os.NetworkOnMainThreadException
// at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128)
// at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
// at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
// at java.net.InetAddress.getByName(InetAddress.java:289)
// at org.xbill.DNS.SimpleResolver.<init>(SimpleResolver.java:56)
// at org.xbill.DNS.SimpleResolver.<init>(SimpleResolver.java:68)
// at org.xbill.DNS.ExtendedResolver.<init>(ExtendedResolver.java:266)
// at org.xbill.DNS.Lookup.refreshDefault(Lookup.java:86)
// at org.xbill.DNS.Lookup.<clinit>(Lookup.java:97)
Thread thread = new Thread() {
@Override
public void run() {
ResolverConfig.refresh();
Lookup.refreshDefault();
}
};
thread.setDaemon(true);
thread.start();
}
};

Expand Down

0 comments on commit 7d4c109

Please sign in to comment.