-1

I am getting NetworkOnMainThreadException while running my code. I have a Fragment where i am showing some ID from the webservices that gets called when i click on a button. Following is my code. I have used Asynctask as mentioned for this purpose but still i keep getting this error.

public class AboutMeFragView extends Fragment implements ObsrvIntModel {

    private Button getConfButton;
    private UsrDataCtrl m_UsrDataCtrl;
    private UsrDataModel m_UsrDataModel;
    private boolean m_bResUpdate;
    private String retc;

    public static AboutMeFragView newInstance() {
        AboutMeFragView aboutMeFragment = new AboutMeFragView();
        return aboutMeFragment;
    }

    public AboutMeFragView() {}

    //inflate the data on this view from the relevant xml file fragment_about_me.xml
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_about_me, container, false);

        getConfButton = (Button) rootView.findViewById(R.id.get_config_button);

        getConfButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // Toast.makeText(getActivity(), "Implement methods to get the configuration", Toast.LENGTH_LONG).show();
                //call your model to get the data from the server and show it on the UI
               enableStrictMode();
                new GetCredsTask().execute();

            }
        });

        return rootView;
    }

    //whenever fragment is associated with our main activity
    //following method would get called
    //also we make sure here that whatever navigation activity is selected
    //our action bar shows up the same activity name


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        ((CpActivity)activity).onSectionAttached(1);
    }


    @Override
    public void update(boolean result) {
          m_bResUpdate = result;
    }

    public void enableStrictMode()
    {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

        StrictMode.setThreadPolicy(policy);
    }


    private class GetCredsTask extends AsyncTask<Void, Void, String> {

        public GetCredsTask() {
            super();
        }

        @Override
        protected String doInBackground(Void... params) {
            m_UsrDataModel = new UsrDataModel(AboutMeFragView.this);
            m_UsrDataCtrl = new UsrDataCtrl(m_UsrDataModel);
            m_UsrDataCtrl.execConfig();

            retc = m_UsrDataModel.getM_authid();

            if(m_bResUpdate != true) {
                retc = "404";
            }
            Log.d("doInBackground", retc);
            return retc;
        }


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String s) {
            Log.d("onPostExecute", retc);
            if (m_bResUpdate == true)
                Toast.makeText(getActivity(), s, Toast.LENGTH_LONG).show();
            else
                Toast.makeText(getActivity(), retc, Toast.LENGTH_LONG).show();

            super.onPostExecute(s);
        }


        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        protected void execute() {
            doInBackground();
        }
    }
}

Thanks

kuldeep
  • 817
  • 10
  • 27
  • No because I already checked that thread. I think I did not miss anything , but whatever I have missed I hope to get some help from here! Thanks – kuldeep Apr 13 '15 at 16:53
  • 3
    Remove "protected void execute() { doInBackground(); }" Method. No need to override it. – Dhaval Patel Apr 13 '15 at 16:53

1 Answers1

2

You are overriding execute(), which is causing the task to be posted on the main thread instead of executed in the background. The normal implementation posts the execution of the task to a background thread, i.e.

Edit:

    public final AsyncTask<Params, Progress, Result> More ...execute(Params... params) {
        return executeOnExecutor(sDefaultExecutor, params);
   }
Submersed
  • 8,810
  • 2
  • 30
  • 38
  • Thanks a lot ! However, I have a question out of curiosity, How do we know that overriding execute or other possible things may lead our code to execute in main UI thread ? Any help appreciated. – kuldeep Apr 13 '15 at 16:57
  • 1
    You can check the looper or thread that you're on using Looper.getMainLooper().equals(Looper.myLooper()). For more info see the Looper reference. All lifecycle methods by default run on the main thread, so unless you post execution to a separate thread just assume it's running on the main thread if you're calling a method from one of your lifecycle methods. – Submersed Apr 13 '15 at 16:59