没有合适的资源?快使用搜索试试~ 我知道了~
Android通过访问网页查看网页源码 1.添加网络权限 <!--访问网络的权限--> <uses android:name=android.permission.INTERNET/> 2.获取网络中网页的数据 /** * 获取网页HTML源代码 * @param path 网页路径 */ public static String getHtml(String path) throws Exception { URL url=new URL(path); HttpURLConnection conn=(HttpURLConn
资源详情
资源评论
资源推荐
Android通过访问网页查看网页源码实例详解通过访问网页查看网页源码实例详解
Android通过访问网页查看网页源码通过访问网页查看网页源码
1.添加网络权限添加网络权限
<!--访问网络的权限-->
<uses-permission android:name="android.permission.INTERNET"/>
2.获取网络中网页的数据获取网络中网页的数据
/**
* 获取网页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();
}
3.处理查看网页源码的控制处理查看网页源码的控制
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) {
weixin_38599430
- 粉丝: 0
- 资源: 887
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0