Exception information:
Java.lang.RuntimeException:Unable to start Activity componentinfo{com.synology.synologycloud/ Com.synology.synologycloud.MainActivity}: Android.os.NetworkOnMainThreadException
The first time you see this exception, literally means "network exception" in the main thread. And then I went to get to know the next anomaly, first look at the official note.
Public classNetworkonmainthreadexceptionextends RuntimeException
| Java.lang.Object |
| ? |
Java.lang.Throwable |
|   |
? |
java . lang. Exception |
|   |
  |
? |
java . lang. RuntimeException |
|
|
|
? |
Android.os.NetworkOnMainThreadException |
Class Overview
The exception is thrown when an application attempts to perform a networking operation on its main thread.
This is a thrown for applications targeting the honeycomb SDK or higher. Applications targeting earlier SDK versions is allowed to does networking on their main event loop threads, but it's heavil Y discouraged. See the document designing for responsiveness.
Also See StrictMode .
An app will throw this exception if it requests a network operation in the main thread. The Android design is designed to prevent the Web request from being too long and causing the interface to suspend animation.
So a GET request cannot be initiated in the main UI thread, my workaround is to open another thread, as follows:
No more access to network resources in the main thread in Android
New Thread (New Runnable () {
@Override
public void Run () {
try {
Getoauthtoken ();
} catch (Malformedurlexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (Unsupportedencodingexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}). Start ();