import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.u;
import java.u;
import net.;
/**
*基金查询调用示例代码 - 聚合数据
*在线接口文档:
**/
public class JuheDemo {
public static final String DEF_CHATSET = "UTF-8";
public static final int DEF_CONN_TIMEOUT = 30000;
public static final int DEF_READ_TIMEOUT = 30000;
public static String userAgent = "Mozilla (Windows NT 6.1) AppleWebKi (KHTML, like Gecko) Chrome Safari;;
//配置您申请的KEY
public static final String APPKEY ="*************************";
//1.基金基本信息
public static void getRequest1(){
String result =null;
String url =";;//请求接口地址
Map params = new HashMap();//请求参数
("key",APPKEY);//您申请的appkey
("secID","");//证券内部编码,可通过SecID接口获取到。(可多值输入,以","隔开)
("ticker","");//输入一个或多个基金代码,用","分隔,如"000001","000001,000003"可通过SecID接口获取到。(可多值输入,以","隔开)
("etfLof","");//基金类型,输入'ETF'会获取ETF类型基金,输入'Lof'会获取LOF类型基金
("listStatusCd","");//上市状态:可输入L-上市,S-暂停,DE-终止上市,UN-未上市(可多值输入)
("pagesize","");//指定每页返回数量,默认全部
("pagenum","");//指定返回第几页,默认1
try {
result =net(url, params, "GET");
JSONObject object = JSONObject.fromObject(result);
i("error_code")==0){
Sy("result"));
}else{
Sy("error_code")+":"+object.get("reason"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
//2.基金日行情
public static void getRequest2(){
String result =null;
String url =";;//请求接口地址
Map params = new HashMap();//请求参数
("key",APPKEY);//您申请的appkey
("beginDate","");//起始日期,输入格式“YYYYMMDD”
("endDate","");//截止日期,输入格式“YYYYMMDD”
("tradeDate","");//输入一个日期,不输入其他请求参数,可获取到一天全部基金日行情数据,输入格式“YYYYMMDD”
("secID","");//证券ID,证券统一编码,可通过getSecID接口获取到。
("ticker","");//基金交易代码,如'150001'
try {
result =net(url, params, "GET");
JSONObject object = JSONObject.fromObject(result);
i("error_code")==0){
Sy("result"));
}else{
Sy("error_code")+":"+object.get("reason"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
//3.基金历史净值
public static void getRequest3(){
String result =null;
String url =";;//请求接口地址
Map params = new HashMap();//请求参数
("key",APPKEY);//您申请的appKey
("secID","");//证券内部编码(可多值输入,以","分割)
("ticker","");//输入一个或多个基金代码,用","分隔,如"000001"、"000001,000003"(可多值输入)
("dataDate","");//输入一个日期,不输入其他请求参数,可获取到一天全部基金净值数据,输入格式“YYYYMMDD”
("beginDate","");//起始净值日期,输入格式“YYYYMMDD”
("endDate","");//截止净值日期,输入格式“YYYYMMDD”
("field","");//可选参数,用,分隔,默认为空,返回全部字段,不选即为默认值。返回字段见下方
try {
result =net(url, params, "GET");
JSONObject object = JSONObject.fromObject(result);
i("error_code")==0){
Sy("result"));
}else{
Sy("error_code")+":"+object.get("reason"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
//4.基金历史收益
public static void getRequest4(){
String result =null;
String url =";;//请求接口地址
Map params = new HashMap();//请求参数
("key",APPKEY);//您申请的appKey
("secID","");//证券内部编码(可多值输入,以","分割)
("ticker","");//输入一个或多个基金代码,用","分隔,如"000001"、"000001,000003"(可多值输入)
("dataDate","");//输入一个日期,不输入其他请求参数,可获取到一天全部基金净值数据,输入格式“YYYYMMDD”
("beginDate","");//起始净值日期,输入格式“YYYYMMDD”
("endDate","");//截止净值日期,输入格式“YYYYMMDD”
("field","");//可选参数,用,分隔,默认为空,返回全部字段,不选即为默认值。返回字段见下方
try {
result =net(url, params, "GET");
JSONObject object = JSONObject.fromObject(result);
i("error_code")==0){
Sy("result"));
}else{
Sy("error_code")+":"+object.get("reason"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
//5.基金净值调整
public static void getRequest5(){
String result =null;
String url =";;//请求接口地址
Map params = new HashMap();//请求参数
("key",APPKEY);//您申请的appKey
("secID","");//证券内部编码(可多值输入,以","分割)
("ticker","");//输入一个或多个基金代码,用","分隔,如"000001"、"000001,000003"(可多值输入)
("adjustedType","");//基金净值调整类型,D为分红,S为拆分,默认为D。
("beginDate","");//起始日期,默认为1年前当前日期
("endDate","");//截止日期,默认为今天
("field","");//可选参数,用,分隔,默认为空,返回全部字段,不选即为默认值。返回字段见下方
try {
result =net(url, params, "GET");
JSONObject object = JSONObject.fromObject(result);
i("error_code")==0){
Sy("result"));
}else{
Sy("error_code")+":"+object.get("reason"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
//6.基金资产配置
public static void getRequest6(){
String result =null;
String url =";;//请求接口地址
Map params = new HashMap();//请求参数
("key",APPKEY);//您申请的appKey
("secID","");//证券ID,可通过交易代码和交易市场在getSecID获取到。
("ticker","");//输入一个或多个基金代码,用","分隔,如"000001"、"000001,000003"(可多值输入)
("reportDate","");//输入一个报告日期,如20141231,不输入其他请求参数,可获取这个报告期全部基金资产配置,输入格式“YYYYMMDD”
("beginDate","");//起始日期,输入格式“YYYYMMDD”
("endDate","");//截止日期,输入格式“YYYYMMDD”
("field","");//可选参数,用,分隔,默认为空,返回全部字段,不选即为默认值。返回字段见下方
try {
result =net(url, params, "GET");
JSONObject object = JSONObject.fromObject(result);
i("error_code")==0){
Sy("result"));
}else{
Sy("error_code")+":"+object.get("reason"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
//7.基金持仓明细
public static void getRequest7(){
String result =null;
String url =";;//请求接口地址
Map params = new HashMap();//请求参数
("key",APPKEY);//您申请的appKey
("secID","");//证券内部编码(可多值输入,以","分割)
("ticker","");//输入一个或多个基金代码,用","分隔,如"000001"、"000001,000003"(可多值输入)
("reportDate","");//输入一个报告日期,如20141231,不输入其他请求参数,可获取这个报告期全部基金资产配置,输入格式“YYYYMMDD”
("beginDate","");//起始日期,输入格式“YYYYMMDD”
("endDate","");//截止日期,输入格式“YYYYMMDD”
("secType","");//报告期内基金投资组合中持有证券类型,可输入一种或多种证券类型,用","分隔,默认为e。(可多值输入)
("field","");//可选参数,用,分隔,默认为空,返回全部字段,不选即为默认值。返回字段见下方
try {
result =net(url, params, "GET");
JSONObject object = JSONObject.fromObject(result);
i("error_code")==0){
Sy("result"));
}else{
Sy("error_code")+":"+object.get("reason"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
基金申赎清单基本信息
public static void getRequest8(){
String result =null;
String url =";;//请求接口地址
Map params = new HashMap();//请求参数
("key",APPKEY);//您申请的appKey
("secID","");//证券内部编码(可多值输入,以","分割)
("ticker","");//输入一个或多个基金代码,用","分隔,如"000001"、"000001,000003"(可多值输入)
("beginDate","");//起始净值日期,输入格式“YYYYMMDD”
("endDate","");//截止净值日期,输入格式“YYYYMMDD”
("field","");//可选参数,用,分隔,默认为空,返回全部字段,不选即为默认值。返回字段见下方
try {
result =net(url, params, "GET");
JSONObject object = JSONObject.fromObject(result);
i("error_code")==0){
Sy("result"));
}else{
Sy("error_code")+":"+object.get("reason"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
基金申赎清单成分券信息
public static void getRequest9(){
String result =null;
String url =";;//请求接口地址
Map params = new HashMap();//请求参数
("key",APPKEY);//您申请的appKey
("secID","");//证券内部编码(可多值输入,以","分割)
("ticker","");//输入一个或多个基金代码,用","分隔,如"000001"、"000001,000003"
("beginDate","");//起始净值日期,输入格式“YYYYMMDD”
("endDate","");//截止净值日期,输入格式“YYYYMMDD”
("field","");//可选参数,用,分隔,默认为空,返回全部字段,不选即为默认值。返回字段见下方
try {
result =net(url, params, "GET");
JSONObject object = JSONObject.fromObject(result);
i("error_code")==0){
Sy("result"));
}else{
Sy("error_code")+":"+object.get("reason"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
//10.基金份额变动
public static void getRequest10(){
String result =null;
String url =";;//请求接口地址
Map params = new HashMap();//请求参数
("key",APPKEY);//您申请的appKey
("secID","");//证券ID,证券统一编码,可通过交易代码在getSecID获取到。
("ticker","");//输入一个或多个基金代码,用","分隔,如"000001"、"000001,000003"
("beginDate","");//起始日期,输入格式“YYYYMMDD”
("endDate","");//截止日期,输入格式“YYYYMMDD”
("field","");//可选参数,用,分隔,默认为空,返回全部字段,不选即为默认值。返回字段见下方
try {
result =net(url, params, "GET");
JSONObject object = JSONObject.fromObject(result);
i("error_code")==0){
Sy("result"));
}else{
Sy("error_code")+":"+object.get("reason"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
//11.基金评级
public static void getRequest11(){
String result =null;
String url =";;//请求接口地址
Map params = new HashMap();//请求参数
("key",APPKEY);//您申请的appKey
("beginDate","");//起始日期,默认为一年前
("endDate","");//截止日期,默认为今天
("secID","");//证券ID,证券统一编码,可通过交易代码在getSecID获取到。(可多值输入)
("ticker","");//输入一个或多个基金代码,用","分隔,如"000001"、"000001,000003"(可多值输入)
("field","");//可选参数,用,分隔,默认为空,返回全部字段,不选即为默认值。返回字段见下方
try {
result =net(url, params, "GET");
JSONObject object = JSONObject.fromObject(result);
i("error_code")==0){
Sy("result"));
}else{
Sy("error_code")+":"+object.get("reason"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
}
/**
*
* @param strUrl 请求地址
* @param params 请求参数
* @param method 请求方法
* @return 网络请求字符串
* @throws Exception
*/
public static String net(String strUrl, Map params,String method) throws Exception {
HttpURLConnection conn = null;
BufferedReader reader = null;
String rs = null;
try {
StringBuffer sb = new StringBuffer();
if(method==null || me("GET")){
strUrl = strUrl+"?"+urlencode(params);
}
URL url = new URL(strUrl);
conn = (HttpURLConnection) url.openConnection();
if(method==null || me("GET")){
conn.setRequestMethod("GET");
}else{
conn.setRequestMethod("POST");
conn.setDoOutput(true);
}
conn.setRequestProperty("User-agent", userAgent);
conn.setUseCaches(false);
conn.setConnectTimeout(DEF_CONN_TIMEOUT);
conn.setReadTimeout(DEF_READ_TIMEOUT);
conn.setInstanceFollowRedirects(false);
conn.connect();
if (params!= null && me("POST")) {
try {
DataOutputStream out = new DataOutputStream());
out.writeBytes(urlencode(params));
} catch (Exception e) {
// TODO: handle exception
}
}
InputStream is = conn.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
(strRead);
}
rs = ();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
if (conn != null) {
conn.disconnect();
}
}
return rs;
}
//将map型转为请求参数型
public static String urlencode(Mapdata) {
StringBuilder sb = new StringBuilder();
for i : da()) {
try {
()).append("=").append()+"","UTF-8")).append("&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return ();
}
}
从事IT行业多年,用Java,前端,知Python,有多年的开发经验。如果你正在入门学习Java 我做Java这几年整理了一些学习手册,面试题,开发工具,PDF文档书籍教程,需要的私信回复(1)自动获取~
————————————————
版权声明:本文为CSDN博主「另个名字是Larry」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接: