Connecting to the Network via HttpURLConnection

Source: Internet
Author: User

Connecting to the Network

Note that to perform the network operations, your application manifest must include the following permissions.

    <uses-permission android:name= "Android.permission.INTERNET"/>    <uses-permission android:name= " Android.permission.ACCESS_NETWORK_STATE "/>

Choose an HTTP Client

Most network-connected Android apps use the HTTP to send and receive. Android includes-HTTP clients:httpurlconnection and Apache HttpClient. And it is recommend using HttpURLConnection for applications targeted at gingerbread and higher.

Check the Network Connection

Before your app attempts to connect to the network, it should check to see whether a network connection is available using Getactivenetworkinfo () and isconnected ().

public static Boolean isnetworkavailable (context context) {Connectivitymanager manager = (Connectivitymanager) Context.getsystemservice (Context.connectivity_service); Networkinfo info = manager.getactivenetworkinfo (); if (info! = null && info.isconnected ()) {return true;} return false;}

Perform Network Operations on a separate Thread

Network operations can involve unpredictable delays. To prevent the-causing a poor user experience, always perform network operations on a separate thread from the UI. The Asynctask provides one of the simplest ways to fire off a new task from the UI thread.

Private class Downloadtask extends Asynctask<string, Void, string> {private string DownloadURL (String path) {try {UR L url = new URL (path); HttpURLConnection connection = ((httpurlconnection) url.openconnection ()); Connection.setreadtimeout (10 * 1000); Connection.setconnecttimeout (5 *); Connection.setrequestmethod ("GET"); Connection.connect (); if ( Connection.getresponsecode () = = () {InputStream InputStream = Connection.getinputstream (); Bufferedinputstream bis = new Bufferedinputstream (InputStream); byte[] buffer = new Byte[1024];int length = Bis.read (buffe R); return new String (buffer, 0, length);}} catch (Malformedurlexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} return null;} @Overrideprotected string Doinbackground (String ... params) {return DownloadURL (Params[0]);} @Overrideprotected void OnPostExecute (String result) {log.i ("tag", result);}}

Connecting to the Network via HttpURLConnection

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.