In Android, network programming can be implemented in multiple ways:
- Create a URL and use urlconnection/httpurlconnection
- Use httpclient
- Use webview
Create a URL and use urlconnection/httpurlconnection
Java.net. * provides basic functions for accessing the HTTP service. The basic operations for using these interfaces include:
- Create URL and urlconnection/httpurlconnection object
- Set connection Parameters
- Connect to the server
- Write Data to the server
- Read data from the server
Source code:
Try{// Create a URL objectURL url =NewURL ("Http://t.sina.cn/fesky");// Create a URL ConnectionUrlconnection connection = URL. openconnection ();// You can directly convert an HTTP connection to httpurlconnection,// You can use some HTTP connection methods, such as setrequestmethod ().// Httpurlconnection connection// = (Httpurlconnection) URL. openconnection (proxy_yours );// Set parametersConnection. setconnecttimeout (10000); connection. addrequestproperty ("User-Agent","J2-midp2.0");// Connect to the serverConnection. Connect ();}Catch(Ioexception e ){// Todo auto-generated Catch BlockE. printstacktrace ();}
Use httpclient
For the httpclient class, you can use the httppost, httpget, and httpresponse classes for network connection.
Use webview
The Android mobile phone has a built-in high-performance WebKit kernel browser, which is encapsulated into a webview component in the SDK.
Http://developer.android.com/guide/tutorials/views/hello-webview.html:
1. XML definition of webview:
<WebviewAndroid:ID="@ + ID/webview"Android:Layout_width="Fill_parent"Android:Layout_height="Fill_parent"/>
2. permission settings in the manifest file:
<Uses-Permission Android: Name = "android. Permission. Internet"/>
3. To support javascript:
Webview. getsettings (). setjavascriptenabled (true );
4. If you need to display a webpage in webview, instead of browsing in a built-in browser, you need mwebview. setwebviewclient and override the shouldoverrideurlloading method.
5. if you do not do any processing, when displaying your Brower UI, click the system "back" key, and the entire browser will be "back" to other activities as a whole, instead of back to the browser history page. If you want to implement back in the History page, you need to process the back event in the current activity: mwebview. Goback ();
Webview; /** Called when the activity is first created .*/ @ Override Public Void Oncreate (bundle savedinstancestate ){ Super . Oncreate (savedinstancestate); setcontentview (R. layout. Main ); // Obtain the webview object Webview = (webview) findviewbyid (R. Id. webview ); // Enable Javascript Webview. getsettings (). setjavascriptenabled ( True ); // If you need to display a webpage in webview, instead of browsing in a built-in browser, // You need mwebview. setwebviewclient and rewrite it. // Shouldoverrideurlloading method. Webview. setwebviewclient ( New Webviewclientdemo ()); // Load the webpage Webview. loadurl (" Http://t.sina.cn/fesky ") ;}@ Override Public Boolean Onkeydown ( Int Keycode, keyevent event ){ // Press the back key to return to the history page. If (Keycode = keyevent. keycode_back) & webview. cangoback () {webview. Goback (); Return True ;} Return Super . Onkeydown (keycode, event );} Private Class Webviewclientdemo Extends Webviewclient {@ override// Display the page in webview instead of in the default browser Public Boolean Shouldoverrideurlloading (webview view, string URL) {view. loadurl (URL ); Return True ;}}
The above Code uses the loadurl method to load webpages. You can also use the loaddata or loaddatawithbaseurl method to load webpages:
Webview. loaddata (HTML, "text/html", "UTF-8 ");
If HTML contains Chinese characters, webview. loaddata (urlencoder. encode (HTML, encoding), mimetype, encoding) is required );
You can use loadurl to display local images or webpages. However, the URL prefix is file: //, for example, "file: // android_asset/test.htm ".