-2

i get a NetworkOnMainThreadException, but as as i see it, it is not in the main thread. Here is my code(i use the library from svnkit.com)

public class MainActivity extends Activity{

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    sendFiles();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        thread.run();
    }

    void sendFiles() throws SVNException, IOException {    
        DAVRepositoryFactory.setup();
        SVNURL url = SVNURL.parseURIEncoded("...");
        SVNRepository repository = DAVRepositoryFactory.create(url);
        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager( "...", "...".toCharArray() );
        repository.setAuthenticationManager( authManager );
        long r= repository.getLatestRevision();
    }
}

the exception is thrown at repository.getLatestRevision(); (and of course catched in run()). Can anyone explain to me, why?

Ginso
  • 459
  • 17
  • 33
  • Because you are using UI thread for network operation. Use `AysncTask` For more help.. Read [here](https://developer.android.com/reference/android/os/AsyncTask.html) – Rethinavel Oct 13 '16 at 07:55

1 Answers1

1

Yes, of course. You call your Thread.run() on main thead. To avoid that error, use start instead of run.

Kingfisher Phuoc
  • 8,052
  • 9
  • 46
  • 86