Solution of HttpURLConnection throw exception in Android

Source: Internet
Author: User
Tags thread throw exception

HTTP URL link, an error occurs, mainly because in the main UI thread, the Networkonmainthreadexception exception is thrown by using network invoke (network call).

Version: API level above 11.

You can avoid this situation by using a simple thread that invokes the program in the background (asynctask);

Otherwise, the exception is thrown at the Getresponsecode () function.

Android api:http://developer.android.com/reference/android/os/asynctask.html.

Code:

String quakefeed = getString (r.string.quake_feed);  
url = new URL (quakefeed);  
      
URLConnection connection;  
Connection = Url.openconnection ();  
      
HttpURLConnection httpconnection = (httpurlconnection) connection;  
int responsecode = Httpconnection.getresponsecode ();

Modify:

Thread t = new Thread (new Runnable () {  
    @Override public
    void Run () {  
        refreshearthquakes ();  
    }  
});  
      
T.start ();

That is, put a program that requires network calls (network call) into threads to execute.

Author: csdn Blog spike_king

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

Related Article

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.