Http方式获取网络数据
通过以下代码可以根据网址获取网页的html数据,安卓中获取网络数据的时候会用到,而且会用Java中的sax方式解析获取到数据。(sax解析主要是解析xml)具体代码如下:
package com.wyl; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection; public class HttpDownloadTest {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
try {
//创建url对象
URL url = new URL("http://www.baidu.com");
//创建connection
URLConnection conn = url.openConnection();
//使用io流读取数据
InputStream ips = conn.getInputStream();
InputStreamReader reader = new InputStreamReader(ips);
BufferedReader br = new BufferedReader(reader);
// String s = br.readLine();
while(br.readLine()!=null){
//返回读取出来的结果
sb.append(br.readLine());
}
System.out.println(sb);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

实例2:
package wyl; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection; public class httpdownloadtest {
public static void main(String[] args) {
// getSDPath();
// System.out.println("路径:"+getSDPath());
System.out.println("xml:"+getXml("http://www.weibo.com"));
System.out.println("======");
System.out.println("xml:"+getXml("http://www.baidu.com"));
}
/**
* 获取sd卡路径
* @return
*/
// public static String getSDPath(){
// return Environment.getExternalStorageDirectory().getPath();
// }
/**
* 根据网址获取转换后的xml
* @param dizhi
* @return
*/
public static String getXml(String dizhi){
BufferedReader bufferedreader;
String xml = "";
StringBuffer sb = new StringBuffer();
try {
URL url = new URL(dizhi);
//1 获取一个连接
URLConnection conn = url.openConnection();
//2 把inputStream封装成为BufferedReader,
bufferedreader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while(bufferedreader.readLine()!=null){
//3 从缓冲流里读出数据
sb.append(bufferedreader.readLine());
}
//4 把StringBuffer转换为String
xml = sb.toString();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return xml;
} }
Http方式获取网络数据的更多相关文章
- [NSURLSession/Delegate]用Post方式获取网络数据并把数据显示到表格
#pragma mark 实现NSURLSessionDataDelegate代理 @interface ViewController ()<UITableViewDataSource,UITa ...
- [NSURLConnection]分别用Post和Get方式获取网络数据并把数据显示到表格
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate> { UIButton* getButton; ...
- 用 get 同步/异步 方式获取网络数据并输出
//同步请求 //创建NSString用来存储请求的网址 NSString* str=@"http://v.juhe.cn/weather/index?format=2&cityna ...
- android—获取网络数据
取网络数据主要靠发交易(或者说请求,接口等),而这些交易由java中的网络通信,HttpURLConnection和HttpClient实现,以下是具体例子. 大家都知道,网络通信,发送请求有两种 ...
- 使用promise方式来获取网络数据
获取网络数据 let data = []; new Promise(function(resolve,reject){ axios.post('api.php').then(function(resp ...
- Swift - 异步获取网络数据封装类
使用NSURLConnection.sendAsynchronousRequest()可以采用异步获取的方式取得数据.下面通过对数据获取类进行封装,演示如何进行数据请求与接收. 1,HttpContr ...
- Windows Phone 同步方式获取网络类型
原文:Windows Phone 同步方式获取网络类型 在Windows Phone 开发中有时候需要获取设备当前连接网络的类型,是Wifi,还是2G,3G,或者4G,SDK中提供获取网络类型的API ...
- Android简易实战教程--第四十七话《使用OKhttp回调方式获取网络信息》
在之前的小案例中写过一篇使用HttpUrlConnection获取网络数据的例子.在OKhttp盛行的时代,当然要学会怎么使用它,本篇就对其基本使用做一个介绍,然后再使用它的接口回调的方式获取相同的数 ...
- Swift实战-豆瓣电台(三)获取网络数据
观看地址:http://v.youku.com/v_show/id_XNzMwMzQxMzky.html 这节内容,我们先说了怎么将storyboard中的组件在类中进行绑定.然后写了一个类用来获取网 ...
随机推荐
- 大数据时代之hadoop(五):hadoop 分布式计算框架(MapReduce)
大数据时代之hadoop(一):hadoop安装 大数据时代之hadoop(二):hadoop脚本解析 大数据时代之hadoop(三):hadoop数据流(生命周期) 大数据时代之hadoop(四): ...
- java Script 中的keyCode 和charCode
其实很长一段时间,我都没有完全弄明白keyCode 和charCode ,自己也认真看过,但是就是理解不透彻,为了防止以后再出现混乱,写篇博客记录一下吧! 首先 在不同的浏览器中,他们有不同的说法哦 ...
- 阿里Android一面(校招)
4.8号晚上8点多接到了阿里一面的电话,面试官人很好,和我聊了半个多小时.我面的是无线事业部,就是做淘宝客户端的那个部门.面试问的都很基础,刚开始问了hashmap和快速排序.接着就是问Android ...
- Quiz 6b Question 7————An Introduction to Interactive Programming in Python
Question 7 Convert the following English description into code. Initialize n to be 1000. Initiali ...
- ajax.js
/**通用ajax服务的定义对象 * services可以是单个服务对象,也可以是service服务数组 * 具体服务的定义请参考appendServices成员函数 */ function Serv ...
- Python网络编程——获取远程设备的IP地址
有时需要把设备的主机名转换成对应的IP地址,下面是一个简单的操作. import socket def get_remote_machine_info(): # 定义get_remote_machin ...
- [LeetCode]题解(python):066-Plus One
题目来源: https://leetcode.com/problems/plus-one/ 题意分析: 给定一个数组,将数加一,返回新的数组.比如[9,9],返回[1,0,0]. 题目思路: 这道题目 ...
- struts文件上传和下载
文件上传 jsp中 <a href="/file/new.action">文件上传案例</a> fileaction中 @Override public S ...
- 虽然net人
http://v.qq.com/boke/page/c/h/0/c01173tzeh0.html http://v.qq.com/boke/page/r/7/x/r0117l07r7x.html ht ...
- Hdu 3371 Connect the Cities(最小生成树)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...