I get this error :
java.net.SocketTimeoutException: Read timed out
http://api.trove.nla.gov.au/result?key=<KEY HERE>&zone=all&reclevel=brief&q=%20am%20only%20yours%20fear&encoding=json
Exception in thread "main" java.lang.NullPointerException at
trove.connect.TroveService.QueryTrove(TroveService.java:134) at
datasetconverter.CsvReader2.main(CsvReader2.java:64) Java Result: 1
BUILD SUCCESSFUL (total time: 1 minute 41 seconds)
When I execute my code:
public String getJSON(String url, int timeout) throws IOException, Exception {
try {
URL u = new URL(url);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setRequestProperty("Content-length", "0");
c.setUseCaches(false);
c.setAllowUserInteraction(false);
c.setConnectTimeout(timeout);
c.setReadTimeout(timeout);
c.connect();
int status = c.getResponseCode();
switch (status) {
case 504:
case 503:
Thread.sleep(10000);
this.QueryTrove(url);
break;
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
br.close();
return sb.toString();
}
} catch (MalformedURLException ex) {
System.out.println(ex);
System.out.println(url);
} catch (IOException ex) {
System.out.println(ex);
System.out.println(url);
}
return null;
}
public int QueryTrove(String query) throws Exception{
int sum =0;
String json = getJSON("http://api.trove.nla.gov.au/result?key=<KEY HERE>&zone=all&reclevel=brief&q="+query+"&encoding=json",10000);
Gson gson = new Gson();
JsonResponse r = gson.fromJson(json, JsonResponse.class);
for (Zone zone : r.getResponse().getZones()) {
sum = sum + Integer.parseInt(zone.records.getTotal());
}
return sum;
}
Is this because of the setConnectionTimeout and how can this be fixed ?
Read timeout happens because the server took longer then the “read-timeout” parameter to respond.
But assuming you have a reasonable read-timeout value (let’s say 30 seconds), it doesn’t have to mean that the problem is that your read-timeout is too short, it could be that the server is stuck and would never return an answer.
To diagnose there are several things that you can do:
If the server answers your browser and not your java app, then first make sure that you are calling the exact same URL. Other then that it’s possible the server is ignoring you due to your USER-AGENT, try and impersonate a real browser.
Also just a friendly advice, I would pass the timeout as a parameter, just define a reasonable timeout and use it directly, unless its a very unusual use-case, no body wants to pass the timeout when using this kind of abstraction.