httpURLConnection获得网络数据解析json串
来源:互联网 发布:北航软件工程硕士学费 编辑:程序博客网 时间:2024/04/19 18:13
new Thread() {
public void run() {
getData();
};
}.start();
}
/**
* 获取网络数据
*/
private void getData() {
// 要访问的服务器地址
String path = "http://172.20.81.59:8080/bwie/json/index.json";
try {
// 设置要访问地址
URL url = new URL(path);
// 打开连接,得到HttpURLConnection对象
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
// 设置联网超时
connection.setConnectTimeout(5000);
// 设置请求方式
connection.setRequestMethod("GET");
//连接
connection.connect();
// 得到响应码
int responseCode = connection.getResponseCode();
// 如果响应码是200,代表请求成功
if (responseCode == 200) {
// 得到输入流,包含请求到的数据
InputStream inputStream = connection.getInputStream();
// 记录每一行读到的数据
String s = "";
// 追加流里的数据
StringBuffer buffer = new StringBuffer();
// 读出流里的数据
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
// 只要不等于null,流里就一直有数据,就要一直循环读取
while ((s = bufferedReader.readLine()) != null) {
buffer.append(s);
}
String data = buffer.toString();
System.out.println(data);
// 解析数据
Gson gson = new Gson();
Bean bean = gson.fromJson(data, Bean.class);
// 子线程不能更新UI,通过handler发送到主线程
Message msg = Message.obtain();
msg.obj = bean;
handler.sendMessage(msg);
- httpURLConnection获得网络数据解析json串
- HttpURLConnection解析网络数据
- Android使用HttpURLConnection请求网络返回JSON数据并解析
- HttpURLConnection获得网络数据(Gson应用)
- HttpUrlConnection获取网络json串
- Android HttpURLConnection数据获取并JSON解析
- 使用HttpURLConnection请求Gson网络解析数据
- HttpURLConnection下载数据,JSON解析数据, BaseAdapter 适配数据。
- HttpURLConnection请求数据(GET)并用JSON解析数据(二)
- UI-json解析网络数据
- 网络编程--JSON数据解析
- Json解析网络请求数据
- HttpURLConnection请求json数据
- HttpURLConnection解析数据
- HttpURLConnection 网络请求数据
- HttpURLConnection---网络请求数据
- 网络数据传递之 Json数据解析
- httpurlconnection用于解析Json文件
- ThreadLocal的错误理解纠正
- Quartz(08) quartz spring web 项目的整合(方法二)
- wireshark学习总结(1)---有用的链接
- android studio sqlite一个基本操作
- 集训总结一
- httpURLConnection获得网络数据解析json串
- java运算符
- UVA532 Dungeon Master
- UI基本控件
- 物理地址和逻辑地址(讲的很好)
- Java 判断字符串是否为空的三种方法.
- 172. Factorial Trailing Zeroes
- texlive - 专业的LaTeX: 在Linux下编写高质量的文档
- IOS开发-基础篇-One