NBA籃球賽事賽程相關(guān)信息
1.
按年份查詢籃球賽事
2.
按球隊(duì)查詢籃球賽事
3.
按對戰(zhàn)球隊(duì)查詢籃球賽事
注意,該示例代碼僅適用于 www. 網(wǎng)站下API 使用該產(chǎn)品前,您需要通過 https://www./#/api/detail/?productID=125
申請API服務(wù)
1.按年份查詢籃球賽事
package net.apishop.www.controller;
import
java.io.DataOutputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.UnsupportedEncodingException;
import
java.net.HttpURLConnection;
import
java.net.MalformedURLException;
import
java.net.URL;
import
java.net.URLEncoder;
import
java.util.HashMap;
import
java.util.Map;
import
com.alibaba.fastjson.JSONObject;
/**
*
httpUrlConnection訪問遠(yuǎn)程接口工具
*/
public
class Api
{
/**
* 方法體說明:向遠(yuǎn)程接口發(fā)起請求,返回字節(jié)流類型結(jié)果
* param url 接口地址
* param requestMethod 請求方式
* param params 傳遞參數(shù)
重點(diǎn):參數(shù)值需要用Base64進(jìn)行轉(zhuǎn)碼
* return InputStream 返回結(jié)果
*/
public static InputStream httpRequestToStream(String url, String requestMethod,
Map<String, String> params)
{
InputStream is = null;
try
{
String parameters = "";
boolean hasParams = false;
// 將參數(shù)集合拼接成特定格式,如name=zhangsan&age=24
for (String key : params.keySet())
{
String value = URLEncoder.encode(params.get(key), "UTF-8");
parameters += key + "=" + value + "&";
hasParams = true;
}
if (hasParams)
{
parameters = parameters.substring(0, parameters.length() - 1);
}
// 請求方式是否為get
boolean isGet = "get".equalsIgnoreCase(requestMethod);
// 請求方式是否為post
boolean isPost = "post".equalsIgnoreCase(requestMethod);
if (isGet)
{
url += "?" + parameters;
}
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
// 請求的參數(shù)類型(使用restlet框架時(shí),為了兼容框架,必須設(shè)置Content-Type為“”空)
conn.setRequestProperty("Content-Type",
"application/octet-stream");
// conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// 設(shè)置連接超時(shí)時(shí)間
conn.setConnectTimeout(50000);
// 設(shè)置讀取返回內(nèi)容超時(shí)時(shí)間
conn.setReadTimeout(50000);
// 設(shè)置向HttpURLConnection對象中輸出,因?yàn)?/span>post方式將請求參數(shù)放在http正文內(nèi),因此需要設(shè)置為ture,默認(rèn)false
if (isPost)
{
conn.setDoOutput(true);
}
// 設(shè)置從HttpURLConnection對象讀入,默認(rèn)為true
conn.setDoInput(true);
// 設(shè)置是否使用緩存,post方式不能使用緩存
if (isPost)
{
conn.setUseCaches(false);
}
// 設(shè)置請求方式,默認(rèn)為GET
conn.setRequestMethod(requestMethod);
// post方式需要將傳遞的參數(shù)輸出到conn對象中
if (isPost)
{
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(parameters);
dos.flush();
dos.close();
}
// 從HttpURLConnection對象中讀取響應(yīng)的消息
// 執(zhí)行該語句時(shí)才正式發(fā)起請求
is = conn.getInputStream();
}
catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch(MalformedURLException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
return is;
}
public static void main(String args[])
{
String url =
"https://api./common/postcode/getPostCodeByAddr";
String requestMethod = "POST";
Map<String, String> params = new HashMap<String,
String>();
params.put("year", ""); //年份(范圍:2014~2017)
params.put("page", ""); //頁碼
params.put("pageSize", ""); //每頁條數(shù)(最多40,默認(rèn)20條)
String result = null;
try
{
InputStream is = httpRequestToStream(url, requestMethod, params);
byte[] b = new byte[is.available()];
is.read(b);
result = new String(b);
}
catch(IOException e)
{
e.printStackTrace();
}
if (result != null)
{
JSONObject jsonObject = JSONObject.parseObject(result);
String status_code = jsonObject.getString("statusCode");
if (status_code == "000000")
{
// 狀態(tài)碼為000000, 說明請求成功
System.out.println("請求成功:" +
jsonObject.getString("result"));
}
else
{
// 狀態(tài)碼非000000, 說明請求失敗
System.out.println("請求失?。?/span>" +
jsonObject.getString("desc"));
}
}
else
{
// 返回內(nèi)容異常,發(fā)送請求失敗,以下可根據(jù)業(yè)務(wù)邏輯自行修改
System.out.println("發(fā)送請求失敗");
}
}
}
2.按球隊(duì)查詢籃球賽事
package net.apishop.www.controller;
import
java.io.DataOutputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.UnsupportedEncodingException;
import
java.net.HttpURLConnection;
import
java.net.MalformedURLException;
import
java.net.URL;
import
java.net.URLEncoder;
import
java.util.HashMap;
import
java.util.Map;
import
com.alibaba.fastjson.JSONObject;
/**
*
httpUrlConnection訪問遠(yuǎn)程接口工具
*/
public
class Api
{
/**
* 方法體說明:向遠(yuǎn)程接口發(fā)起請求,返回字節(jié)流類型結(jié)果
* param url 接口地址
* param requestMethod 請求方式
* param params 傳遞參數(shù)
重點(diǎn):參數(shù)值需要用Base64進(jìn)行轉(zhuǎn)碼
* return InputStream 返回結(jié)果
*/
public static InputStream httpRequestToStream(String url, String requestMethod,
Map<String, String> params)
{
InputStream is = null;
try
{
String parameters = "";
boolean hasParams = false;
// 將參數(shù)集合拼接成特定格式,如name=zhangsan&age=24
for (String key : params.keySet())
{
String value = URLEncoder.encode(params.get(key), "UTF-8");
parameters += key + "=" + value + "&";
hasParams = true;
}
if (hasParams)
{
parameters = parameters.substring(0, parameters.length() - 1);
}
// 請求方式是否為get
boolean isGet = "get".equalsIgnoreCase(requestMethod);
// 請求方式是否為post
boolean isPost = "post".equalsIgnoreCase(requestMethod);
if (isGet)
{
url += "?" + parameters;
}
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
// 請求的參數(shù)類型(使用restlet框架時(shí),為了兼容框架,必須設(shè)置Content-Type為“”空)
conn.setRequestProperty("Content-Type",
"application/octet-stream");
// conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
// 設(shè)置連接超時(shí)時(shí)間
conn.setConnectTimeout(50000);
// 設(shè)置讀取返回內(nèi)容超時(shí)時(shí)間
conn.setReadTimeout(50000);
// 設(shè)置向HttpURLConnection對象中輸出,因?yàn)?/span>post方式將請求參數(shù)放在http正文內(nèi),因此需要設(shè)置為ture,默認(rèn)false
if (isPost)
{
conn.setDoOutput(true);
}
// 設(shè)置從HttpURLConnection對象讀入,默認(rèn)為true
conn.setDoInput(true);
// 設(shè)置是否使用緩存,post方式不能使用緩存
if (isPost)
{
conn.setUseCaches(false);
}
// 設(shè)置請求方式,默認(rèn)為GET
conn.setRequestMethod(requestMethod);
// post方式需要將傳遞的參數(shù)輸出到conn對象中
if (isPost)
{
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(parameters);
dos.flush();
dos.close();
}
// 從HttpURLConnection對象中讀取響應(yīng)的消息
// 執(zhí)行該語句時(shí)才正式發(fā)起請求
is = conn.getInputStream();
}
catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch(MalformedURLException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
return is;
}
public static void main(String args[])
{
String url =
"https://api./common/postcode/getPostCodeByAddr";
String requestMethod = "POST";
Map<String, String> params = new HashMap<String,
String>();
params.put("team", ""); //球隊(duì)名稱(如“火箭”)
params.put("page", ""); //頁碼
params.put("pageSize", ""); //每頁條數(shù)(最多40,默認(rèn)20條)
String result = null;
try
{
InputStream is = httpRequestToStream(url, requestMethod, params);
byte[] b = new byte[is.available()];
is.read(b);
result = new String(b);
}
catch(IOException e)
{
e.printStackTrace();
}
if (result != null)
{
JSONObject jsonObject = JSONObject.parseObject(result);
String status_code = jsonObject.getString("statusCode");
if (status_code == "000000")
{
// 狀態(tài)碼為000000, 說明請求成功
System.out.println("請求成功:" +
jsonObject.getString("result"));
}
else
{
// 狀態(tài)碼非000000, 說明請求失敗
System.out.println("請求失?。?/span>" +
jsonObject.getString("desc"));
}
}
else
{
// 返回內(nèi)容異常,發(fā)送請求失敗,以下可根據(jù)業(yè)務(wù)邏輯自行修改
System.out.println("發(fā)送請求失敗");
}
}
}
3.按對戰(zhàn)球隊(duì)查詢籃球賽事
package net.apishop.www.controller;
import
java.io.DataOutputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.UnsupportedEncodingException;
import
java.net.HttpURLConnection;
import
java.net.MalformedURLException;
import
java.net.URL;
import
java.net.URLEncoder;
import
java.util.HashMap;
import
java.util.Map;
import
com.alibaba.fastjson.JSONObject;
/**
*
httpUrlConnection訪問遠(yuǎn)程接口工具
*/
public
class Api
{
/**
* 方法體說明:向遠(yuǎn)程接口發(fā)起請求,返回字節(jié)流類型結(jié)果
* param url 接口地址
* param requestMethod 請求方式
* param params 傳遞參數(shù)
重點(diǎn):參數(shù)值需要用Base64進(jìn)行轉(zhuǎn)碼
* return InputStream 返回結(jié)果
*/
public static InputStream httpRequestToStream(String url, String requestMethod,
Map<String, String> params)
{
InputStream is = null;
try
{
String parameters = "";
boolean hasParams = false;
// 將參數(shù)集合拼接成特定格式,如name=zhangsan&age=24
for (String key : params.keySet())
{
String value = URLEncoder.encode(params.get(key), "UTF-8");
parameters += key + "=" + value + "&";
hasParams = true;
}
if (hasParams)
{
parameters = parameters.substring(0, parameters.length() - 1);
}
// 請求方式是否為get
boolean isGet = "get".equalsIgnoreCase(requestMethod);
// 請求方式是否為post
boolean isPost = "post".equalsIgnoreCase(requestMethod);
if (isGet)
{
url += "?" + parameters;
}
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
// 請求的參數(shù)類型(使用restlet框架時(shí),為了兼容框架,必須設(shè)置Content-Type為“”空)
conn.setRequestProperty("Content-Type",
"application/octet-stream");
// conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
// 設(shè)置連接超時(shí)時(shí)間
conn.setConnectTimeout(50000);
// 設(shè)置讀取返回內(nèi)容超時(shí)時(shí)間
conn.setReadTimeout(50000);
// 設(shè)置向HttpURLConnection對象中輸出,因?yàn)?/span>post方式將請求參數(shù)放在http正文內(nèi),因此需要設(shè)置為ture,默認(rèn)false
if (isPost)
{
conn.setDoOutput(true);
}
// 設(shè)置從HttpURLConnection對象讀入,默認(rèn)為true
conn.setDoInput(true);
// 設(shè)置是否使用緩存,post方式不能使用緩存
if (isPost)
{
conn.setUseCaches(false);
}
// 設(shè)置請求方式,默認(rèn)為GET
conn.setRequestMethod(requestMethod);
// post方式需要將傳遞的參數(shù)輸出到conn對象中
if (isPost)
{
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(parameters);
dos.flush();
dos.close();
}
// 從HttpURLConnection對象中讀取響應(yīng)的消息
// 執(zhí)行該語句時(shí)才正式發(fā)起請求
is = conn.getInputStream();
}
catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch(MalformedURLException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
return is;
}
public static void main(String args[])
{
String url =
"https://api./common/postcode/getPostCodeByAddr";
String requestMethod = "POST";
Map<String, String> params = new HashMap<String,
String>();
params.put("team1", ""); //客隊(duì)球隊(duì)名稱(如“火箭”)
params.put("team2", ""); //主隊(duì)球隊(duì)名稱(如“馬刺”)
params.put("page", ""); //頁碼
params.put("pageSize", ""); //每頁條數(shù)(最多40,默認(rèn)20條)
String result = null;
try
{
InputStream is = httpRequestToStream(url, requestMethod, params);
byte[] b = new byte[is.available()];
is.read(b);
result = new String(b);
}
catch(IOException e)
{
e.printStackTrace();
}
if (result != null)
{
JSONObject jsonObject = JSONObject.parseObject(result);
String status_code = jsonObject.getString("statusCode");
if (status_code == "000000")
{
// 狀態(tài)碼為000000, 說明請求成功
System.out.println("請求成功:" +
jsonObject.getString("result"));
}
else
{
// 狀態(tài)碼非000000, 說明請求失敗
System.out.println("請求失敗:" +
jsonObject.getString("desc"));
}
}
else
{
// 返回內(nèi)容異常,發(fā)送請求失敗,以下可根據(jù)業(yè)務(wù)邏輯自行修改
System.out.println("發(fā)送請求失敗");
}
}
}
|