Home » Android » multithreading – android.os.networkonmainthreadexception inside a new Thread

multithreading – android.os.networkonmainthreadexception inside a new Thread

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am aware that you can’t do network operations in the main thread, since Android 3.0. So, I made my call inside a new Thread:

button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        user=login.getText().toString();
        password=pass.getText().toString();
        params.add(new BasicNameValuePair("user", user));
        params.add(new BasicNameValuePair("pass", password));
        Thread thread=new Thread(){
            public void run(){
                try {
                    // Throws exception here
                    response=CustomHttpClient.executeHttpPost(urlogin, params); 
                    response=response.replaceAll("\s+","");

                } catch (Exception e) {
                    e.printStackTrace();
                }
                if(response.equals("ok")){

                    Intent home=new Intent(c, HomeActivity.class);
                    home.putExtra("username", user);
                    startActivity(home);
                    Toast toast=Toast.makeText(
                        c, getString(R.string.welcome), Toast.LENGTH_LONG);
                    toast.show();
                }else{
                    if(response.equals("fallo")){
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                Toast toast=Toast.makeText(
                                    c, R.string.nologin, Toast.LENGTH_LONG);
                                toast.show();
                                login.setText("");
                                pass.setText("");
                            }
                        });
                    }else if(response.equals("nologin")){
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                Toast toast=Toast.makeText(
                                    c, R.string.nouser, Toast.LENGTH_LONG);
                                toast.show();
                                login.setText("");
                                pass.setText("");

                            }
                        });
                    }
                }
            }
        };
        thread.run();
    }
});

But, I receive that exception despite I’m not on main thread (or at least I think that…)

How to&Answers:

using run() does not start a separate thread, it actually starts a runnable on the same thread that it was started on.

you need to use start() to start a new thread.

Answer:

These lines

Toast toast=Toast.makeText(c, getString(R.string.welcome), Toast.LENGTH_LONG);
toast.show();

need to be ran on the UI Thread as you are doing a couple times below it with runOnUiThread(). Toast is a UI element.

Answer:

You can use this code

if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = 
        new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

to avoid exception also for SDK > 9 you need to execute network operations on seperate thread other than UI or you can use AsyncTask.