Google Maps and java.net.SocketTimeoutException: Read timed out

2k Views Asked by At

The code below has a few issues. First, it's giving me a SocketTimeoutException. This was working just fine a few days ago, but now it's throwing this error. I'm not sure how to trace the problem to the source. I tried increasing the timeout (which I really don't think will work in the end), but it always times out after 18 seconds. What's more strange is that the URL works when I try getting a JSON file from something else (such as http://mysafeinfo.com/api/data?list=englishmonarchs&format=json). I've waited about a day and the issue is still there. Is there anything I could do to resolve this issue?

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Timer;
import java.util.TimerTask;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class SocketTimeoutError {

    public static void main(String args[]) {
        URL myURL;
        TimerTask task = null;
        try {
            myURL = new URL(
                    "https://maps.googleapis.com/maps/api/geocode/json?address=Chicago+Illinois");

            URLConnection conn = myURL.openConnection();
            conn.connect();
            conn.setConnectTimeout(4 * 600000);
            conn.setReadTimeout(4 * 600000);
            //
            Timer timer = new Timer();
            task = new TimerTask() {
                int time = 0;

                public void run() {
                    System.out.println(time++);
                }
            };
            //
            System.out.println("Connecting..." + "Timeout="
                    + conn.getConnectTimeout());
            timer.scheduleAtFixedRate(task, 0, 1000);
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    conn.getInputStream()));
            task.cancel();
            JSONParser parser = new JSONParser();
            JSONObject jsonObject = (JSONObject) parser.parse(in);
            String status = (String) jsonObject.get("status");
            System.out.println(status);

        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

}

Here's my console output:

Connecting...Timeout=2400000
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at sun.security.ssl.InputRecord.readFully(Unknown Source)
    at sun.security.ssl.InputRecord.read(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readDataRecord(Unknown Source)
    at sun.security.ssl.AppInputStream.read(Unknown Source)
    at java.io.BufferedInputStream.fill(Unknown Source)
    at java.io.BufferedInputStream.read1(Unknown Source)
    at java.io.BufferedInputStream.read(Unknown Source)
    at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
    at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at SocketTimeoutError.main(SocketTimeoutError.java:38)

---Edit---
I've been doing this on my work machine. I just tried this code on my home computer and it works just fine. Also, the company I work for has a Google for Work license. I don't understand how it could be a network issue, though, between the company and the google servers, because I'm able to view the json in my Chrome browser.

1

There are 1 best solutions below

0
On BEST ANSWER

I've figured out what the issue was. I had to connect through a proxy (which our browsers automatically do and explains why I got a response when I used a web browser). I just had to modify my code in the following way:

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.url.com", 80));
URLConnection myURLConnection = myURL.openConnection(proxy);