【发布时间】:2013-12-15 06:27:05
【问题描述】:
我正在试验 Riot Games API。我确定我的 URL 很好,我必须包含的 API 密钥也很好;我的请求没有理由返回 401。起初我以为它与 Eclipse 有关,但将 API 放入我的浏览器中也会返回 401(API 通常返回的 JSON 格式)。我知道有这方面的论坛,但我想也许这里有人可以指出我的程序是否有错误:
public class APITry {
public static void main(String[] args) throws IOException{
URL LOLAPI = new URL("https://prod.api.pvp.net/api/lol/na/v1.1/summoner/by-name/RiotSchmick&api_key=<key>"); //<key> is, of course, replaced with my key
BufferedReader in = new BufferedReader(new InputStreamReader(LOLAPI.openStream()));
StringBuilder build = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null)
build.append(in.toString());
in.close();
String INFO = build.toString();
JSONParser prse = new JSONParser();
try {
Object obj = prse.parse(in);
JSONObject PARSED = (JSONObject) obj;
String message = (String) PARSED.get("message");
int summonerID = (int) PARSED.get("losses");
System.out.println("message:" + message);
System.out.println("retrieved, is " + summonerID);
} catch (ParseException e) {
e.printStackTrace();
System.out.println("parse exception");
} catch(NullPointerException e) {
e.printStackTrace();
} catch(FileNotFoundException e) {
e.printStackTrace();
}
}
我的进口都算进去了。
【问题讨论】:
-
您确定不需要包含一些身份验证 http 标头,例如基本身份验证之类的吗?
-
不确定它是否也可以这样工作;您可以尝试使用其中一种 java riot-api 包装器
标签: java json api riot-games-api