How to read a JSON from a second link in an API?

2

Talk guys, I'm trying to get the JSON, from a second link that is provided by the API. But it gives error. Here is the code:

private static String URL = "https://google-play-api-znbelznpav.now.sh/api/apps/com.fungames.sniper3d/reviews/?lang=pt-br";

private void leitorJson(String url)
    throws URISyntaxException, JSONException, MalformedURLException, IOException {
    JSONTokener tokener = null;
    JSONObject principal = null;
    JSONArray arrayDeJson = null;
    String urlSegundaVez = null;
    URI urlAPI = new URI(url);
    do {
        if (urlSegundaVez == null) {
            tokener = new JSONTokener(urlAPI.toURL().openStream());

            principal = new JSONObject(tokener);

            arrayDeJson = principal.getJSONArray("results");
        } else {

            URI segundaURL = new URI(urlSegundaVez);

            tokener = new JSONTokener(segundaURL.toURL().openStream());

            principal = new JSONObject(tokener);

            arrayDeJson = principal.getJSONArray("results");
        }

        ArrayList<String> objetosJSON = new ArrayList<String>();

        int tamanho = arrayDeJson.length();

        for (int i = 0; i < tamanho; i++) {
            String novoObjtoJSON = arrayDeJson.getJSONObject(i).getString("text").replaceAll("Resenha completa",
                    " ");
            System.out.println(novoObjtoJSON.toString());
            objetosJSON.add(novoObjtoJSON);
        }
            tokener = null;
            arrayDeJson = null;
            urlSegundaVez = null;
            urlSegundaVez = principal.get("next").toString();
            principal = null;
    } while (urlSegundaVez != null);

}

But it gives you an error, when I get the JSON of the second link, which I get at:

urlSegundaVez = principal.get("next").toString();

I do not understand why it gives this error, since the JSON of the second link has the same format as the first one. The following error:

 org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
    at org.json.JSONTokener.syntaxError(JSONTokener.java:451)
    at org.json.JSONObject.<init>(JSONObject.java:195)
    at com.rayner.arhf.testes.Testes.leitorJson(Testes.java:59)
    at com.rayner.arhf.testes.Testes.main(Testes.java:33)
    
asked by anonymous 09.10.2016 / 17:03

1 answer

1

(Great big chance to be ignorant of me but ...) I've never read data from the internet directly by class URI, when I did a project that read JSON's I used the following method:

URL url = new URL("http://exemplo.com/arquivo.json");
Scanner scanner;
scanner = new Scanner(url.openStream());
String json = scanner.next();
while (scanner.hasNext()) json += " "+scanner.next();
return new JSONObject(json);
    
10.10.2016 / 01:40