1.在mainfest文件中配置网络权限:
<uses-permission android:name="android.permission.INTERNET"/>
2.利用Handler 的消息机制在主线程中更新UI
package com.example.html;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.example.html.utils.StreamUtils;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
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;
public class MainActivity extends Activity implements OnClickListener {
protected static final int SHOW_TEXT = 1;
protected static final int ERROR = 2;
private Button button;
private EditText et_path;
private TextView tv_content;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case ERROR:
Toast.makeText(MainActivity.this, "连接失败", 0).show();
case SHOW_TEXT:
//如果消息类型为显示文本,取出消息对象的字符串,显示在UI界面
String text = (String) msg.obj;
tv_content.setText(text);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
et_path = (EditText) findViewById(R.id.et_path);
tv_content = (TextView) findViewById(R.id.tv_content);
button.setOnClickListener(this);
}
@Override
public void onClick(View view) {
final String path = et_path.getText().toString().trim();
//输入的文本路径是不是为空
if (TextUtils.isEmpty(path)) {
Toast.makeText(this, "路径不能为空", 0).show();
} else {
new Thread() {
public void run() {
try {
//新url的连接地址
URL url = new URL(path);
//建立http连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//请求方式为GET
conn.setRequestMethod("GET");
//设置连接时间为 5 秒
conn.setConnectTimeout(5000);
//连接成功服务器返回请求码
int code = conn.getResponseCode();
//若为200请求成功
if (code == 200) {
//取得读取流
InputStream is = conn.getInputStream();
//StreamUtils.readSteramUtils(is)为自定义方法,把流里面的数据转为字符串
String result = StreamUtils.readSteramUtils(is);
//利用消息机制把字符串result发送给handlerMessage(Message msg)方法处理
Message msg = new Message();
//发送消息类型为SHOW_TEXT
msg.what = SHOW_TEXT;
msg.obj = result;
handler.sendMessage(msg);
}
} catch (Exception e) {
//如果产生异常,利用消息机制发送给handlerMessage(Message msg)方法处理,消息类型为ERROR
Message msg = new Message();
msg.what = ERROR;
handler.sendMessage(msg);
e.printStackTrace();
}
}
}.start();
}
}
}
这个类的作用为将 HttpURLConnection 的 conn.getInputStream() 流里面内容,将里面的内容转为字符串并返回
package com.example.html.utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
public class StreamUtils {
public static String readSteramUtils(InputStream is) {
try {
//字节数组输入流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
byte [] buffer = new byte[1024];
while ((len = is.read(buffer)) != -1) {
baos.write(buffer);
}
is.close();
baos.close();
return new String(baos.toString());
} catch (Exception e) {
e.printStackTrace();
return "读取数据失败";
}
}
}
全部代码:
package com.example.html;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.example.html.utils.StreamUtils;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
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;
public class MainActivity extends Activity implements OnClickListener {
protected static final int SHOW_TEXT = 1;
protected static final int ERROR = 2;
private Button button;
private EditText et_path;
private TextView tv_content;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case ERROR:
Toast.makeText(MainActivity.this, "连接失败", 0).show();
case SHOW_TEXT:
String text = (String) msg.obj;
tv_content.setText(text);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
et_path = (EditText) findViewById(R.id.et_path);
tv_content = (TextView) findViewById(R.id.tv_content);
button.setOnClickListener(this);
}
@Override
public void onClick(View view) {
final String path = et_path.getText().toString().trim();
if (TextUtils.isEmpty(path)) {
Toast.makeText(this, "路径不能为空", 0).show();
} else {
new Thread() {
public void run() {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
if (code == 200) {
InputStream is = conn.getInputStream();
String result = StreamUtils.readSteramUtils(is);
Message msg = new Message();
msg.what = SHOW_TEXT;
msg.obj = result;
handler.sendMessage(msg);
}
} catch (Exception e) {
Message msg = new Message();
msg.what = ERROR;
handler.sendMessage(msg);
e.printStackTrace();
}
}
}.start();
}
}
}
package com.example.html.utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
public class StreamUtils {
public static String readSteramUtils(InputStream is) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
byte [] buffer = new byte[1024];
while ((len = is.read(buffer)) != -1) {
baos.write(buffer);
}
is.close();
baos.close();
return new String(baos.toString());
} catch (Exception e) {
e.printStackTrace();
return "读取数据失败";
}
}
}
<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=".MainActivity" >
<EditText
android:id="@+id/et_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入网址路径" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="浏览" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>