1

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

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Ashenguard
  • 174
  • 1
  • 10
  • https://mkyong.com/java/java-httpurlconnection-follow-redirect-example/ – OneCricketeer Jan 08 '21 at 19:34
  • I ran your code and it works: WebReader webReader = new WebReader("https://api.spiget.org/v2/authors/1001/"); webReader.readLines(); – i.merkurev Jan 08 '21 at 19:34
  • If you run `connection.getHeaderField("Location")`, you'll see it redirects you to the HTTP version of the site. See https://stackoverflow.com/questions/18431440/301-moved-permanently. – ifly6 Jan 08 '21 at 19:36
  • 1
    Does this answer your question? [301 Moved Permanently](https://stackoverflow.com/questions/18431440/301-moved-permanently) – ifly6 Jan 08 '21 at 19:37
  • @ifly6 No, unfortunately! I got the error again by using that method. – Ashenguard Jan 08 '21 at 19:51
  • @i.merkurev It works for 4 or 5 times but I have a list of 12 ids (Mostly duplicated but not sure what is the ID) And I get the error on the 6th to the 7th time usually – Ashenguard Jan 08 '21 at 19:53

0 Answers0