-2

I'm trying to read date from JSON using a URL , but it throws an exception. and the response from header is 301 Moved Permanently

this basically means the there is something wrong with link, but I cannot figure out what it is exactly since the API provider did not mention anything in regard to changes to their API

Any help is appreciated.

here is my code

private static String readUrl(String urlString) throws Exception {
        BufferedReader reader = null;
        try {
            URL url = new URL(urlString);
            reader = new BufferedReader(new InputStreamReader(url.openStream()));
            StringBuffer buffer = new StringBuffer();
            int read;
            char[] chars = new char[1024];
            while ((read = reader.read(chars)) != -1)
                buffer.append(chars, 0, read);

            return buffer.toString();
        } finally {
            if (reader != null)
                reader.close();
        }
    }
String url = "http://webservice.fanart.tv/v3/movies/" + movie.id + "?api_key=" + apikey;
        List<String> enClearLogos = new ArrayList<>();
        try {
            JSONObject json = new JSONObject(readUrl(url));
            JSONArray jsonArray = json.getJSONArray("hdmovielogo");
            for(int i = 0; i < jsonArray.length(); i++){
                JSONObject movieObject = jsonArray.getJSONObject(i);
                if (movieObject.getString("lang").equalsIgnoreCase("en"))
                    enClearLogos.add(movieObject.getString("url"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

here example

{
    "name": "The Bad Guys",
    "tmdb_id": "629542",
    "imdb_id": "tt8115900",
    "hdmovieclearart": [
        {
            "id": "362695",
            "url": "https://assets.fanart.tv/fanart/movies/629542/hdmovieclearart/the-bad-guys-6231fd4379a87.png",
            "lang": "en",
            "likes": "6"
        },
        {
            "id": "362696",
            "url": "https://assets.fanart.tv/fanart/movies/629542/hdmovieclearart/the-bad-guys-6231fd53d3a96.png",
            "lang": "ru",
            "likes": "3"
        },
        {
            "id": "367992",
            "url": "https://assets.fanart.tv/fanart/movies/629542/hdmovieclearart/the-bad-guys-6280fb431126b.png",
            "lang": "cz",
            "likes": "2"
        }
    ],
    "hdmovielogo": [
        {
            "id": "369396",
            "url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-629dc8e4da93a.png",
            "lang": "fr",
            "likes": "3"
        },
        {
            "id": "368375",
            "url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-62899cfba2c00.png",
            "lang": "en",
            "likes": "3"
        },
        {
            "id": "371110",
            "url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-62bdec1d38353.png",
            "lang": "es",
            "likes": "2"
        },
        {
            "id": "355025",
            "url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-61ba168a41d9b.png",
            "lang": "ru",
            "likes": "2"
        },
        {
            "id": "355031",
            "url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-61ba1f851cd32.png",
            "lang": "en",
            "likes": "2"
        },
        {
            "id": "355032",
            "url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-61ba1f96a497e.png",
            "lang": "en",
            "likes": "1"
        },
        {
            "id": "366828",
            "url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-626984d807f4d.png",
            "lang": "cz",
            "likes": "1"
        },
        {
            "id": "360922",
            "url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-6217a7c5ba60d.png",
            "lang": "it",
            "likes": "0"
        }
    ],
    "movieposter": [
        {
            "id": "367357",
            "url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-6274ea20dcc86.jpg",
            "lang": "en",
            "likes": "2"
        },
        {
            "id": "371114",
            "url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-62bdecfbd6715.jpg",
            "lang": "es",
            "likes": "2"
        },
        {
            "id": "368000",
            "url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-628108a944560.jpg",
            "lang": "pt",
            "likes": "2"
        },
        {
            "id": "367358",
            "url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-6274ea33e2ed5.jpg",
            "lang": "00",
            "likes": "2"
        },
        {
            "id": "355026",
            "url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-61ba169a2b57e.jpg",
            "lang": "en",
            "likes": "2"
        },
        {
            "id": "358210",
            "url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-61ee6f9a14706.jpg",
            "lang": "00",
            "likes": "1"
        },
        {
            "id": "367991",
            "url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-6280fb200c5bf.jpg",
            "lang": "cz",
            "likes": "0"
        },
        {
            "id": "367990",
            "url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-6280fb1ba6d39.jpg",
            "lang": "cz",
            "likes": "0"
        }
    ],
    "moviethumb": [
        {
            "id": "371117",
            "url": "https://assets.fanart.tv/fanart/movies/629542/moviethumb/the-bad-guys-62bdee5332ad2.jpg",
            "lang": "es",
            "likes": "2"
        },
        {
            "id": "367994",
            "url": "https://assets.fanart.tv/fanart/movies/629542/moviethumb/the-bad-guys-6280fb7b224fd.jpg",
            "lang": "cz",
            "likes": "1"
        },
        {
            "id": "370972",
            "url": "https://assets.fanart.tv/fanart/movies/629542/moviethumb/the-bad-guys-62bbbc9385121.jpg",
            "lang": "en",
            "likes": "0"
        },
        {
            "id": "367260",
            "url": "https://assets.fanart.tv/fanart/movies/629542/moviethumb/the-bad-guys-6272bfb851ca5.jpg",
            "lang": "en",
            "likes": "0"
        }
    ],
    "moviebackground": [
        {
            "id": "355027",
            "url": "https://assets.fanart.tv/fanart/movies/629542/moviebackground/the-bad-guys-61ba16ab7df92.jpg",
            "lang": "",
            "likes": "2"
        },
        {
            "id": "367177",
            "url": "https://assets.fanart.tv/fanart/movies/629542/moviebackground/the-bad-guys-6271f6e99b835.jpg",
            "lang": "",
            "likes": "0"
        }
    ],
    "moviedisc": [
        {
            "id": "367993",
            "url": "https://assets.fanart.tv/fanart/movies/629542/moviedisc/the-bad-guys-6280fb602b271.png",
            "lang": "cz",
            "likes": "1",
            "disc": "1",
            "disc_type": "bluray"
        },
        {
            "id": "358213",
            "url": "https://assets.fanart.tv/fanart/movies/629542/moviedisc/the-bad-guys-61ee85436574f.png",
            "lang": "en",
            "likes": "0",
            "disc": "1",
            "disc_type": "bluray"
        }
    ],
    "moviebanner": [
        {
            "id": "358216",
            "url": "https://assets.fanart.tv/fanart/movies/629542/moviebanner/the-bad-guys-61ee93348cb62.jpg",
            "lang": "en",
            "likes": "1"
        },
        {
            "id": "370973",
            "url": "https://assets.fanart.tv/fanart/movies/629542/moviebanner/the-bad-guys-62bbbd7215d83.jpg",
            "lang": "en",
            "likes": "0"
        }
    ]
}

exception message

W/System.err: org.json.JSONException: Value <html> of type java.lang.String cannot be converted to JSONObject
W/System.err:     at org.json.JSON.typeMismatch(JSON.java:112)
W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:168)
W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:181)
6
  • Please include the full exception message and stack trace in the question text Commented Oct 29, 2022 at 23:18
  • Obviously, JSON you're trying to parse is invalid. Try to print it on the console. Commented Oct 29, 2022 at 23:34
  • it is the exact same thing on the console. I just did curl 'https://webservice.fanart.tv/v3/movies/629542?api_key=xxxxxxxxxxxxxxx Commented Oct 29, 2022 at 23:36
  • yes just like that , so what should it look like normally? Commented Oct 29, 2022 at 23:43
  • 1
    Sorry, my bad, the sample-JSON is valid, it should be parsed fine (I've tried it). But exception should have a reason, either something is missing or something gets added to it. Commented Oct 29, 2022 at 23:52

1 Answer 1

0

The http:// causes url redirection and throws this message in the header response (301 Moved Permanently) so I had to use https:// instead now everything works nicely.

more info here Response 301 Moved Permanently

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.