ciao!
sto facendo qualche test con le librerie apache.
volevo interrogare un servizio rest, e fare il parsin del json.
il servizio è questo qui https://httpbin.org/get, che restituisce questo:
codice:
{
"args": {},
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.5",
"Cookie": "_ga=GA1.2.593513573.1485521352; _gat=1",
"Dnt": "1",
"Host": "httpbin.org",
"Referer": "https://httpbin.org/",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0"
},
"origin": "IP_ADDRESS",
"url": "https://httpbin.org/get"
}
l'ho anche validato su jsonlint.
con questo codice:
codice:
public class Main {
public static void main(String[] args) {
try {
String strUrl = "https://httpbin.org/get";
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(strUrl);
HttpResponse response = client.execute(request);
JSONArray array = new JSONArray(new JSONTokener(response.getEntity().getContent()));
System.out.println(array.toString());
// for (int i = 0; i < array.length(); i++) {
// JSONObject object = array.getJSONObject(i);
// System.out.println(object.getString("url"));
// }
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
ottengo questo errore:
codice:
Exception in thread "main" org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:451)
at org.json.JSONArray.<init>(JSONArray.java:108)
at com.mp.testmaven.Main.main(Main.java:23)
qualche idea??