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中的组件在类中进行绑定.然后写了一个类用来获取网 ...
随机推荐
- ASP.NET页面传值方式
http://www.cnblogs.com/zhangkai2237/archive/2012/05/06/2486462.html http://www.cnblogs.com/xiaoyusmi ...
- 移除Strorefront站点footer上的Storefront Design By WooThemes字样
进入wp-content\themes\storefront\inc\structure\footer.php, 注释掉代码: if ( ! function_exists( 'storefront_ ...
- Nginx 之三:nginx服务器模块、web请求处理机制及事件驱动模型、进程功能和进程间通信
一:Nginx的模块化结构设计: 1.核心模块:指的是nginx服务器运行当中必不可少的模块,这些模块提供了最基本最核心的服务,比如权限控制.进程管理.错误日志.事件驱动.正则表达式解析等,nginx ...
- poj 1905 Expanding Rods 二分
/** 题解晚上写 **/ #include <iostream> #include <math.h> #include <algorithm> #include ...
- 高级UNIX环境编程
常规文件 只有索引节号(i-number,是一批i-node的索引),i-node不含文件名及数据字节,包括文件类型,链接数量,uid,gid,访问权限,字节数,最后访问时间,最后修改时间,信息节点最 ...
- cocos2dx进阶学习之坐标转换
在cocos2dx中,有四种坐标系 GL坐标系:左下为原点,x轴向右,y轴向上 UI坐标系:左上为原点,x轴向右,y轴向下 世界坐标系:与GL坐标系相同 本地坐标系:是节点(CCNode)的坐标系,原 ...
- 基于visual Studio2013解决C语言竞赛题之0306分数转换
题目 解决代码及点评 根据题目要去,我们可以通过if实现该功能,伪代码如下: if(a > 90) print 'A' else if(a>80) print 'b' else ...
- Uva 552 Prime Ring Problem(dfs)
题目链接:Uva 552 思路分析:时间限制为3s,数据较小,使用深度搜索查找所有的解. 代码如下: #include <iostream> #include <string.h&g ...
- 暂时和永久改动oracle sysdate的默认输出格式
1.当前会话有效 alter session set NLS_DATE_FORMAT='YYYY-MM-DD:HH24:MI:SS'; 2.永久生效 sys用户登入后运行例如以下命令 然后重新启动数据 ...
- 请问set JAVA_OPTS的各项參数是什么意思?
http://topic.csdn.net/u/20090910/10/20c6ba01-28ac-482e-94b2-bfce0a952f77.html 请问set JAVA_OPTS的各项參数是什 ...