Android Network Connection Processing

Source: Internet
Author: User

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 ".

 

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.