StreamTool.java:
package util;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
public class StreamTool {
/**
* 把一个inputstream里面的内容转化成一个byte[]
*/
public static byte[] getBytes(InputStream is) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
is.close();
bos.flush();
byte[] result = bos.toByteArray();
System.out.println(new String(result));
return result;
}
}
NetUtil.java:
package service;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import util.StreamTool;
import android.widget.Toast;
public class NetUtil {
public static String getHtml(String address) throws Exception {
URL url = new URL(address);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if (code == 200) {
InputStream is = conn.getInputStream();
byte[] result = StreamTool.getBytes(is);
return new String(result);
} else {
throw new IllegalStateException("访问失败");
}
}
}
MainActivity.java:
package com.example.httpviewer;
import service.NetUtil;
import android.app.Activity;
import android.os.Bundle;
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 {
private EditText mEtAddress;
private Button mBtView;
private TextView mTvView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBtView = (Button) this.findViewById(R.id.chakan);
mEtAddress = (EditText) this.findViewById(R.id.editText1);
mTvView = (TextView) this.findViewById(R.id.TextView1);
mBtView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.chakan:
// 按钮对应的点击事件
String address = mEtAddress.getText().toString().trim();
if ("".equals(address)) {
Toast.makeText(this, "地址不能为空", Toast.LENGTH_SHORT).show();
return;
}
try {
String html = NetUtil.getHtml(address);
mTvView.setText(html);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "获取数据失败", 0).show();
}
break;
}
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/input_address" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="2"
android:text="http://www.baidu.com" />
<Button
android:id="@+id/chakan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/go" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/TextView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center" />
</ScrollView>
</LinearLayout>