Android Network Connection Processing

Source: Internet
Author: User

From: <a href = "http://www.cnblogs.com/feisky/archive/2010/01/13/1646919.html"> http://www.cnblogs.com/feisky/archive/2010/01/13/1646919.html </a>

 

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 URL object URL url = new URL ("http://t.sina.cn/fesky"); // create URL connection URLConnection connection = url. openConnection (); // for HTTP connections, you can directly convert them to HttpURLConnection. // you can use some HTTP connection-specific methods, such as setRequestMethod () // HttpURLConnection connection // = (HttpURLConnection) url. openConnection (Proxy_yours); // you can specify the connection parameter. setConnectTimeout (10000); connection. addRequestProperty ("User-Agent", "j2e-midp2.0"); // connect to the server connection. 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:

<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. * // @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // obtain the WebView object webview = (WebView) findViewById (R. id. webview); // enables JavaScriptwebview. getSettings (). setJavaScriptEnabled (true); // if you want to display a webpage in WebView, instead of browsing in a built-in browser, you need mWebView. setWebViewClient, and rewrite // shouldOverride UrlLoading method. Webview. setWebViewClient (new WebViewClientDemo (); // load the webview of the webpage. loadUrl ("http://t.sina.cn/fesky") ;}@ Overridepublic 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 public boolean shouldOverrideUrlLoading (WebView view, String url) {view in the WebView instead of the default browser. 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 ".

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.