Fala pessoal, to tentando ler um JSON, mas quando eu obtenho o segundo link e vou pegar o JSON desse link, ele quebra o mostra o seguinte erro :
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.(JSONObject.java:195)
at com.rayner.arhf.testes.Testes.leitorJson(Testes.java:59)
at com.rayner.arhf.testes.Testes.main(Testes.java:33)
Porém, quando é usado o primeiro link, ele não dá erro. E JSON do segundo link é igual ao do primeiro link.
Segue o código:
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());
//ele não chega em principal.
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);
}