4

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 ?

6
  • 1
    Please add the entire stack trace. Difficult to diagnose without. Commented Dec 4, 2012 at 8:01
  • I also need TroveService.java and CsvReader2.java, it gives you Nullpointer Exception. Commented Dec 4, 2012 at 8:07
  • CsvReader2 calls TroveService, which have these 2 functions, where the exception happens Commented Dec 4, 2012 at 8:09
  • What's value of timeout you are using? Commented Dec 4, 2012 at 8:10
  • Yes thanks. You are getting a NPE in your code. TroveService, line 134. What could be null there? Please also paste that class in here. Commented Dec 4, 2012 at 8:14

1 Answer 1

5

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:

  • first try and see how long it takes to get a full response when you try to access through the browser.
  • try and get rid of the timeout (the default is no timeout) and see if you ever get the response.

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.

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.