【Android進階】使用HttpURLConnection實現網頁源碼的下載

來源:互聯網
上載者:User

標籤:url   源碼   http   

上一篇文章主要介紹的圖片檔案的下載與顯示,這一篇文章主要介紹如何根據網頁的地址,擷取網頁原始碼的擷取

其實,網站原始碼的擷取比圖片的下載與顯示更加簡單,只需要對之前的代碼稍作修改即可


public class OtherActivity extends Activity {private TextView tv;private static final int LOAD_SUCCESS = 1;private static final int LOAD_ERROR = -1;private Handler handler = new Handler() {public void handleMessage(Message msg) {switch (msg.what) {case LOAD_SUCCESS:tv.setText((String) msg.obj);break;case LOAD_ERROR:Toast.makeText(getApplicationContext(), "載入失敗", 0).show();break;}};};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv = (TextView) findViewById(R.id.tv);}// Button的點擊事件public void show(View view) {new Thread(new Runnable() {public void run() {getHttp();}}).start();}// 下載圖片的主方法private void getHttp() {URL url = null;InputStream is = null;ByteArrayOutputStream byteArrayOutputStream = null;try {// 構建圖片的url地址url = new URL("http://www.baidu.com");// 開啟串連HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 設定逾時的時間,5000毫秒即5秒conn.setConnectTimeout(5000);// 設定擷取圖片的方式為GETconn.setRequestMethod("GET");if (conn.getResponseCode() == 200) {is = conn.getInputStream();byteArrayOutputStream = new ByteArrayOutputStream();int len = 0;byte[] buffer = new byte[1024];while ((len = is.read(buffer)) != -1) {byteArrayOutputStream.write(buffer, 0, len);}byteArrayOutputStream.flush();byte[] arr = byteArrayOutputStream.toByteArray();Message msg = handler.obtainMessage();msg.what = LOAD_SUCCESS;msg.obj = new String(arr);handler.sendMessage(msg);}} catch (Exception e) {handler.sendEmptyMessage(LOAD_ERROR);e.printStackTrace();} finally {try {if (is != null) {is.close();}if (byteArrayOutputStream != null) {byteArrayOutputStream.close();}} catch (Exception e) {handler.sendEmptyMessage(LOAD_ERROR);e.printStackTrace();}}}}

布局檔案

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_gravity="bottom"        android:onClick="show"        android:text="顯示" />    <TextView        android:id="@+id/tv"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>

許可權

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.