Android Network Connection processing learning notes

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 useURLConnection/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:

 
 
  1. Try {
  2. // Create a URL object
  3. URL url = new URL ("http://t.sina.cn/fesky ");
  4. // Create a URL Connection
  5. URLConnection connection = url. openConnection ();
  6. // You can directly convert an HTTP connection to HttpURLConnection,
  7. // You can use some HTTP connection methods, such as setRequestMethod ().
  8. // HttpURLConnection connection
  9. // = (HttpURLConnection) url. openConnection (Proxy_yours );
  10. // Set parameters
  11. Connection. setConnectTimeout (10000 );
  12. Connection. addRequestProperty ("User-Agent", "j2e-midp2.0 ");
  13. // Connect to the server
  14. Connection. connect ();
  15.  
  16. } Catch (IOException e ){
  17. // TODO Auto-generated catch block
  18. E. printStackTrace ();
  19. }

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:

 
 
  1. <WebView    
  2.         android:id="@+id/webview"   
  3.         android:layout_width="fill_parent"   
  4.         android:layout_height="fill_parent"   
  5.     />  

2. permission settings in the Manifest file:

 
 
  1. <uses-permission android:name="android.permission.INTERNET" /> 

3. To support JavaScript:

 
 
  1. 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 ();

 
 
  1. WebView webview;
  2. /** Called when the activity is first created .*/
  3. @ Override
  4. Public void onCreate (Bundle savedInstanceState ){
  5. Super. onCreate (savedInstanceState );
  6. SetContentView (R. layout. main );
  7. // Obtain the WebView object
  8. Webview = (WebView) findViewById (R. id. webview );
  9. // Enable JavaScript
  10. Webview. getSettings (). setJavaScriptEnabled (true );
  11. // If you need to display a webpage in WebView, instead of browsing in a built-in browser,
  12. // You need mWebView. setWebViewClient and rewrite it.
  13. // ShouldOverrideUrlLoading method.
  14. Webview. setWebViewClient (new WebViewClientDemo ());
  15. // Load the webpage
  16. Webview. loadUrl ("http://t.sina.cn/fesky ");
  17. }
  18. @ Override
  19. Public boolean onKeyDown (int keyCode, KeyEvent event ){
  20. // Press the BACK key to return to the history page.
  21. If (keyCode = KeyEvent. KEYCODE_BACK) & webview. canGoBack ()){
  22. Webview. goBack ();
  23. Return true;
  24. }
  25. Return super. onKeyDown (keyCode, event );
  26. }
  27. Private class WebViewClientDemo extends WebViewClient {
  28. @ Override
  29. // Display the page in WebView instead of in the default browser
  30. Public boolean shouldOverrideUrlLoading (WebView view, String url ){
  31. View. loadUrl (url );
  32. Return true;
  33. }
  34. }

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

Android layout attributes

Android smartphone operating system

Build an Android Application Development Environment

Looking at the future of smart terminals in the Android Application Field

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.