In Android, there are several ways to implement network programming:
Create a URL and use Urlconnection/httpurlconnection
Using HttpClient
Using WebView
Create a URL and use Urlconnection/httpurlconnection
Java.net.* below provides the basic functionality to access the HTTP service. The basic operations that use this part of the interface mainly include:
Creating URLs and Urlconnection/httpurlconnection objects
1 Setting Connection Parameters
2 Connecting to the server
3 writing data to the server
4 reading data from the server
Source:
try {
Create a URL object
URL url = new URL ("Http://t.sina.cn/fesky");
Create a URL connection
URLConnection connection = Url.openconnection ();
For HTTP connections You can switch directly to HttpURLConnection,
This allows you to use some HTTP connection-specific methods, such as Setrequestmethod (), etc.
HttpURLConnection Connection
= (httpurlconnection) url.openconnection (proxy_yours);
Setting the parameter Www.jb51.net
Connection.setconnecttimeout (10000);
Connection.addrequestproperty ("User-agent", "j2me/midp2.0");
Connecting to a server
Connection.connect ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Using HttpClient
For the HttpClient class, you can use the HttpPost and HttpGet classes and HttpResponse for network connectivity.
Using WebView
A high-performance WebKit kernel browser is built into the Android phone, which is packaged as a WebView component in the SDK.
1. XML definition for WebView:
<webview
Android:id= "@+id/webview"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
/>
Settings for permissions in the 2.Manifest file:
<uses-permission android:name= "Android.permission.INTERNET"/>
3. If you want to support JavaScript:webview.getSettings (). Setjavascriptenabled (True);
4. If you need to display the Web page in WebView instead of browsing in the built-in browser, you need to mwebview.setwebviewclient and override the Shouldoverrideurlloading method.
5. If you do not do any processing, when you display your Brower UI, click the System "back" button, the entire browser as a whole "back" to other activity, rather than the hope of the browser history page back. If you want to implement back in the history page, you need to handle 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);
Get WebView Object
WebView = (webview) Findviewbyid (R.id.webview);
Enable JavaScript
Webview.getsettings (). Setjavascriptenabled (True);
If you need to display a Web page in WebView instead of browsing in a built-in browser,
You need to mwebview.setwebviewclient and rewrite
Shouldoverrideurlloading method.
Webview.setwebviewclient (New Webviewclientdemo ());
Loading Web pages
Webview.loadurl ("Http://t.sina.cn/fesky");
}
@Override
public boolean onKeyDown (int keycode, keyevent event) {
Press the Back button 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 the default browser
public boolean shouldoverrideurlloading (WebView view, String URL) {
View.loadurl (URL);
return true;
}
}
Webview.loaddata (HTML, "text/html", "utf-8");
If the HTML contains Chinese, you need Webview.loaddata (Urlencoder.encode (html,encoding), mimeType, encoding);
For a local picture or Web page display, you can use Loadurl, but the URL has an address prefix of file:///, such as "file:///android_asset/test.htm".
WebView using----Android network connection processing analysis