在Android中,可以有多种方式来实现网络编程:

创建URL,并使用URLConnection/HttpURLConnection 

使用HttpClient
 
使用WebView

URLConnection/HttpURLConnection

java.net.*下面提供了访问 HTTP 服务的基本功能。使用这部分接口的基本操作主要包括:

创建 URL 以及 URLConnection / HttpURLConnection 对象

1 设置连接参数
 
2 连接到服务器

3 向服务器写数据

4 从服务器读取数据

源码:

1. try {  
2.         // 创建URL对象  
3.         URL url = new URL("http://t.sina.cn/fesky");  
4.         // 创建URL连接  
5.         URLConnection connection = url.openConnection();  
6.         // 对于 HTTP 连接可以直接转换成 HttpURLConnection,  
7.         // 这样就可以使用一些 HTTP 连接特定的方法,如 setRequestMethod() 等  
8.         // HttpURLConnection connection  
9.         // =(HttpURLConnection)url.openConnection(Proxy_yours);  
10.         // 设置参数  
11.         connection.setConnectTimeout(10000);  
12.         connection.addRequestProperty("User-Agent", "J2me/MIDP2.0");  
13.         // 连接服务器  
14.         connection.connect();  
15.  
16.     } catch (IOException e) {  
17.         // TODO Auto-generated catch block  
18.         e.printStackTrace();  
19.     }


使用HttpClient
对于HttpClient类,可以使用HttpPost和HttpGet类以及HttpResponse来进行网络连接。

Android网络连接处理_android

 

使用WebView

Android手机中内置了一款高性能webkit内核浏览器,在SDK中封装成了WebView组件。

http://developer.android.com/guide/tutorials/views/hello-webview.html

提供了一个简单的例子:

1. webview的XML定义:

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

2.Manifest文件中权限的设定:



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


3.如果想要支持JavaScript:



1. webview.getSettings().setJavaScriptEnabled(true);


4.如果需要在WebView中显示网页,而不是在内置浏览器中浏览,则需要mWebView.setWebViewClient,并重写shouldOverrideUrlLoading方法。

5.如果不做任何处理,在显示你的Brower UI时,点击系统“Back”键,整个Browser会作为一个整体“Back"到其他Activity中,而不是希望的在Browser的历史页面中Back。如果希望实现在历史页面中Back,需要在当前Activity中处理Back事件: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.         // 获取WebView对象  
8.         webview = (WebView) findViewById(R.id.webview);   
9.         // 使能JavaScript  
10.         webview.getSettings().setJavaScriptEnabled(true);   
11.         // 如果需要在WebView中显示网页,而不是在内置浏览器中浏览,  
12.         // 则需要mWebView.setWebViewClient,并重写  
13.         // shouldOverrideUrlLoading方法。  
14.         webview.setWebViewClient(new WebViewClientDemo());  
15.         // 加载网页  
16.         webview.loadUrl("http://t.sina.cn/fesky");    
17.     }  
18.     @Override  
19.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
20.         // 按下BACK键回到历史页面中  
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.         // 在WebView中而不是默认浏览器中显示页面  
30.         public boolean shouldOverrideUrlLoading(WebView view, String url) {   
31.             view.loadUrl(url);   
32.             return true;   
33.         }   
34.     } 
35. webview.loadData(html, “text/html”, "utf-8”);


如果html中包含中文,则需要webview.loadData(URLEncoder.encode(html,encoding), mimeType, encoding);
对于本地图片或网页的显示,可以使用loadUrl,不过Url的地址前缀为file:///,如"file:///android_asset/test.htm”。