//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>
转载于:https://blog.51cto.com/xiaoshitou/1603437