Android studio 解析网页数据,把网页Json格式的数据提取到Android studio上
工具类
HtmlService
具体实现如下:
public class HtmlService {
public static String getHtml(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = conn.getInputStream();//通过输入流获取html数据
byte[] data = readInputStream(inStream);//得到html的二进制数据
String html = new String(data, "UTF-8");
return html;
}
public static byte[] readInputStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len=inStream.read(buffer)) != -1 ){
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}
}
当想要获取网页数据时,比如天气 http://wthrcdn.etouch.cn/weather_mini?city长沙
其网页显示结果如下:
然后
String path= http://wthrcdn.etouch.cn/weather_mini?city长沙;
String html=html = HtmlService.getHtml(path);
html就是结果
更多相关文章
- Android sqlite数据库存取图片信息
- Android数据显示之ListView
- Android的SharedPreferences保存与删除数据简单实例
- 在android中,如何用指定的浏览器打开某网页?
- android查询数据库获得手机里面所有的联系人
- Android静态安全检测 -> HTTPS敏感数据劫持漏洞
- Android MPAndroidChart之PieChart和数据结构以及模型【5】
- android 视图设置多个setTag数据
- Android 采用SharedPreferences读/写数据 【学习记录】