返回顶部
首页 > 资讯 > 精选 >Java编程实现NBA赛事接口调用实例代码
  • 643
分享到

Java编程实现NBA赛事接口调用实例代码

java接口对接开发实例ava 2023-05-30 21:05:57 643人浏览 泡泡鱼
摘要

找别人提供的接口,比如在这里我选择的是聚合数据提供的接口要申请相应的AppKey方可使用,此参数会作为接口的参数调用。调用别人提供的接口方法代码如下:package juheapi.nba;import net.sf.JSON.jsONOb

找别人提供的接口,比如在这里我选择的是聚合数据提供的接口

要申请相应的AppKey方可使用,此参数会作为接口的参数调用。

调用别人提供的接口方法

代码如下:

package juheapi.nba;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;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";//配置您申请的KEYpublic 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,默认jsontry {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,默认jsonparams.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,默认jsonparams.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();}}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();}}

--结束END--

本文标题: Java编程实现NBA赛事接口调用实例代码

本文链接: https://lsjlt.com/news/221296.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作