我试图从这个网站的Json API中读取口袋妖怪的名字:http://pokeapi.pokesnipe.de/
我尝试了一些东西,但我仍然无法让它为我的生活而工作。这是代码:
public static void main(String[] args) throws IOException {
String sURL = "http://pokeapi.pokesnipe.de/";
URL url = new URL(sURL);
HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.setRequestProperty("User-Agent", "Mozilla/5.0");
request.connect();
JsonParser jp = new JsonParser();
JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
JsonObject rootobj = root.getAsJsonObject();
String name = rootobj.get("name").getAsString();
System.out.println(name);
}
这是我收到的错误:
Exception in thread "main" java.net.UnknownServiceException: no content-type
at java.net.URLConnection.getContentHandler(Unknown Source)
at java.net.URLConnection.getContent(Unknown Source)
at pokeglass.RetrieveData.main(RetrieveData.java:24)
第24行写道:
JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
非常感谢任何帮助。
为什么将其标记为重复?那个帖子里甚至都没有答案..