0

When I try to run this code it gives my this error android.os.NetworkOnMainThreadException Here is the complete code:

final EditText editText = (EditText) findViewById(R.id.ed);
    Button buttonX = (Button) findViewById(R.id.ok);
    buttonX.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String message = editText.getText().toString();
            new Thread() {
                public void run() {
                    Log_in.this.runOnUiThread(new Runnable() {
                        @Override 
                        public void run() {
                            try {
                                URL url = new URL(message);
                                HttpURLConnection.setFollowRedirects(false);
                                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                                con.setRequestMethod("HEAD");
                                con.connect();
                                Log.d("URL Result", "con.getResponseCode() IS : " + con.getResponseCode());
                                if ((con.getResponseCode() >= 200) && (con.getResponseCode() <= 399)) {
                                    Log.d("URL Result", "Sucess");

                                    Toast.makeText(getBaseContext(), "GOOD!", Toast.LENGTH_SHORT).show();

                                    Intent intent = new Intent(Log_in.this, MainActivity.class);
                                    startActivity(intent);
                                } else
                                    Log.d("URL Result", "con.getResponseCode() IS : " + con.getResponseCode());
                            } catch (Exception e)

                            {
                                e.printStackTrace();
                                Log.d("URL Result", "fail");
                            }
                        }
                    });
                }
            }.start();
        }
    });
}

I think I should use AsyncTask but I do not know how to use it on my current code, does someone have any idea how I should do this or my code is messed up and I should change it somehow?

We're All Mad Here
  • 1,544
  • 3
  • 18
  • 46
  • 2
    possible duplicate of [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – marcinj Sep 11 '15 at 13:14
  • Well I don't think it is and that's why I asked the question, as I said I don't know how to use `Asynctask` to my code – We're All Mad Here Sep 11 '15 at 13:21
  • 1
    Type AsynTask in google search you will get lot of tutorial...anyways for your case make the httpUrlconnection call in doInBackground method and pass the response code to onPostExecute method of asynctask...Inside onPostExecute based on response code display Toast or start an activity – Shadow Droid Sep 11 '15 at 13:34
  • I guess I could do that as well but now I solved my problem, thank you – We're All Mad Here Sep 11 '15 at 13:40

1 Answers1

2
new Thread() {
       public void run() {
             Log_in.this.runOnUiThread(new Runnable() {

you are spawning a thread just to make its run method run on the UI Thread. That's the reason why you are getting the NetworkOnMainThreadException. You have to use

 Log_in.this.runOnUiThread(new Runnable() {

only to make changes to the UI and to show your Toast

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Well I tried what you said but I have to use `public void run` because if I don't an error occurs saying `invalid method declaration; return type required`. Moreover if I delete the run method below the runnable the urlconnection creates unhandled exception, would you be as kind to help me more please? – We're All Mad Here Sep 11 '15 at 13:20
  • could you update your question or make a gist with the changes you made ? – Blackbelt Sep 11 '15 at 13:26
  • I just commented the `new Thread {..}` and the `public void run {..}` above the runnable but the same error still occurs, sorry I am being handful but I can't understand how to do it – We're All Mad Here Sep 11 '15 at 13:30
  • 1
    what I mean is something like [this](https://gist.github.com/anonymous/16c0539e2562fcf58257) – Blackbelt Sep 11 '15 at 13:35
  • Thank you very much for thoroughly explaining this, it works – We're All Mad Here Sep 11 '15 at 13:39