Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`

泄露秘密 提交于 2019-12-24 02:39:11

问题


When I run this piece of code on Android 2.3.6, it works fine. On 4.0 I get a StrictMode exception. From some questions on SO, seems like you cant do network operations on the UI thread. But I am not doing any UI operation on the UI thread.

I am calling doBackground directly and not execute, since I need to check the return value of the doBackground. I guess the below should have worked.

What am I missing here ?

handler.postDelayed(new Runnable() {
@Override
public void run() {
    God.identityHash = (String) new SwitchServer((IActionBar) splashScreen,
                God.mCruiseOnServer, nameText, phoneNumberText)
                .doInBackground(null);

    if (!mIsBackButtonPressed && God.identityHash != null) {
        FlurryAgent.logEvent(God.TCACCEPTED_AND_REGISTERED);
        Intent intent = new Intent(SplashScreen.this, HomeScreen.class);
        SplashScreen.this.startActivity(intent);
        finish();
    } else {
        Toast.makeText(splashScreen,
            "Unable to save your details. Please try again.",
            Toast.LENGTH_LONG).show();
        God.setTermsAndConditions(splashScreen, false);
    }
}
}, SPLASH_DURATION); 

Edit : Strict mode is not enabled on the phone.


回答1:


Firstly, StrictMode is something you explicitly turned on from code, it is just a debugging aid, and should not be left on in production code.

Secondly, calling .doInBackground manually means running it on the UI thread. If you use AsyncTask as intended, you get its return value as a parameter in .onPostExecute.



来源:https://stackoverflow.com/questions/15921252/exception-on-android-4-0-android-os-strictmodeandroidblockguardpolicy-onnetwor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!