第一步:找別人提供的接口,比如在這里我選擇的是聚合數據提供的接口
第二步:要申請相應的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