第一步:找别人提供的接口,比如在这里我选择的是聚合数据提供的接口
第二步:要申请相应的appkey方可使用,此参数会作为接口的参数调用。
第三步:调用别人提供的接口方法
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
package juheapi.nba; /** * created by administrator on 2017/11/19/019. */ import net.sf.json.jsonobject; import java.io.*; import java.net.httpurlconnection; import java.net.url; import java.net.urlencoder; import java.util.hashmap; import java.util.map; /** *nba赛事调用示例代码 - 聚合数据 *在线接口文档:https://www.juhe.cn/docs/92 **/ public class nbademo { 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 = "b5ca2acdab01c88c99d532cdfb5c3aa2" ; //1.nba常规赛赛程赛果 public static void getrequest1(){ string result = null ; string url = "https://op.juhe.cn/onebox/basketball/nba" ; //请求接口地址 map params = new hashmap(); //请求参数 params.put( "key" ,appkey); //应用appkey(应用详细页查询) params.put( "dtype" , "" ); //返回数据的格式,xml或json,默认json 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 = "https://op.juhe.cn/onebox/basketball/team" ; //请求接口地址 map params = new hashmap(); //请求参数 params.put( "key" ,appkey); //应用appkey(应用详细页查询) params.put( "dtype" , "" ); //返回数据的格式,xml或json,默认json params.put( "team" , "" ); //球队名称 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 = "https://op.juhe.cn/onebox/basketball/combat" ; //请求接口地址 map params = new hashmap(); //请求参数 params.put( "key" ,appkey); //应用appkey(应用详细页查询) params.put( "dtype" , "" ); //返回数据的格式,xml或json,默认json params.put( "hteam" , "勇士" ); //主队球队名称 params.put( "vteam" , "76人" ); //客队球队名称 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(); } } /** * * @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(map<string,object>data) { 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(); } } |
1
2
3
4
5
6
7
8
9
10
11
|
package juheapi.nba; /** * created by administrator on 2017/11/19/019. */ public class nbademotest extends nbademo{ public static void main(string[] args) { getrequest3(); } } |
在使用上述代码时可能会出现异常,一般都是由于jar包不全导致的。
问题一: 抛出 nestableruntimeexception
解决办法:查看jar包是否完整
包括:commons-beanutils-1.7.0.jar
commons-collections-3.1.jar
commons-lang-2.4.jar (使用过高版本也会报nestableruntimeexception)
commons-logging-1.1.1.jar
ezmorph-1.0.6.jar
json-lib-2.1.jar
log4j.jar
问题二:user-specified log class'org.apache.commons.logging.impl.log4jlogger' cannot be found or is notuseable.
解决办法:添加log4j的jar包。
总结
以上就是本文关于java编程实现nba赛事接口调用实例代码的全部内容,希望对大家有所帮助。
原文链接:https://www.2cto.com/kf/201711/699181.html