I'm trying to get data from this link: https://api.spiget.org/v2/authors/1001/
Which you can see it contains some JSON data. But When I try to read it I get this:
<html>
<head><title>301 Moved Permanently</title><script src="/cdn-cgi/apps/head/FuNWa0bpYJxeKtykPLzC2_Mj2DQ.js"></script></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.4.6 (Ubuntu)</center>
</body>
</html>
My code is this:
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
public class WebReader {
private String url;
public WebReader(String url) {
this.url = url;
}
public List<String> readLines() {
List<String> result = new ArrayList<>();
try {
URLConnection con = new URL(url).openConnection();
BufferedReader buffer = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line = buffer.readLine()) != null)
result.add(line);
} catch (IOException ignored) {}
return result;
}
public String read() {
List<String> lines = readLines();
StringBuilder result = new StringBuilder();
for (String line: lines) {
result.append(line.replace("\n", "")).append("\n");
}
return result.toString();
}
public JSONObject readJSON() {
String data = read();
System.out.println(url); // To check the link
System.out.println(data); // To check data
return new JSONObject(data);
}
}
This is working for other IDs like 661860 but as soon as I reach 1001 it throws an exception.
EDIT ----------------------------------------------------------------
Now it is not even working for other IDs