Android项目获取网页信息的三种方法

1.调用jsoup.connect直接获取html代码(需要下载jsoup包)

具体操作可以参考我的另一篇文章eclipse创建Android项目调用jsoup获取网页信息

2.调用url.openStream获取网页html代码

		URL url = new URL("网页地址");
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				url.openStream(), "UTF-8"));
		BufferedWriter writer = new BufferedWriter(write);
		StringBuffer html = new StringBuffer(); // 用来保存读取页面的数据.
		String line;
		while ((line = reader.readLine()) != null) {
			System.out.println(line);//可以逐行分析,获取信息
			html.append(line);
		}
		System.out.println(html);
		//Document doc = Jsoup.parse(html.toString());//也可以与jsoup结合使用,分析整体html代码
		reader.close();

3.使用get方法提交表单获取网页代码

public static String sendPostByJson(String url,String para) {//para属于setRequestMethod为post时使用,使用get时可以忽略
		HttpURLConnection httpConn = null;
		PrintWriter out = null;
		BufferedReader in = null;
		StringBuffer result = new StringBuffer("");
		try {
			URL realUrl = new URL(url);
			// 打开和URL之间的连接
			httpConn = (HttpURLConnection) realUrl.openConnection();
			//httpConn.setRequestMethod("POST");
			 httpConn.setRequestMethod("GET");
			httpConn.setConnectTimeout(Integer.valueOf(30000));
			httpConn.setReadTimeout(Integer.valueOf(30000));
			//注意:下面四个setRequestProperty具体内容第一个参数不修改,第二个参数根据网页具体内容修改,查看方式参考下面的图片。
			httpConn.setRequestProperty("Accept-Charset", "UTF-8");
			httpConn.setRequestProperty("Connection", "keep-alive");
			//httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
			httpConn.setRequestProperty("Content-Type", "text/javascript");
			httpConn.setRequestProperty("Accept",
			"*/*");
//"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");

			// 发送POST必须设置下面两行
			httpConn.setDoInput(true);
			httpConn.setDoOutput(true);
			// 获取HttpURLConnection对象对应的输出流,此处应设置默认字符集
			out = new PrintWriter(new OutputStreamWriter(
					httpConn.getOutputStream(), "UTF-8"));
			// 发送请求参数
			out.print(para);
			// flush输出流的缓冲
			out.flush();
			// 定义BufferedReader输入流来读取URL的响应
			in = new BufferedReader(new InputStreamReader(
					httpConn.getInputStream(), "UTF-8"));
			String line = "";
			while ((line = in.readLine()) != null) {
				result.append(line);//可以逐行分析
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
				if (httpConn != null) {
					httpConn.disconnect();
				}
			} catch (IOException e) {
				e.printStackTrace();
				e.printStackTrace();
			}
		}
		return result.toString();//也可以整个html分析,参考jsoup

	}

进入想要获取信息的网页,点击F12进入开发者模式,选择网络,刷新页面,点击标头,参考对应字条,没有的字条就不修改。在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值