分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

Html代码查看器

发布时间:2023-09-06 01:09责任编辑:赖小花关键词:暂无标签

Html代码查看器

效果:

分析:

1、连接网络需要权限

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

2、要把默认协议和默认端口http和80写上

3、把访问网络的代码放在AsyncTask里面

4、得到内容类型两种方法

  String contentType=conn.getHeaderField("Content-Type");

  conn.getContentType

5、分割函数

  String encoding=contentType.split("=")[0];

  以等号为分割,取第0段

代码:

 1 package fry; 2 ?3 import java.io.InputStreamReader; 4 import java.net.HttpURLConnection; 5 import java.net.MalformedURLException; 6 import java.net.URL; 7 ?8 import com.example.watchCode.R; 9 10 import android.app.Activity;11 import android.os.AsyncTask;12 import android.os.Bundle;13 import android.util.Log;14 import android.view.View;15 import android.widget.Button;16 import android.widget.EditText;17 18 public class Activity01 extends Activity{19 ????private Button btn_checked;20 ????private EditText et_webSite;21 ????private EditText et_content;22 ????@Override23 ????protected void onCreate(Bundle savedInstanceState) {24 ????????// TODO Auto-generated method stub25 ????????super.onCreate(savedInstanceState);26 ????????setContentView(R.layout.activity01);27 ????????28 ????????btn_checked=(Button) findViewById(R.id.btn_checked);29 ????????et_webSite=(EditText) findViewById(R.id.et_webSite);30 ????????et_content=(EditText) findViewById(R.id.et_content);31 ????????32 ????}33 ????34 ????35 ????public void onClick(View view){36 ????????//获取编辑框输入的网址37 ????????String urlStr=et_webSite.getText().toString();38 ????????new AsyncTask<String,Void,String>(){39 ????????????40 ????????????@Override41 ????????????protected String doInBackground(String... arg0) {42 ????????????????//字符串缓冲区43 ????????????????StringBuffer buffer= new StringBuffer();44 ????????????????try {45 ????????????????????String s=arg0[0]+":80";46 ????????????????????//封装URL对象47 ????????????????????URL url=new URL(s);48 ????????????????????//获取http连接对象49 ????????????????????HttpURLConnection conn =(HttpURLConnection) url.openConnection();50 ????????????????????conn.setRequestMethod("GET");51 ????????????????????conn.setConnectTimeout(5000);52 ????????????????????//获取状态码53 ????????????????????int code=conn.getResponseCode();54 ????????????????????Log.d("fry", "获取到状态码:"+code);55 ????????????????????if(code==200){//请求成功56 ????????????????????????String contentType=conn.getHeaderField("Content-Type");57 ????????????????????????String encoding=contentType.split("=")[0];58 ????????????????????????Log.d("fry", encoding);59 ????????????????????????//获取响应消息的实体内容60 ????????????????????????InputStreamReader reader=new InputStreamReader(conn.getInputStream(),"UTF-8");61 ????????????????????????char[] charArr=new char[1024*8];62 ????????????????????????int len=0;63 ????????????????????????while((len=reader.read(charArr))!=-1){64 ????????????????????????????//字符数组转字符串65 ????????????????????????????String str=new String(charArr,0,len);66 ????????????????????????????//在结尾追加字符串67 ????????????????????????????buffer.append(str);68 ????????????????????????}69 ????????????????????}70 ????????????????????71 ????????????????} catch (Exception e) {72 ????????????????????e.printStackTrace();73 ????????????????}74 ????????????????75 ????????????????return buffer.toString();76 ????????????}77 ????????????protected void onPostExecute(String result) {78 ????????????????//设置字符串到编辑框79 ????????????????et_content.setText(result);80 ????????????????81 ????????????};82 ????????????83 ????????}.execute("http://"+urlStr);84 ????}85 ????86 }

本来getResponseCode做了连接、获取状态码,返回状态码三件事。

现在conn.connect()把连接这件事做了。

5000ms没有成功连接就抛出异常

Html代码查看器

原文地址:http://www.cnblogs.com/Renyi-Fan/p/7487883.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved