Android - android.os.NetworkOnMainThreadException

后端 未结 8 1187
迷失自我
迷失自我 2020-11-22 08:15

I have this exception and I was reading a thread on this, and it seemed confusing:

How to fix android.os.NetworkOnMainThreadException?

I already added this l

相关标签:
8条回答
  • 2020-11-22 08:37

    NetworkOnMainThreadException is thrown when your app tries networking operation in main thread.

    To fix that you can use a private inner class within your Activity that extends android.os.AsyncTask<Params, Progress, Result> which will do the server call stuffs.

    Something as,

    private class SendfeedbackJob extends AsyncTask<String, Void, String> {
    
        @Override
        protected String doInBackground(String[] params) {
            // do above Server call here
            return "some message";
        }
    
        @Override
        protected void onPostExecute(String message) {
            //process message
        }
    }
    

    And then invoke above class from submit.setOnClickListener as below,

    SendfeedbackJob job = new SendfeedbackJob();
    job.execute(pass, email);
    

    AsyncTask

    References

    AsyncTask doc

    AsyncTask Android example

    0 讨论(0)
  • 2020-11-22 08:41
    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
    
    0 讨论(0)
  • 2020-11-22 08:43

    look at this link : https://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

    The exception that is thrown when an application attempts to perform a networking operation on its main thread. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged.

    if you set minSdkVersion <11, then your application will work and you can run network operation in main thread.

    0 讨论(0)
  • 2020-11-22 08:51
     try 
        {  
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("myUrl");
    
            // no idea what this does :)
            httppost.setEntity(new UrlEncodedFormEntity(postParameters));
    
            // This is the line that send the request
            HttpResponse response = httpclient.execute(httppost);
    
            HttpEntity entity = response.getEntity();            
    
            InputStream is = entity.getContent();
        } 
        catch (Exception e) 
        {     
            Log.e("log_tag", "Error in http connection "+e.toString());
        }        
    

    Here is your problem. Since api 11, this exception will inform you that you are running long tasks on the ui thread (the http communication in your class), and according with the new StrictGuard policy this is not possibile. So you have two different choice

    1. Use thread or aynctask in order to execut yout long term task (better way)
    0 讨论(0)
  • 2020-11-22 08:55

    You can just create Async class as below

    class Retrievedata extends AsyncTask<String, Void, String> {
    @Override
        protected String doInBackground(String... params) {
             try{
                    //Your code 
                    }
            return null;
        }
    }
    

    You can just put your all code inside doInBackground method

    0 讨论(0)
  • 2020-11-22 08:56

    NetworkOnMainThreadException: The exception that is thrown when an application attempts to perform a networking operation on its main thread.

    You should call sendfeedback method on asynctask then only above code will work. As webserver is taking lot of time to response main thread becomes unresponsive. To avoid it you should call it on another thread. Hence asynctask is better.

    here is link that illustrates how to use asynctask

    0 讨论(0)
提交回复
热议问题