激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java編程實現NBA賽事接口調用實例代碼

Java編程實現NBA賽事接口調用實例代碼

2021-02-07 17:01笑容刺眼 Java教程

這篇文章主要介紹了Java編程實現NBA賽事接口調用實例代碼,具有一定參考價值,需要的朋友可以了解下。

第一步:找別人提供的接口,比如在這里我選擇的是聚合數據提供的接口

第二步:要申請相應的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

Java編程實現NBA賽事接口調用實例代碼

解決辦法:查看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賽事接口調用實例代碼

總結

以上就是本文關于java編程實現nba賽事接口調用實例代碼的全部內容,希望對大家有所幫助。

原文鏈接:https://www.2cto.com/kf/201711/699181.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美日韩精品一二三区 | 国产色91| 日韩精品久久久久久久电影99爱 | 撅高 自己扒开 调教 | 午夜视频在线 | 一级黄色在线免费观看 | 干一夜综合 | 免费黄色欧美视频 | 91短视频在线观看 | 亚洲成人自拍电影 | 国产午夜亚洲精品 | 国产成人高清成人av片在线看 | 强伦女教师视频 | 斗破苍穹在线免费 | japanese xxxxhd| 久久久成人一区二区免费影院 | 国产精品免费大片 | 精品亚洲二区 | a免费视频 | 久久久国产视频 | 黄色网页在线观看 | 亚洲 综合 欧美 动漫 丝袜图 | 久久影院免费观看 | 欧美成年私人网站 | 污版视频在线观看 | 久久影院国产精品 | 97干色 | 九九黄色 | 小视频免费在线观看 | 中文字幕欧美在线 | 久夜草 | 毛片在哪看 | 一区二区三区视频在线观看 | 天天看夜夜爽 | 黄色免费电影网址 | 欧美日韩夜夜 | 国产一级爱c视频 | 日本精品婷婷久久爽一下 | 激情视频免费看 | 欧美日韩经典在线 | 国产精品久久久久久久久久三级 |