Android Network connection processing analysis _android

Source: Internet
Author: User

In Android, there are several ways to achieve network programming:

Create a URL and use the Urlconnection/httpurlconnection

Using HttpClient

Using WebView

Create a URL and use the Urlconnection/httpurlconnection

JAVA.NET.* provides the basic functionality to access the HTTP service below. The basic operations that use this part of the interface include:

Creating URLs and Urlconnection/httpurlconnection objects

1 Setting Connection Parameters

2 Connecting to the server

3 Write data to the server

4 reading data from the server

Source Code :

try { 
       //Create URL object  
         url url = new URL ("Http://t.sina.cn/fesky");  
       // Create URL connection  
        urlconnection connection = url.openconnection (); 
       //For HTTP connections can be directly converted to httpurlconnection, 
        /So you can use some HTTP connection-specific methods, such as Setrequestmethod ()  
        //HttpURLConnection connection 
       //= ( HttpURLConnection) url.openconnection (proxy_yours);  
       //Setting parameters   Www.jb51.net
        connection.setconnecttimeout (10000);  
        connection.addrequestproperty ("User-agent", "j2me/midp2.0");  
       //Connection server  
        Connection.connect ();  

   } catch (IOException e) { 
        //TODO auto-generated catch block 
        E.printstacktrace ();  
   }
uses httpclient
for HttpClient class, You can use the HttpPost and HttpGet classes and HttpResponse for network connectivity.

Using WebView

A high-performance webkit kernel browser is built into the Android phone, encapsulated as a WebView component in the SDK.

1. WebView's XML definition:

<webview
Android:id= "@+id/webview"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
/>
Set permissions in 2.Manifest files:
<uses-permission android:name= "Android.permission.INTERNET"/>
3. If you want to support JavaScript:webview.getSettings (). Setjavascriptenabled (True);
4. If you need to display a Web page in WebView instead of browsing in a built-in browser, you need to mwebview.setwebviewclient and override the Shouldoverrideurlloading method.

5. If you do not do any processing, in the display of your Brower UI, click the System "back" button, the entire browser will as a whole "back" to other activity, rather than the hope in the browser history page back. If you want to implement back in the history page, you need to handle the back event in the current activity: Mwebview.goback ();

WebView WebView;


/** called when the "activity is" is a. */ 


@Override


public void OnCreate (Bundle savedinstancestate) {


super.oncreate (savedinstancestate);


Setcontentview (R.layout.main);


//Get WebView object


WebView = (webview) Findviewbyid (R.id.webview);


//enabling JavaScript


webview.getsettings (). Setjavascriptenabled (True);


//If you need to display the page in WebView instead of browsing in the built-in browser,


You need to mwebview.setwebviewclient and rewrite the


//Shouldoverrideurlloading method.


webview.setwebviewclient (New Webviewclientdemo ());


//Loading Web page


Webview.loadurl ("Http://t.sina.cn/fesky");


    } 


@Override


public boolean onKeyDown (int keycode, keyevent event) {


//Press the Back button to return to the history page


if ((keycode = = keyevent.keycode_back) &amp;&amp; webview.cangoback ()) {


Webview.goback ();


return true;


        }  


return Super.onkeydown (KeyCode, event);


    } 


Private class Webviewclientdemo extends Webviewclient {


@Override


//Display page in WebView instead of the default browser


public boolean shouldoverrideurlloading (webview view, String URL) {


view.loadurl (URL);


return true;


        }  


    }


webview.loaddata (HTML, "text/html", "utf-8");

If the HTML contains Chinese, you need Webview.loaddata (Urlencoder.encode (html,encoding), mimetype, encoding);

You can use Loadurl for the display of a local picture or Web page, but the address prefix for the URL is file:///, such as "file:///android_asset/test.htm".

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.