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
1. Set connection Parameters
2. Connect to the server
3. Write Data to the server
4. read data from the server
Source code:
Try {
// Create a URL object
URL url = new URL ("http://t.sina.cn/fesky ");
// Create a URL Connection
URLConnection 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 the parameter www.2cto.com
Connection. setConnectTimeout (10000 );
Connection. addRequestProperty ("User-Agent", "j2e-midp2.0 ");
// Connect to the server
Connection. connect ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. 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.
1. XML definition of webview:
<WebView
Android: 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 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;
}
}
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 ".