查看网络html代码
<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_url"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="http://www.baidu.com/" />
<Button
android:onClick="btn_click"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#6E8B3D"
android:text="查看"
/>
<TextView
android:id="@+id/tv_code"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
package com.example.netcodeview;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.example.utils.StreamTools;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
public static final int ERROR = 1;
protected static final int SHOWTEXT = 2;
private TextView tv_content;
private EditText et_path;
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case SHOWTEXT:
String content = (String) msg.obj;
tv_content.setText(content);
break;
case ERROR:
Toast.makeText(MainActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_path = (EditText) findViewById(R.id.et_url);
tv_content = (TextView) findViewById(R.id.tv_code);
}
public void btn_click(View view) {
// btn的点击事件处理
final String url_path = et_path.getText().toString().trim();
if(TextUtils.isEmpty(url_path)){
Toast.makeText(MainActivity.this, "路径不能为空", Toast.LENGTH_SHORT).show();
}else{
//连接网络是一个耗时的任务需要开启一个子线程
new Thread(){
public void run() {
try {
URL url = new URL(url_path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
//conn.setRequestProperty(field, newValue);
int code = conn.getResponseCode();
if (code==200) {
//把流转换成文本是一个非常常用的任务所以可以单独抽出一个类来处理。
InputStream is = conn.getInputStream();
String result = StreamTools.readInputStream(is);
//tv_content.setText(result); 这是个更新UI的操作 要在主线程当中进行
//所以要用到消息管理器Handler 向主线程发送消息
Message msg = new Message();
msg.what = SHOWTEXT;
msg.obj = result;
handler.sendMessage(msg);
} else {
Message msg = new Message();
msg.what = ERROR;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
Message msg = new Message();
msg.what = ERROR;
handler.sendMessage(msg);
}
};
}.start();
}
}
@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;
}
}
从网络得到的输入流的转换
public class StreamTools {
/**
* 把输入流里面的内容转换为字符串
* @param is
* @return
*/
public static String readInputStream(InputStream is){
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
byte[] buffer = new byte[1024];
while((len=is.read(buffer))!=-1){
baos.write(buffer, 0, len);
}
is.close();
baos.close();
byte[] result=baos.toByteArray();
//解析result里面的字符串 查看里面文档的编码方式是哪一种
String temp = new String(result);
if(temp.contains("utf-8")){
return temp;
}else if(temp.contains("gb2312")){
return new String(result,"gb2312");
}
return temp;
} catch (Exception e) {
e.printStackTrace();
return "网络访问异常";
}
}
}
最后添加访问网络的权限