android html解析

html解析

在这里插入图片描述

package com.example.wangyebiancheng2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {
 TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=findViewById(R.id.textView);
    }
    public void zhengzebiaodashi(View v){

        String a="<%@ page language=\"java\" contentType=\"text/html; charset=ISO-8859-1\"\n" +
                "    pageEncoding=\"ISO-8859-1\" import=\"java.sql.*,java.io.*\"%>\n" +
                "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n" +
                "<html>\n" +
                "<head>\n" +
                "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">\n" +
                "<title> title </title>\n" +
                "</head>\n" +
                "<body>" +
                "<form action=\"libailogintest.jsp\" method=\"post\">\n" +
                "username:<input type=\"text\" name=\"username\" value=\"\"/><br>\n" +
                "\n" +
                "<input type=\"checkbox\" >\n" +
                "<input type=\"button\" value=\"hello\" οnclick=\"javascript:alert('hello');\"/>\n" +
                "<input type=\"submit\" value=\"jianceyemian34\" />\n" +
                "</form>\n" +
                "<label>信息</label>\n" +
                "</body>\n" +
                "</html>";

        int size1 = a.indexOf("<label>")+7;
        int size2 = a.indexOf("</label>");
        String b=a.substring(size1,size2);
        textView.setText(b);

    }
}

在线获取html并解析源码

package com.example.wangluobiancheng;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView textView_HTTP;
    Button button_http;
    String a;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView_HTTP = (TextView) findViewById(R.id.TextView_HTTP);
        //Button button_http = (Button) findViewById(R.id.Button_HTTP);
        button_http = (Button) findViewById(R.id.Button_HTTP);
        button_http.setOnClickListener(new OnClickListener() {
            //给button_http按钮设置监听器
            public void onClick(View v) {
                new Thread(new Runnable() {  // 创建一个新线程,用于从网络上获取文件
                    public void run() {
                        send();     //调用send()方法,用于发送用户名、密码到Web服务器
                        //Message m = handler.obtainMessage();    // 获取一个Message
                        //handler.sendMessage(m);      // 发送消息
                    }
                }).start();
            }

        });

    }


    public void send() {
        //String httpUrl = "http://192.168.42.25:8080/***/login.jsp";
        String httpUrl = "http://www.baidu.com";

        String resultData = "";//定义一个resultData用于存储获得的数据
        URL url = null; //定义URL对象
        try {
            url = new URL(httpUrl); //构造一个URL对象时需要使用异常处理
            textView_HTTP.setText("url = new URL(httpUrl);"+url.toString());
        } catch (Exception e) {
            //MalformedURLException e
            textView_HTTP.setText("Sorry,error");//获取到的数据为空时显示
            //System.out.println(e.getMessage());//打印出异常信息
        }

        if (url != null) {//如果URL不为空时
            try {
                //有关网络操作时,需要使用异常处理
                HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); //使用HttpURLConnection打开连接

                InputStreamReader in = new InputStreamReader(urlConn.getInputStream());//得到读取的内容
                BufferedReader buffer = new BufferedReader(in);//为输出创建BufferedReader
                String inputLine = null;
                while (((inputLine = buffer.readLine()) != null)) {
                    // 读取获得的数据
                    resultData += inputLine + "\n"; // 加上"\n"实现换行
                }

                in.close();//关闭InputStreamReader
                urlConn.disconnect(); //关闭HTTP连接
                if (resultData != null) {//如果获取到的数据不为空
                    textView_HTTP.setText(resultData);
                    a=resultData;
                } else {
                    textView_HTTP.setText("Sorry,the content is null");//获取到的数据为空时显示
                }

            } catch (IOException e) {
                textView_HTTP.setText(e.getMessage());
                textView_HTTP.setText(resultData);
                //出现异常时,打印异常信息
            }

        } else {
            textView_HTTP.setText("url is null"); //当url为空时输出
        }
    }
    public void mesg(View v) {
    int size1 = a.indexOf("<title>")+7;
    int size2 = a.indexOf("</title>");
    String b=a.substring(size1,size2);
        textView_HTTP.setText("error"+size1+"d"+size2+b);
       // textView_HTTP.setText(b);
        }
}
        /*extends AppCompatActivity {

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

如果大量的信息读取需要:android网络数据库编程

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值