title: What is the best way to solve android.os.NetworkOnMainThreadException
body: <p>I am trying to put the file throght webdav in android,</p>
  
  <p>after i execute the program, the Logcat appear android.os.NetworkOnMainThreadException</p>
  
  <p>i had try two ways to sloves it, likes the code below</p>
  
  <pre><code>        if (android.os.Build.VERSION.SDK_INT &gt; 9) {
              StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
              StrictMode.setThreadPolicy(policy);
          }
  </code></pre>
  
  <p>and this one</p>
  
  <pre><code>        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()  
          .detectDiskReads()  
          .detectDiskWrites()  
          .detectNetwork()   // or .detectAll() for all detectable problems  
          .penaltyLog()  
          .build());  
          StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()  
          .detectLeakedSqlLiteObjects()  
          .detectLeakedClosableObjects()  
          .penaltyLog()  
          .penaltyDeath()  
          .build());  
  </code></pre>
  
  <p>it could work but those ways force my project to close,</p>
  
  <p>so i thought its not good ways to use on my project</p>
  
  <p>after that i had been researched on it,</p>
  
  <p>everyone recommended this way to solve it</p>
  
  <p><a href="https://stackoverflow.com/questions/23305588/android-os-networkonmainthreadexception-how-to-solve-it">android.os.NetworkOnMainThreadException. How to solve it?</a></p>
  
  <p>But i am beginner i dont know how to fill my code in,</p>
  
  <p>I would like to know how could I fill in my code in this way,</p>
  
  <p>Thank you in advance for any assistance that can be provided here.</p>
  
  <pre><code>         loginConfirmBtn.setOnClickListener(new Button.OnClickListener(){
              @Override
              public void onClick(View v) {
                  ConnectivityManager connMgr = (ConnectivityManager) 
                      getSystemService(Context.CONNECTIVITY_SERVICE);
                      NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
                      if (networkInfo != null &amp;&amp; networkInfo.isConnected()) {
  
                          getIpAddress = ipAddress.getText().toString().trim();
                          getUserName = userName.getText().toString().trim();
                          getPassWord = passWord.getText().toString().trim();
  
                          uploadFiles(sourceFilePath);
  
                          Toast.makeText(MainActivity.this, "upload suseeful", Toast.LENGTH_SHORT).show();
                          hideKeyboard();
  
                      } else {
  
                          Toast.makeText(MainActivity.this, "make sure you have connected the internet", Toast.LENGTH_SHORT).show();                          
             }
  
          public void uploadFiles(String dir){
              Log.d("edoc","PutRunGenuine");
              String fileFromEscan = dir;
  
              Conn conn_adv = new Conn(new Constants(getUserName, getPassWord, getIpAddress, 80));                                conn_adv.setUrl("/collab/my/"+conn_adv.getUser()+"/sourceFilePath2");
              Log.d("edoc","edoc test");
                 if (fileFromEscan == null) 
                 {
                     return;
                 }
                 try {                 
                 String putAvg = "PUT avg: ";
                 ExtractedMethod exm =  new ExtractedMethod();
                 exm.run(HTCode.Code.PUT, 1, conn_adv, true, fileFromEscan, 1000, true);
  
                 Log.d("edoc","edoc_message hello");
                 synchronized(conn_adv){
                     conn_adv.notify();                           
                 }
  
                 } catch (Exception io) {
                     io.printStackTrace();
                     Log.d("edoc","edoc Exception"+io);
                   }
             }
  </code></pre>