<!--访问网络的权限--> <uses-permission android:name="android.permission.INTERNET"/>
/**
* 获取网页HTML源代码
* @param path 网页路径
*/
public static String getHtml(String path) throws Exception {
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode()==200){
InputStream inStream=conn.getInputStream();
byte[] data=read(inStream);
String html=new String(data,"UTF-8");
return html;
}
return null;
}
/**
* 读取流中的数据
*/
public static byte[] read(InputStream inputStream) throws IOException {
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
byte[] b=new byte[1024];
int len=0;
while((len=inputStream.read(b))!=-1){
outputStream.write(b);
}
inputStream.close();
return outputStream.toByteArray();
}
public class HtmlViewActivity extends Activity {
private EditText pathText;
private TextView codeView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pathText=(EditText) findViewById(R.id.pagepath);//网页路径
codeView=(TextView)findViewById(R.id.codeView);//显示获得的源码
Button button=(Button) findViewById(R.id.button);//查看按钮
button.setOnClickListener(new ButtonClickListener());//按钮事件
}
/**
* 查看按钮处理事件
*/
private final class ButtonClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
String path=pathText.getText().toString();
try {
String html=PageService.getHtml(path);
codeView.setText(html);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), R.string.error, 1);
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有