HttpURLConnection加载数据
2016-03-02 19:45
435 查看
new Thread(){
public void run() {
getInfo();
}
}.start();
private void getInfo() {
String get_path="http://169.254.172.203:8081/jsondata.json";
try {
URL url = new URL(get_path);
HttpURLConnection openConnection=(HttpURLConnection)url.openConnection();
//设置链接超时
openConnection.setConnectTimeout(5000);
//设置请求方式
openConnection.setRequestMethod("GET");
//设置读取超时
openConnection.setReadTimeout(5000);
//开始链接
openConnection.connect();
//通过响应吗判断是否链接成功
if(openConnection.getResponseCode()==200){
InputStream inputStream = openConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
//接受流里面的数据
String readData;
//拼接流里的数据,保证数据是完整的
StringBuilder stringBuilder = new StringBuilder();
while((readData=bufferedReader.readLine())!=null){
stringBuilder.append(readData);
}
String stringData=stringBuilder.toString();
Gson gson=new Gson();
JsonData jsonData = gson.fromJson(stringData, JsonData.class);
Data data = jsonData.data;
ArrayList<Items> items = data.items;
for (int i = 0; i < items.size(); i++) {
String name = items.get(i).name;
}
// InputStream open = getAssets().open("jsondata.json");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public void run() {
getInfo();
}
}.start();
private void getInfo() {
String get_path="http://169.254.172.203:8081/jsondata.json";
try {
URL url = new URL(get_path);
HttpURLConnection openConnection=(HttpURLConnection)url.openConnection();
//设置链接超时
openConnection.setConnectTimeout(5000);
//设置请求方式
openConnection.setRequestMethod("GET");
//设置读取超时
openConnection.setReadTimeout(5000);
//开始链接
openConnection.connect();
//通过响应吗判断是否链接成功
if(openConnection.getResponseCode()==200){
InputStream inputStream = openConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
//接受流里面的数据
String readData;
//拼接流里的数据,保证数据是完整的
StringBuilder stringBuilder = new StringBuilder();
while((readData=bufferedReader.readLine())!=null){
stringBuilder.append(readData);
}
String stringData=stringBuilder.toString();
Gson gson=new Gson();
JsonData jsonData = gson.fromJson(stringData, JsonData.class);
Data data = jsonData.data;
ArrayList<Items> items = data.items;
for (int i = 0; i < items.size(); i++) {
String name = items.get(i).name;
}
// InputStream open = getAssets().open("jsondata.json");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
相关文章推荐
- HttpApplication的处理管道19个事件。
- https://localhost:1158/em 页面无法打开的问题
- 创建一个简单的HTTP服务(自动查找未使用的端口)
- Tomcat的HTTP和AJP连接器
- 虚幻引擎网络架构——No.0概述
- 获取网络状态ios(2G、3G、4G、Wifi)
- android网络下载mp3文件不全问题
- python调试django-rest-framework API的小工具--httpie
- 杂记——ASIHTTPReques 学习
- 给libvirt创建的虚拟机指定固定IP
- iOS Https SSL认证 - AFNetWorking
- Android移动端网络优化
- Android Https
- 递归神经网络(RNN)简介
- ios 网络编程之多线程
- http://www.cnblogs.com/stephen-liu74/archive/2012/08/01/2561557.html
- 选择HttpHandler还是HttpModule?
- 详解TCP协议的服务特点以及连接建立与终止的过程(俗称三次握手四次挥手)
- 【error】No 'Access-Control-Allow-Origin' 跨域问题
- IOS--网络请求基础