-3

I am trying to put the file throght webdav in android,

after i execute the program, the Logcat appear android.os.NetworkOnMainThreadException

i had try two ways to sloves it, likes the code below

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

and this one

        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());  

it could work but those ways force my project to close,

so i thought its not good ways to use on my project

after that i had been researched on it,

everyone recommended this way to solve it

android.os.NetworkOnMainThreadException. How to solve it?

But i am beginner i dont know how to fill my code in,

I would like to know how could I fill in my code in this way,

Thank you in advance for any assistance that can be provided here.

         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 && 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);
                 }
           }
Community
  • 1
  • 1

4 Answers4

2

android.os.NetworkOnMainThreadException

Means that you are doing some Network job in the MainThread. However any Networking job should be done in other thread than the Main(UI Thread)

You have to move uploadFiles(sourceFilePath); method to the BackgroundThread. As some suggested on the comments you can use AsyncTask and move that method to doInBackground()

hrskrs
  • 4,447
  • 5
  • 38
  • 52
0

hungwei0331,

You have to do your work inside doInBackground() function of async task. Because, Network operations are time-consuming so you can't execute this function on activity thread.

Solution is to use asyncTask or thread.

Kirankumar Zinzuvadia
  • 1,249
  • 10
  • 17
0

Use AsyncTask and perform your network operations in its doInBackground.

Narendra Singh
  • 3,990
  • 5
  • 37
  • 78
0

Easiest way is Thread with join. It's ugly but workable.

Thread t = new Thread(new Runnable() {
  public void run() {
    //do all here
    });
  }
});

t.Start();
t.Join();
Alexey Kurilov
  • 438
  • 4
  • 10