Java編程實(shí)現(xiàn)NBA賽事接口調(diào)用實(shí)例代碼
第一步:找別人提供的接口,比如在這里我選擇的是聚合數(shù)據(jù)提供的接口
第二步:要申請(qǐng)相應(yīng)的AppKey方可使用,此參數(shù)會(huì)作為接口的參數(shù)調(diào)用。
第三步:調(diào)用別人提供的接口方法
代碼如下:
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賽事調(diào)用示例代碼 - 聚合數(shù)據(jù)
*在線接口文檔: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";
//配置您申請(qǐng)的KEY
public static final String APPKEY ="b5ca2acdab01c88c99d532cdfb5c3aa2";
//1.NBA常規(guī)賽賽程賽果
public static void getRequest1(){
String result =null;
String url ="https://op.juhe.cn/onebox/basketball/nba";
//請(qǐng)求接口地址
Map params = new HashMap();
//請(qǐng)求參數(shù)
params.put("key",APPKEY);
//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁(yè)查詢)
params.put("dtype","");
//返回?cái)?shù)據(jù)的格式,xml或json,默認(rèn)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.球隊(duì)賽程賽事查詢
public static void getRequest2(){
String result =null;
String url ="https://op.juhe.cn/onebox/basketball/team";
//請(qǐng)求接口地址
Map params = new HashMap();
//請(qǐng)求參數(shù)
params.put("key",APPKEY);
//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁(yè)查詢)
params.put("dtype","");
//返回?cái)?shù)據(jù)的格式,xml或json,默認(rèn)json
params.put("team","");
//球隊(duì)名稱
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.球隊(duì)對(duì)戰(zhàn)賽賽程查詢
public static void getRequest3(){
String result =null;
String url ="https://op.juhe.cn/onebox/basketball/combat";
//請(qǐng)求接口地址
Map params = new HashMap();
//請(qǐng)求參數(shù)
params.put("key",APPKEY);
//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁(yè)查詢)
params.put("dtype","");
//返回?cái)?shù)據(jù)的格式,xml或json,默認(rèn)json
params.put("hteam","勇士");
//主隊(duì)球隊(duì)名稱
params.put("vteam","76人");
//客隊(duì)球隊(duì)名稱
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 請(qǐng)求地址
* @param params 請(qǐng)求參數(shù)
* @param method 請(qǐng)求方法
* @return 網(wǎng)絡(luò)請(qǐng)求字符串
* @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型轉(zhuǎn)為請(qǐng)求參數(shù)型
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();
}
}
package juheapi.nba;
/**
* Created by Administrator on 2017/11/19/019.
*/
public class NBADemoTest extends NBADemo{
public static void main(String[] args) {
getRequest3();
}
}
在使用上述代碼時(shí)可能會(huì)出現(xiàn)異常,一般都是由于jar包不全導(dǎo)致的。
問題一: 拋出 NestableRuntimeException

解決辦法:查看jar包是否完整
包括:commons-beanutils-1.7.0.jar
commons-collections-3.1.jar
commons-lang-2.4.jar (使用過高版本也會(huì)報(bào)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包。

總結(jié)
以上就是本文關(guān)于Java編程實(shí)現(xiàn)NBA賽事接口調(diào)用實(shí)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的網(wǎng)絡(luò)爬蟲代碼示例
如有不足之處,歡迎留言指出。
相關(guān)文章
springboot使用@KafkaListener監(jiān)聽多個(gè)kafka配置實(shí)現(xiàn)
當(dāng)服務(wù)中需要監(jiān)聽多個(gè)kafka時(shí),?需要配置多個(gè)kafka,本文主要介紹了springboot使用@KafkaListener監(jiān)聽多個(gè)kafka配置實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04
Spring使用IOC與DI實(shí)現(xiàn)完全注解開發(fā)
IOC也是Spring的核心之一了,之前學(xué)的時(shí)候是采用xml配置文件的方式去實(shí)現(xiàn)的,后來(lái)其中也多少穿插了幾個(gè)注解,但是沒有說(shuō)完全采用注解實(shí)現(xiàn)。那么這篇文章就和大家分享一下,全部采用注解來(lái)實(shí)現(xiàn)IOC + DI2022-09-09
Java用自定義的類作為HashMap的key值實(shí)例
下面小編就為大家?guī)?lái)一篇Java用自定義的類作為HashMap的key值實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2016-12-12
spring一個(gè)項(xiàng)目多個(gè)模塊聚合打包問題解決方案(最新推薦)
最近遇到個(gè)需求,針對(duì)后端解耦模塊較多的項(xiàng)目,想在云端啟動(dòng)時(shí)簡(jiǎn)潔些只啟動(dòng)一個(gè)jar文件的情景,本文重點(diǎn)給大家介紹spring一個(gè)項(xiàng)目多個(gè)模塊聚合打包問題解決方案,感興趣的朋友一起看看吧2023-09-09
自定義注解和springAOP捕獲Service層異常,并處理自定義異常操作
這篇文章主要介紹了自定義注解和springAOP捕獲Service層異常,并處理自定義異常操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06

