java 利用HttpURLConnection方式获取restful格式的服务数据
/**
* @Author:
* @Description:利用HttpURLConnection方式获取restful格式的服务数据
* @Date:
*/
private static List<Object> getjsonlist(String targetURL){
List<Object> result_list = new ArrayList<Object>(); try {
URL restServiceURL = new URL(targetURL);
HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
httpConnection.setRequestMethod("GET");
httpConnection.setRequestProperty("Accept", "application/json");
if (httpConnection.getResponseCode() != 200) {
throw new RuntimeException("HTTP GET Request Failed with Error code : "
+ httpConnection.getResponseCode());
} BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
(httpConnection.getInputStream())));
String output;
String result= "";
while ((output = responseBuffer.readLine()) != null) {
result=output;
}
httpConnection.disconnect(); //输出结果内容格式如下:
//<string xmlns="http://od.xxxxa.cn/">{"Result":["91443d34-524f-4fdd-b052-cca52777f434","29b601af-f534-46c7-b272-5801469aaf1c"],
// "ExeResult":true,"ErrorMessage":null,"Message":null,"ResponseTime":"2018-09-11 14:19:24.756",
// "RequestGuid":"c887f1b4-3238-4ed7-a5c2-305fff97caa8"}</string>
int firstIndex = result.indexOf("{");
int lastIndex =result.lastIndexOf("}");
result = result.substring(firstIndex,lastIndex+1);
result_list= CommonMethod.getJsonList(result);
//内容为:
//{"Result":["91443d34-524f-4fdd-b052-cca52777f434","29b601af-f534-46c7-b272-5801469aaf1c"],
// "ExeResult":true,"ErrorMessage":null,"Message":null,"ResponseTime":"2018-09-11 14:19:24.756",
// "RequestGuid":"c887f1b4-3238-4ed7-a5c2-305fff97caa8"}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result_list;
}
/**
* @Author:sks
* @Description:根据Json串获取对应的列表,Json字符串格式如下:
* {"Result":["9cc13ba8-9f4c-40f9-b019-4b885b8f4e7a","8357616f-0cd6-4a8d-838b-264f7b26f2e9"],
* "ExeResult":true,"ErrorMessage":null,"Message":null,"ResponseTime":"2018-09-11 14:37:04.335",
* "RequestGuid":"cac0bbb6-f7a4-4613-9cee-2a90af7cb7af"}
* 要获取ExeResult值为true的结果:["9cc13ba8-9f4c-40f9-b019-4b885b8f4e7a","8357616f-0cd6-4a8d-838b-264f7b26f2e9"]
* @Date:2018-09-11
*/
public static List<Object> getJsonList(String json){
List<Object> list =null;
try {
JSONObject jsonObj = new JSONObject(json);
if (jsonObj != null && jsonObj.get("ExeResult").toString() == "true") {
JSONArray objar = new JSONArray(jsonObj.get("Result").toString());
list = objar.toList();
}
}
catch (JSONException ex){
ex.printStackTrace();
}
return list;
}
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.IOException;
java 利用HttpURLConnection方式获取restful格式的服务数据的更多相关文章
- java通过jni方式获取硬盘序列号(windows,linux)
linux系统java通过jni方式获取硬盘序列号 http://blog.csdn.net/starter110/article/details/8186788 使用jni在windows下读取硬盘 ...
- XML(php中获取xml文件的方式/ajax获取xml格式的响应数据的方式)
1.XML 格式规范: ① 必须有一个根元素 ② 不可有空格.不可以数字或.开头.大小写敏感 ③ 不可交叉嵌套 ④ 属性双引号(浏览器自动修正成双引号了) ⑤ 特殊符号要使用实体 ⑥ 注释和HTML一 ...
- struts2使用jsp和<s:property>标签获取json格式的返回数据
struts2使用jsp和<s:property>标签获取json格式的返回数据 1.struts2的action中 return "success"; 2.指向的返回 ...
- ORACLE 分页 java 用jdbc方式以 sys账号连接oracle数据的问题
2,3,4,6,8 betwenen 为闭区间,前后都包括 select * from(select a.*,rownum rn from (select * from student) a ) ...
- JAVA通过HTTP方式获取数据
测试获取免费天气数据接口:http://www.weather.com.cn/data/sk/101190408.html URL数据如下图: 代码部分: package https; import ...
- Java 利用HttpURLConnection发送http请求
写了一个简单的 Http 请求的Class,实现了 get, post ,postfile package com.asus.uts.util; import org.json.JSONExcepti ...
- java 利用JAX-RS快速开发RESTful 服务
JAX-RS(Java API for RESTful Web Services)同样也是JSR的一部分,详细规范定义见 https://jcp.org/en/jsr/detail?id=311 .从 ...
- java 利用反射机制,获取实体所有属性和方法,并对属性赋值
一个普通的实体Person: private int id; private String name; private Date createdTime;...//其它字段// get set方法 . ...
- java 利用JAX-RS快速开发RESTful 服务实例
首先看web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns=" ...
随机推荐
- Linux C语言编程学习笔记 (1)进程控制入门
想进行Linux系统开发已经很久了,一直没有付诸实践.今日终于开始学习Linux下的C语言编程,研究一天,终于大概弄明白了Linux系统进程管理的一些基本概念和编程方法,总结下来以方便大家学习和自己实 ...
- js的sort()方法
说明 如果调用该方法时没有使用参数,将按字母顺序对数组中的元素进行排序,说得更精确点,是按照字符编码的顺序进行排序.要实现这一点,首先应把数组的元素都转换成字符串(如有必要),以便进行比较. arra ...
- VC6微软正则表达式greta使用案例
#include <string> #include "regexpr2.h" using namespace std; using namespace regex;/ ...
- Codeforces Round #369 (Div. 2) C. Coloring Trees 动态规划
C. Coloring Trees 题目连接: http://www.codeforces.com/contest/711/problem/C Description ZS the Coder and ...
- crontab计划执行脚本详解
Crontab是Linux系统中在固定时间执行某一个程序的工具,类似于Windows系统中的任务计划程序. 一.安装crontab yum install vixie-cron #安装 chkcon ...
- MongoDB简单使用 —— 驱动
C#中可以通过官方的驱动MongoDB.Drvier来使用,使用Nuget安装即可. Install-Package MongoDB.Driver Bson文档操作: using MongoDB.Bs ...
- [转].net reactor 学习系列(五)---源代码加密程序
.NET Reactor使用教程(加密源代码示例) 1.打开 Eziriz .NET Reactor,主界面如图1所示: 图1 2.单击 Main Assembly 右边的 Open,选择要加密的软件 ...
- ios之快速枚举
for(UIView * subView in self.view.subviews) { if([subView isKindOfClass:[XYZSeniorQueryView class]]) ...
- C++类静态数据成员与类静态成员函数
from:://http://blog.csdn.net/taina2008/article/details/1684834 把类中的函数都定义成静态函数,这样相当于在编译时就分配了空间,这样不需要实 ...
- pthread_attr_init线程属性
转自:http://blog.csdn.net/pbymw8iwm/article/details/6721038 1.线程属性 线程具有属性,用pthread_attr_t表示,在对该结构进行处理之 ...