0

I am using fragments to take user data and then in my last fragment I am using a submit button to submit the data to the server to save it, The problem which I am facing is when I click submit button, the app crashes and says unfortunately app has stopped I think its due to android.os.networkOnMainThreadException because this error I was getting previously.

My code of last fragment where I am having submit button is

View rootView = inflater.inflate(R.layout.fragment_dependent, container, false);

    Button submit = (Button) rootView.findViewById(R.id.submit);

    final EditText empid = (EditText) getActivity().findViewById(R.id.empidtext);
    final EditText fname = (EditText) getActivity().findViewById(R.id.fnametext);
    final EditText lname = (EditText) getActivity().findViewById(R.id.lnametext);
    final EditText family = (EditText) getActivity().findViewById(R.id.familytext);
    final EditText email = (EditText) getActivity().findViewById(R.id.emailtext);
    final EditText religion = (EditText) getActivity().findViewById(R.id.religiontext);
    final EditText nationality = (EditText) getActivity().findViewById(R.id.nationalitytext);
    final EditText chronicdiseases = (EditText) getActivity().findViewById(R.id.chronicdiseasestext);


    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Toast.makeText(getActivity(), empid.getText().toString()+" : "+fname.getText().toString(),
                    Toast.LENGTH_SHORT).show();
            eid = empid.getText().toString();
            name = fname.getText().toString();

            new Insert().execute();
        }
    });

    return rootView;

Now I am using asynctask, its code is

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

    protected String doInBackground(Void... params) {

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

        nameValuePairs.add(new BasicNameValuePair("empid",eid));
        nameValuePairs.add(new BasicNameValuePair("fname",name));
        //nameValuePairs.add(new BasicNameValuePair("lname",lname.getText().toString()));
        //nameValuePairs.add(new BasicNameValuePair("family",family.getText().toString()));

        try
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://192.168.0.103/insert.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            iss = entity.getContent();

            Toast.makeText(getActivity(), "Done",
                    Toast.LENGTH_LONG).show();
        }
        catch(Exception e)
        {
            Toast.makeText(getActivity(), "Error "+e.toString(),
                    Toast.LENGTH_LONG).show();
        }

        return "done";
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
knownUnknown
  • 869
  • 11
  • 18

1 Answers1

1

show the toast message in ui thread or move the toast message to onPostExecute() method.

 try
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://192.168.0.103/insert.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        iss = entity.getContent();

        runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        Toast.makeText(getActivity(), "Done",Toast.LENGTH_LONG).show();
                    }
                });


    }
    catch(Exception e)
    {
        Toast.makeText(getActivity(), "Error "+e.toString(),
                Toast.LENGTH_LONG).show();
    }
SaravInfern
  • 3,338
  • 1
  • 20
  • 44