简体   繁体   English

AsyncTask中的android.os.NetworkOnMainThreadException

[英]android.os.NetworkOnMainThreadException in AsyncTask

I realise that this error happens when you attempt to do some sort of network request on the UI thread, but as you can see in the code below I am actually calling the Http Get in an AsyncTask: 我意识到当你尝试在UI线程上做某种网络请求时会发生这个错误,但正如你在下面的代码中看到的那样,我实际上是在AsyncTask中调用Http Get:

public class LeftPaneFragment extends Fragment {

    private ImageView _profileImage;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(wj.tweetTab.R.layout.left_pane, container);

        _profileImage = (ImageView) view.findViewById(R.id.profileImage);

        setUpProfileInfo(view);

        return view;
    }

    private void setUpProfileInfo(View view) {          
        new SetUpUserInfo().doInBackground();
    }

    private class SetUpUserInfo extends AsyncTask<Void, Void, Drawable> {

        @Override
        protected Drawable doInBackground(Void... params) {

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet request = new HttpGet(_model.UserInfo.ProfileImageUrl);

            InputStream inputStream = null;

            try {
                HttpResponse response = httpClient.execute(request);        
                inputStream = response.getEntity().getContent();
            }
            catch (Exception e) {               
                Log.e("setUpUserInfo.doInBackground", e.getMessage());
            }

            return Drawable.createFromStream(inputStream, "src");
        }

        @Override
        protected void onPostExecute(Drawable result) {
            _profileImage.setImageDrawable(result);
        }
    }
}

Can anyone see any obvious problems here? 谁能在这里看到任何明显的问题? Also can the NetworkOnMainThreadException exception be thrown for any other reason than doing a http request in the main thread? 除了在主线程中执行http请求之外,还可以抛出NetworkOnMainThreadException异常吗?

I am a newcomer to Android, only been working with it a few days. 我是Android的新手,只用了几天就搞定了。

but as you can see in the code below I am actually calling the Http Get in an AsyncTask 但正如您在下面的代码中看到的那样,我实际上是在AsyncTask中调用Http Get

You're not, actually. 实际上,你不是。 You need to call execute() instead of calling doInBackground() directly, otherwise you're not using any of the plumbing provided by the AsyncTask , and you're just calling the method directly in the UI thread. 您需要调用execute()而不是直接调用doInBackground() ,否则您不会使用AsyncTask提供的任何管道,而只是直接在UI线程中调用该方法。

Maybe Android SDK version is to high (version >= 3.0). 也许Android SDK版本很高(版本> = 3.0)。

Try to add code 尝试添加代码

import android.os.StrictMode; import android.os.StrictMode;

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()   
        .detectDiskReads()   
        .detectDiskWrites()   
        .detectNetwork()   // or .detectAll() for all detectable problems   
        .penaltyLog()   
        .build());  

in onCreateView() function; onCreateView()函数中;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM