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.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;
/**
*基金查询调用示例代码 - 聚合数据
*在线接口文档:juhe/docs/171
**/
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/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";
//装备您恳求的KEY
public static final String APPKEY ="*************************";
//1.基金基本信息
public static void getRequest1(){
String result =null;
String url ="japi.juhe/fundApi/base.from";//恳求接口地址
Map params = new HashMap();//恳求参数
params.put("key",APPKEY);//您恳求的appkey
params.put("secID","");//证券内部编码基金150001,可通过SecID接口获取到。(可多值输入,以","离隔)
params.put("ticker","");//输入一个或多个基金代码基金150001,用","分隔,如"000001","000001,000003"可通过SecID接口获取到。(可多值输入,以","离隔)
params.put("etfLof","");//基金类型,输入'ETF'会获取ETF类型基金,输入'Lof'会获取LOF类型基金
params.put("listStatusCd","");//上市状况:可输入L-上市,S-暂停,DE-停止上市,UN-未上市(可多值输入)
params.put("pagesize","");//指定每页回来数量,默许悉数
params.put("pagenum","");//指定回来第几页,默许1
try {
result =net(url, params, "GET");
JSONObject object = JSONObject.fromObject(result);
if(object.getInt("error_code")==0){
System.out.println(object.get("result"));
}else{
System.out.println(object.get("error_code")+":"+object.get("reason"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
//2.基金日行情
public static void getRequest2(){
String result =null;
String url ="japi.juhe/fundApi/day.from";//恳求接口地址
Map params = new HashMap();//恳求参数
params.put("key",APPKEY);//您恳求的appkey
params.put("beginDate","");//开始日期,输入格局“YYYYMMDD”
params.put("endDate","");//截止日期,输入格局“YYYYMMDD”
params.put("tradeDate","");//输入一个日期,不输入其他恳求参数,可获取到一天悉数基金日行情数据,输入格局“YYYYMMDD”
params.put("secID","");//证券ID,证券一致编码,可通过getSecID接口获取到。
params.put("ticker","");//基金买卖代码,如'150001'
try {
result =net(url, params, "GET");
JSONObject object = JSONObject.fromObject(result);
if(object.getInt("error_code")==0){
System.out.println(object.get("result"));
}else{
System.out.println(object.get("error_code")+":"+object.get("reason"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
//3.基金前史净值
public static void getRequest3(){
String result =null;
String url ="japi.juhe/fundApi/historyNet.from";//恳求接口地址
Map params = new HashMap();//恳求参数
params.put("key",APPKEY);//您恳求的appKey
params.put("secID","");//证券内部编码(可多值输入,以","切割)
params.put("ticker","");//输入一个或多个基金代码,用","分隔,如"000001"、"000001,000003"(可多值输入)
params.put("dataDate","");//输入一个日期,不输入其他恳求参数,可获取到一天悉数基金净值数据,输入格局“YYYYMMDD”
params.put("beginDate","");//开始净值日期,输入格局“YYYYMMDD”
params.put("endDate","");//截止净值日期,输入格局“YYYYMMDD”
params.put("field","");//可选参数,用,分隔,默许为空,回来悉数字段,不选即为默许值。回来字段见下方
try {
result =net(url, params, "GET");
JSONObject object = JSONObject.fromObject(result);
if(object.getInt("error_code")==0){
System.out.println(object.get("result"));
}else{
System.out.println(object.get("error_code")+":"+object.get("reason"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
//4.基金前史收益
public static void getRequest4(){
String result =null;
String url ="japi.juhe/fundApi/earnings.from";//恳求接口地址
Map params = new HashMap();//恳求参数
params.put("key",APPKEY);//您恳求的appKey
params.put("secID","");//证券内部编码(可多值输入,以","切割)
params.put("ticker","");//输入一个或多个基金代码,用","分隔,如"000001"、"000001,000003"(可多值输入)
params.put("dataDate","");//输入一个日期,不输入其他恳求参数,可获取到一天悉数基金净值数据,输入格局“YYYYMMDD”
params.put("beginDate","");//开始净值日期,输入格局“YYYYMMDD”
params.put("endDate","");//截止净值日期,输入格局“YYYYMMDD”
params.put("field","");//可选参数,用,分隔,默许为空,回来悉数字段,不选即为默许值。回来字段见下方
try {
result =net(url, params, "GET");
JSONObject object = JSONObject.fromObject(result);
if(object.getInt("error_code")==0){
System.out.println(object.get("result"));
}else{
System.out.println(object.get("error_code")+":"+object.get("reason"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
//5.基金净值调整
public static void getRequest5(){
String result =null;
String url ="japi.juhe/fundApi/adjust.from";//恳求接口地址
Map params = new HashMap();//恳求参数
params.put("key",APPKEY);//您恳求的appKey
params.put("secID","");//证券内部编码(可多值输入,以","切割)
params.put("ticker","");//输入一个或多个基金代码,用","分隔,如"000001"、"000001,000003"(可多值输入)
params.put("adjustedType","");//基金净值调整类型,D为分红,S为拆分,默许为D。
params.put("beginDate","");//开始日期,默许为1年前当时日期
params.put("endDate","");//截止日期,默许为今日
params.put("field","");//可选参数,用,分隔,默许为空,回来悉数字段,不选即为默许值。回来字段见下方
try {
result =net(url, params, "GET");
JSONObject object = JSONObject.fromObject(result);
if(object.getInt("error_code")==0){
System.out.println(object.get("result"));
}else{
System.out.println(object.get("error_code")+":"+object.get("reason"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
//6.基金财物装备
public static void getRequest6(){
String result =null;
String url ="japi.juhe/fundApi/asset.from";//恳求接口地址
Map params = new HashMap();//恳求参数
params.put("key",APPKEY);//您恳求的appKey
params.put("secID","");//证券ID,可通过买卖代码和买卖市场在getSecID获取到。
params.put("ticker","");//输入一个或多个基金代码,用","分隔,如"000001"、"000001,000003"(可多值输入)
params.put("reportDate","");//输入一个陈述日期,如20141231,不输入其他恳求参数,可获取这个陈述期悉数基金财物装备,输入格局“YYYYMMDD”
params.put("beginDate","");//开始日期,输入格局“YYYYMMDD”
params.put("endDate","");//截止日期,输入格局“YYYYMMDD”
params.put("field","");//可选参数,用,分隔,默许为空,回来悉数字段,不选即为默许值。回来字段见下方
try {
result =net(url, params, "GET");
JSONObject object = JSONObject.fromObject(result);
if(object.getInt("error_code")==0){
System.out.println(object.get("result"));
}else{
System.out.println(object.get("error_code")+":"+object.get("reason"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
//7.基金持仓明细
public static void getRequest7(){
String result =null;
String url ="japi.juhe/fundApi/holdings.from";//恳求接口地址
Map params = new HashMap();//恳求参数
params.put("key",APPKEY);//您恳求的appKey
params.put("secID","");//证券内部编码(可多值输入,以","切割)
params.put("ticker","");//输入一个或多个基金代码,用","分隔,如"000001"、"000001,000003"(可多值输入)
params.put("reportDate","");//输入一个陈述日期,如20141231,不输入其他恳求参数,可获取这个陈述期悉数基金财物装备,输入格局“YYYYMMDD”
params.put("beginDate","");//开始日期,输入格局“YYYYMMDD”
params.put("endDate","");//截止日期,输入格局“YYYYMMDD”
params.put("secType","");//陈述期内基金出资组合中持有证券类型,可输入一种或多种证券类型,用","分隔,默许为e。(可多值输入)
params.put("field","");//可选参数,用,分隔,默许为空,回来悉数字段,不选即为默许值。回来字段见下方
try {
result =net(url, params, "GET");
JSONObject object = JSONObject.fromObject(result);
if(object.getInt("error_code")==0){
System.out.println(object.get("result"));
}else{
System.out.println(object.get("error_code")+":"+object.get("reason"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
//8.ETF基金申赎清单基本信息
public static void getRequest8(){
String result =null;
String url ="japi.juhe/fundApi/etfBase.from";//恳求接口地址
Map params = new HashMap();//恳求参数
params.put("key",APPKEY);//您恳求的appKey
params.put("secID","");//证券内部编码(可多值输入,以","切割)
params.put("ticker","");//输入一个或多个基金代码,用","分隔,如"000001"、"000001,000003"(可多值输入)
params.put("beginDate","");//开始净值日期,输入格局“YYYYMMDD”
params.put("endDate","");//截止净值日期,输入格局“YYYYMMDD”
params.put("field","");//可选参数,用,分隔,默许为空,回来悉数字段,不选即为默许值。回来字段见下方
try {
result =net(url, params, "GET");
JSONObject object = JSONObject.fromObject(result);
if(object.getInt("error_code")==0){
System.out.println(object.get("result"));
}else{
System.out.println(object.get("error_code")+":"+object.get("reason"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
//9.ETF基金申赎清单成分券信息
public static void getRequest9(){
String result =null;
String url ="japi.juhe/fundApi/etfElement.from";//恳求接口地址
Map params = new HashMap();//恳求参数
params.put("key",APPKEY);//您恳求的appKey
params.put("secID","");//证券内部编码(可多值输入,以","切割)
params.put("ticker","");//输入一个或多个基金代码,用","分隔,如"000001"、"000001,000003"
params.put("beginDate","");//开始净值日期,输入格局“YYYYMMDD”
params.put("endDate","");//截止净值日期,输入格局“YYYYMMDD”
params.put("field","");//可选参数,用,分隔,默许为空,回来悉数字段,不选即为默许值。回来字段见下方
try {
result =net(url, params, "GET");
JSONObject object = JSONObject.fromObject(result);
if(object.getInt("error_code")==0){
System.out.println(object.get("result"));
}else{
System.out.println(object.get("error_code")+":"+object.get("reason"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
//10.基金比例变化
public static void getRequest10(){
String result =null;
String url ="japi.juhe/fundApi/share.from";//恳求接口地址
Map params = new HashMap();//恳求参数
params.put("key",APPKEY);//您恳求的appKey
params.put("secID","");//证券ID,证券一致编码,可通过买卖代码在getSecID获取到。
params.put("ticker","");//输入一个或多个基金代码,用","分隔,如"000001"、"000001,000003"
params.put("beginDate","");//开始日期,输入格局“YYYYMMDD”
params.put("endDate","");//截止日期,输入格局“YYYYMMDD”
params.put("field","");//可选参数,用,分隔,默许为空,回来悉数字段,不选即为默许值。回来字段见下方
try {
result =net(url, params, "GET");
JSONObject object = JSONObject.fromObject(result);
if(object.getInt("error_code")==0){
System.out.println(object.get("result"));
}else{
System.out.println(object.get("error_code")+":"+object.get("reason"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
//11.基金评级
public static void getRequest11(){
String result =null;
String url ="japi.juhe/fundApi/grade.from";//恳求接口地址
Map params = new HashMap();//恳求参数
params.put("key",APPKEY);//您恳求的appKey
params.put("beginDate","");//开始日期,默许为一年前
params.put("endDate","");//截止日期,默许为今日
params.put("secID","");//证券ID,证券一致编码,可通过买卖代码在getSecID获取到。(可多值输入)
params.put("ticker","");//输入一个或多个基金代码,用","分隔,如"000001"、"000001,000003"(可多值输入)
params.put("field","");//可选参数,用,分隔,默许为空,回来悉数字段,不选即为默许值。回来字段见下方
try {
result =net(url, params, "GET");
JSONObject object = JSONObject.fromObject(result);
if(object.getInt("error_code")==0){
System.out.println(object.get("result"));
}else{
System.out.println(object.get("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 || method.equals("GET")){
strUrl = strUrl+"?"+urlencode(params);
}
URL url = new URL(strUrl);
conn = (HttpURLConnection) url.openConnection();
if(method==null || method.equals("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 && method.equals("POST")) {
try {
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
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) {
sb.append(strRead);
}
rs = sb.toString();
} 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 (Map.Entry i : data.entrySet()) {
try {
sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue()+"","UTF-8")).append("&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
从事IT职业多年,用Java,前端,知Python,有多年的开发经历。假如你正在入门学习Java 我做Java这几年整理了一些学习手册,面试题,开发工具,PDF文档书本教程,需求的私信回复主动获取~
————————————————
版权声明:本文为CSDN博主「另个姓名是Larry」的原创文章,遵从CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:blog.csdn.net/weixin_31225063/article/details/114611937
免责声明:本站内容和图片由网友提供或来自网络。
如有违反到您的权益,请通知我们删除处理。文章仅代表作者本人的观点,与本站立场无关!
© 2023 nvsheng.cc 女生-个人图集收集 蜀ICP备2021006193号-3|川公网安备 51130202000403号
发表评论