android練習一之獲取網頁源碼

//activity_main.xml
<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"
    tools:context="com.vic.getwebresource.MainActivity" >
     <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:text="http://">
        <requestFocus />
     </EditText>
    <Button
        android:id="@+id/get"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="獲取" />
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:id="@+id/textView1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:layout_below="@+id/editText1"
         android:padding="5dp"
         android:text="先輸入網址,網址加www和不加有區別"
         android:textStyle="bold"
            />
 </ScrollView>
</LinearLayout>
//MainActivity.java
package com.vic.getwebresource;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
 * @author bpm-dev
 *
 */
public class MainActivity extends Activity implements OnClickListener{
 private EditText inUrl; //輸入的鏈接
 private TextView result; //獲取的結果源碼
 private Button get;//獲取按鈕
 private static final int MSG_SUCCESS = 0;
 private static final int MSG_FAILURE = 1;
 private Handler mHandler = null;
 private Thread httpClientThread;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  init();
 }
 
 /**
  * 實例化控件
  */
 private void init() {
  inUrl = (EditText) findViewById(R.id.editText1);
  result = (TextView) findViewById(R.id.textView1);
  get = (Button) findViewById(R.id.get);
  //第三種方式實現監聽-接口實現
  get.setOnClickListener(this);
  mHandler = new Handler(){
   @Override
   public void handleMessage(Message msg){
    switch (msg.what) {
    case MSG_SUCCESS:
     Toast.makeText(getApplicationContext(), "連接成功...", Toast.LENGTH_SHORT).show();
     //連接成功,將結果顯示
     result.setText((String)msg.obj);
     break;
    case MSG_FAILURE:
     Toast.makeText(getApplicationContext(), "連接失敗...", Toast.LENGTH_SHORT).show();
     break;
    default:
     break;
    }
   }
  };
 }
 /**
  * 接口實現監聽事件
  * 獲取按鈕點擊事件
  */
 @Override
 public void onClick(View v) {
  if (v.getId() == R.id.get) {
   Toast.makeText(MainActivity.this, "正在獲取...", 0).show();
   //進行連接
   httpClientThread = new Thread(httpClientRunnable);
   httpClientThread.start();
  }
 }
  
  Runnable httpClientRunnable = new Runnable() {
   //將獲取到的源文件放在TextView上
   @Override
   public void run() {
    httpClientWebData(inUrl.getText().toString());
   }
  };
   /**
    * Get連接
    * @param Url 輸入的鏈接
    */
   protected void httpClientWebData(String Url) {
    //獲取網頁源文件
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(Url);
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    try {
     String content = httpClient.execute(httpGet,responseHandler);
     mHandler.obtainMessage(MSG_SUCCESS,content).sendToTarget();
    } catch (ClientProtocolException e) {
     e.printStackTrace();
    }catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
//AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.vic.getwebresource"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="16" />
<!-- 連網權限 -->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章