zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

[android] 网络html查看器详解手机开发

2023-06-13 09:20:13 时间

访问一个网页的请求实际上就是一个GET请求,应用的布局没啥好说的,线性布局,定义好控件,在activity代码里面先获取到控件,获取到EditText控件的网络路径,开启get请求

 

开启一个新的线程,new Thread(){}.start()

获取Url对象,new出来,参数:path是String的url,内部类访问外部类的变量,应该顶一次final的

主线程中定义成员属性Handler对象,为了方便直接重写handleMessage()方法,回调过来的参数是Message对象,获取Message对象的what属性和obj属性,

New出Url之后,会有异常产生,捕获异常,

调用Url对象的openConnection()方法,得到HttpUrlConnection对象,这里需要强转

调用HttpUrlConnection对象的setRequestMethod()方法

调用HttpUrlConnection对象的setConnectTimeout()方法

调用HttpUrlConnection对象的setRequestProperty()方法

调用HttpUrlConnection对象的getResponseCode()方法,得到响应码,进行判断

调用HttpUrlConnection对象的getInputStream()方法,得到InputStream对象

 

把流的数据转换成文本,是一个非常常用的操作,新建一个包utils,放工具类

新建一个类StreamTools,里面定义一个静态方法readInputStream()

获取ByteArrayOutputStream对象,通过new一个字节数组输出流

定义一个int的len长度是0

定义一个byte[]的数组,通过new byte[1024]定义一个1024字节的数组

定义一个while循环,条件是调用InputStream对象的read(buffer)方法,参数:上面定义的byte[]数组,把数据读入到byte[]数组里面,返回一个读取的长度,如果长度等于-1那就是读到末尾了,因此这个进行循环判断

调用ByteArrayOutputStream对象的write(buffer,0,len)方法,读取字节数组,从0开始到len长度

循环完成之后,关闭输入流,调用ByteArrayOutputStream对象的toByteArray()得到一个字节数组,return出来用new String()包装一下

 

调用Handler对象的sendMessage()方法发送数据

 

当设置请求参数的时候,不能多加冒号,否则容易出错

package com.tsh.hrmlviewer; 

import java.io.InputStream; 

import java.net.HttpURLConnection; 

import java.net.MalformedURLException; 

import java.net.URL; 

import com.tsh.hrmlviewer.utils.StreamTools; 

import android.app.Activity; 

import android.os.Bundle; 

import android.os.Handler; 

import android.os.Message; 

import android.text.TextUtils; 

import android.view.View; 

import android.widget.EditText; 

import android.widget.TextView; 

import android.widget.Toast; 

public class MainActivity extends Activity { 

 protected static final int SUCCESS = 1; 

 protected static final int ERROR = 2; 

 private EditText et_path; 

 private TextView tv_show; 

 // 消息处理器 

 public Handler handler = new Handler() { 

 public void handleMessage(Message msg) { 

 switch (msg.what) { 

 case SUCCESS: 

 String text = (String) msg.obj; 

 tv_show.setText(text); 

 break; 

 case ERROR: 

 Toast.makeText(MainActivity.this, "获取数据失败", 0).show(); 

 break; 

 @Override 

 protected void onCreate(Bundle savedInstanceState) { 

 super.onCreate(savedInstanceState); 

 setContentView(R.layout.activity_main); 

 et_path = (EditText) findViewById(R.id.et_path); 

 tv_show = (TextView) findViewById(R.id.tv_show); 

 // 查看 

 public void click(View v) { 

 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); 

 int code = conn.getResponseCode(); 

 if (code == 200) { 

 InputStream is = conn.getInputStream(); 

 String res = StreamTools.readInputStream(is); 

 Message msg = new Message(); 

 msg.what = SUCCESS; 

 msg.obj = res; 

 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(); 

}

工具类:

package com.tsh.hrmlviewer.utils; 

import java.io.ByteArrayOutputStream; 

import java.io.InputStream; 

public class StreamTools { 

 /** 

 * 读取输入流 

 * @param is 

 * @return 

 public static String readInputStream(InputStream is){ 

 ByteArrayOutputStream baos=new ByteArrayOutputStream(); 

 int len=0; 

 byte[] buffer=new byte[1024]; 

 try { 

 while((len=is.read(buffer))!=-1){ 

 baos.write(buffer,0,len); 

 is.close(); 

 byte[] res=baos.toByteArray(); 

 return new String(res); 

 } catch (Exception e) { 

 e.printStackTrace(); 

 return null; 

}

 [android] 网络html查看器详解手机开发

5481.html

app程序应用开发手机开发无线开发移动端开发