編輯:關於Android編程
URL的openConnection方法將返回一個URLConnection,該對象表示應用程序和URL之間的通信連接。程序可以通過它的實例向該URL發送請求,讀取URL引用的資源。
下面通過一個簡單示例來演示:
Activity:
package com.home.urlconnection; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.webkit.WebView; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { private Button urlConnectionBtn; private Button httpUrlConnectionBtn; private Button httpClientBtn; private TextView showTextView; private WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { urlConnectionBtn = (Button) findViewById(R.id.test_url_main_btn_urlconnection); httpUrlConnectionBtn = (Button) findViewById(R.id.test_url_main_btn_httpurlconnection); httpClientBtn = (Button) findViewById(R.id.test_url_main_btn_httpclient); showTextView = (TextView) findViewById(R.id.test_url_main_tv_show); webView = (WebView) findViewById(R.id.test_url_main_wv); urlConnectionBtn.setOnClickListener(this); httpUrlConnectionBtn.setOnClickListener(this); httpClientBtn.setOnClickListener(this); } @Override public void onClick(View v) { if (v == urlConnectionBtn) { try { // 直接使用URLConnection對象進行連接 URL url = new URL("http://192.168.1.100:8080/myweb/hello.jsp"); // 得到URLConnection對象 URLConnection connection = url.openConnection(); InputStream is = connection.getInputStream(); byte[] bs = new byte[1024]; int len = 0; StringBuffer sb = new StringBuffer(); while ((len = is.read(bs)) != -1) { String str = new String(bs, 0, len); sb.append(str); } showTextView.setText(sb.toString()); } catch (Exception e) { e.printStackTrace(); } } if (v == httpUrlConnectionBtn) { // 直接使用HttpURLConnection對象進行連接 try { URL url = new URL( "http://192.168.1.100:8080/myweb/hello.jsp?username=abc"); // 得到HttpURLConnection對象 HttpURLConnection connection = (HttpURLConnection) url .openConnection(); // 設置為GET方式 connection.setRequestMethod("GET"); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { // 得到響應消息 String message = connection.getResponseMessage(); showTextView.setText(message); } } catch (Exception e) { e.printStackTrace(); } } if (v == httpClientBtn) { try { // 使用ApacheHttp客戶端進行連接(重要方法) HttpClient client = new DefaultHttpClient(); // 如果是Get提交則創建HttpGet對象,否則創建HttpPost對象 // POST提交的方式 HttpPost httpPost = new HttpPost( "http://192.168.1.100:8080/myweb/hello.jsp"); // 如果是Post提交可以將參數封裝到集合中傳遞 List dataList = new ArrayList(); dataList.add(new BasicNameValuePair("username", "abc")); dataList.add(new BasicNameValuePair("pwd", "123")); // UrlEncodedFormEntity用於將集合轉換為Entity對象 httpPost.setEntity(new UrlEncodedFormEntity(dataList)); // GET提交的方式 // HttpGet httpGet = new // HttpGet("http://192.168.1.100:8080/myweb/hello.jsp?username=abc&pwd=321"); // 獲取相應消息 HttpResponse httpResponse = client.execute(httpPost); // 獲取消息內容 HttpEntity entity = httpResponse.getEntity(); // 把消息對象直接轉換為字符串 String content = EntityUtils.toString(entity); // 顯示在TextView中 // showTextView.setText(content); // 通過webview來解析網頁 webView.loadDataWithBaseURL(null, content, "text/html", "utf-8", null); // 直接根據url來進行解析 // webView.loadUrl(url); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } } package com.home.urlconnection; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.webkit.WebView; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { private Button urlConnectionBtn; private Button httpUrlConnectionBtn; private Button httpClientBtn; private TextView showTextView; private WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { urlConnectionBtn = (Button) findViewById(R.id.test_url_main_btn_urlconnection); httpUrlConnectionBtn = (Button) findViewById(R.id.test_url_main_btn_httpurlconnection); httpClientBtn = (Button) findViewById(R.id.test_url_main_btn_httpclient); showTextView = (TextView) findViewById(R.id.test_url_main_tv_show); webView = (WebView) findViewById(R.id.test_url_main_wv); urlConnectionBtn.setOnClickListener(this); httpUrlConnectionBtn.setOnClickListener(this); httpClientBtn.setOnClickListener(this); } @Override public void onClick(View v) { if (v == urlConnectionBtn) { try { // 直接使用URLConnection對象進行連接 URL url = new URL("http://192.168.1.100:8080/myweb/hello.jsp"); // 得到URLConnection對象 URLConnection connection = url.openConnection(); InputStream is = connection.getInputStream(); byte[] bs = new byte[1024]; int len = 0; StringBuffer sb = new StringBuffer(); while ((len = is.read(bs)) != -1) { String str = new String(bs, 0, len); sb.append(str); } showTextView.setText(sb.toString()); } catch (Exception e) { e.printStackTrace(); } } if (v == httpUrlConnectionBtn) { // 直接使用HttpURLConnection對象進行連接 try { URL url = new URL( "http://192.168.1.100:8080/myweb/hello.jsp?username=abc"); // 得到HttpURLConnection對象 HttpURLConnection connection = (HttpURLConnection) url .openConnection(); // 設置為GET方式 connection.setRequestMethod("GET"); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { // 得到響應消息 String message = connection.getResponseMessage(); showTextView.setText(message); } } catch (Exception e) { e.printStackTrace(); } } if (v == httpClientBtn) { try { // 使用ApacheHttp客戶端進行連接(重要方法) HttpClient client = new DefaultHttpClient(); // 如果是Get提交則創建HttpGet對象,否則創建HttpPost對象 // POST提交的方式 HttpPost httpPost = new HttpPost( "http://192.168.1.100:8080/myweb/hello.jsp"); // 如果是Post提交可以將參數封裝到集合中傳遞 List dataList = new ArrayList(); dataList.add(new BasicNameValuePair("username", "abc")); dataList.add(new BasicNameValuePair("pwd", "123")); // UrlEncodedFormEntity用於將集合轉換為Entity對象 httpPost.setEntity(new UrlEncodedFormEntity(dataList)); // GET提交的方式 // HttpGet httpGet = new // HttpGet("http://192.168.1.100:8080/myweb/hello.jsp?username=abc&pwd=321"); // 獲取相應消息 HttpResponse httpResponse = client.execute(httpPost); // 獲取消息內容 HttpEntity entity = httpResponse.getEntity(); // 把消息對象直接轉換為字符串 String content = EntityUtils.toString(entity); // 顯示在TextView中 // showTextView.setText(content); // 通過webview來解析網頁 webView.loadDataWithBaseURL(null, content, "text/html", "utf-8", null); // 直接根據url來進行解析 // webView.loadUrl(url); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } }
上面使用到的url是部署在筆者本機的web應用,這裡不再給出,大家可以換成自己的web應用即可。
布局XML:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/test_url_main_btn_urlconnection" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="使用URLConnection連接" /> <Button android:id="@+id/test_url_main_btn_httpurlconnection" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="使用HttpURLConnection連接" /> <Button android:id="@+id/test_url_main_btn_httpclient" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="使用Apache客戶端連接" /> <TextView android:id="@+id/test_url_main_tv_show" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <WebView android:id="@+id/test_url_main_wv" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/test_url_main_btn_urlconnection" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="使用URLConnection連接" /> <Button android:id="@+id/test_url_main_btn_httpurlconnection" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="使用HttpURLConnection連接" /> <Button android:id="@+id/test_url_main_btn_httpclient" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="使用Apache客戶端連接" /> <TextView android:id="@+id/test_url_main_tv_show" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <WebView android:id="@+id/test_url_main_wv" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
權限:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
凱撒密碼1. 介紹凱撒密碼作為一種最為古老的對稱加密體制,在古羅馬的時候都已經很流行,他的基本思想是:通過把字母移動一定的位數來實現加密和解密。明文中的所有字母都在字母表
本文實例講述了android監聽返回按鈕事件的方法。分享給大家供大家參考。具體如下:用戶在點擊手機的返回按鈕時,默認是推出當前的activty,但是有時用戶不小心按到返回
一、前言前面介紹了Android中視頻直播中的一個重要類ByteBuffer,我們開始動手開發了,因為我們後續肯定是需要直播視頻功能,然後把視頻推流到服務端,本地在進行拉
好久沒寫android的博客,最近在做一個android的項目,裡面用到我們經常用的一個控件就是對話框,大家都知道android自帶的對話框是很丑的,android5.x