我正在尝试使用 gson 库从 url 获取 json 代码,并且我正在使用 apache commons io 来获取 url。但问题是,当我执行代码时,退出返回 403 错误。 我怎么解决这个问题? 这是代码:
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.io.IOUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class gsonexample {
public gsonexample() throws MalformedURLException, IOException {
String url = "http://freemusicarchive.org/api/get/albums.json?api_key=60BLHNQCAOUFPIBZ&limit=5";
String json = IOUtils.toString(new URL(url));
JsonParser parser = new JsonParser();
// The JsonElement is the root node. It can be an object, array, null or
// java primitive.
JsonElement element = parser.parse(json);
// use the isxxx methods to find out the type of jsonelement. In our
// example we know that the root object is the Albums object and
// contains an array of dataset objects
if (element.isJsonObject()) {
JsonObject albums = element.getAsJsonObject();
System.out.println(albums.get("title").getAsString());
JsonArray datasets = albums.getAsJsonArray("dataset");
for (int i = 0; i < datasets.size(); i++) {
JsonObject dataset = datasets.get(i).getAsJsonObject();
System.out.println(dataset.get("album_title").getAsString());
}
}
}
}
这里是导出:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: http://freemusicarchive.org/api/get/albums.json?api_key=60BLHNQCAOUFPIBZ&limit=5
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at java.net.URL.openStream(URL.java:1045)
at org.apache.commons.io.IOUtils.toString(IOUtils.java:1153)
at org.apache.commons.io.IOUtils.toString(IOUtils.java:1140)
at gestor.de.colecciones.gsonexample.<init>(gsonexample.java:24)
at gestor.de.colecciones.main.main(main.java:62)
... 11 more
Exception running application gestor.de.colecciones.main
Java Result: 1
gsonexample.java:24 是 String json = IOUtils.toString(new URL(url));
最佳答案
我也遇到了同样的问题,找到了你的问题。
其中一条评论说要使用
System.setProperty("http.agent", "curl/7.51.0").
所以我这样做了,但仍然遇到同样的错误。因此,我将代码中的 url(与我的代码完全相同)复制并粘贴到浏览器地址栏中,然后按 ENTER 键。回应是:
{"title":"Free Music Archive - Albums","message":"","errors":["invalid or disabled api_key"],"http_status":403,"dataset":[]}.
希望这有帮助。建议我们都研究 freemusicarchive.com 以找到有效和/或未禁用的 key 。
祝你好运,快乐计算。
关于java - 使用 apache commons io 时如何解决错误 403?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44861129/