Android send GET request times wrong

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.