您的位置:首页 > Web前端 > HTML

查看htmlView

2016-05-23 23:42 477 查看
1、视图

<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="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入内容" />
<Button
android:onClick="viewSource"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="查看内容"
/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tv_content"
/>
</ScrollView>

</LinearLayout>


2、权限

<uses-permission android:name="android.permission.INTERNET"/>

3、MainActivity

package com.example.htmlview;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.example.htmlview.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 {

private static final int VIEW_UI = 1;
private static final int ERROR = 2;
private TextView tv_content;
private EditText et_path;
private Handler handler = new Handler(){

@Override
public void handleMessage(android.os.Message msg) {
// TODO Auto-generated method stub
switch(msg.what){
case ERROR:
Toast.makeText(MainActivity.this, "抛出异常", 0).show();
break;
case VIEW_UI:
String str = (String)msg.obj;
tv_content.setText(str);
break;
}
}

};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tv_content = (TextView)findViewById(R.id.tv_content);
et_path = (EditText)findViewById(R.id.et_path);
}

public void viewSource(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);
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
int code = conn.getResponseCode();
if(code == 200){
InputStream is = conn.getInputStream();
String result = StreamTools.readInputStream(is);
if(result != null){
Message msg = new Message();
msg.what = VIEW_UI;
msg.obj = result;
handler.sendMessage(msg);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Message msg = new Message();
msg.what = ERROR;
handler.sendMessage(msg);

}
};
}.start();
}
}
}


4、辅助类

package com.example.htmlview.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class StreamTools {

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);
}
baos.close();
is.close();
byte[] result = baos.toByteArray();
return new String(result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: