1、给权限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.readhtm" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.readhtm.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> <uses-permission android:name="android.permission.INTERNET"> </uses-permission> </manifest>
2、画个按钮
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="122dp" android:text="Button" /> </RelativeLayout>
3、读取封装好的函数
package com.example.readhtm; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { /*****************************************************主要是点击事件*********************************************/ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Button button1 = (Button) findViewById(R.id.button1);//final获得句柄 获得句柄 final TextView textView = (TextView) findViewById(R.id.textView1); button1.setOnClickListener(new OnClickListener() { public void onClick(View v) { String a= getInput(); textView.setText(a); } }); } /*****************************************************读取网页源码的函数*********************************************/ String getInput(){ String str=""; try { URL url = new URL("http://www.baidu.com/"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setConnectTimeout(10000); conn.setRequestMethod("GET"); conn.setRequestProperty("accept", "*/*"); String location = conn.getRequestProperty("location"); int resCode = conn.getResponseCode(); conn.connect(); InputStream stream = conn.getInputStream(); byte[] data=new byte[102400]; int length=stream.read(data); str=new String(data,0,length); conn.disconnect(); System.out.println(str); stream.close(); } catch(Exception ee) { System.out.print("ee:"+ee.getMessage()); } return str; } /*****************************************************意义不大*********************************************/ @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }