Google Maps and java.net.SocketTimeoutException: Read timed out -
the code below has few issues. first, it's giving me sockettimeoutexception. working fine few days ago, it's throwing error. i'm not sure how trace problem source. tried increasing timeout (which don't think work in end), times out after 18 seconds. what's more strange url works when try getting json file else (such http://mysafeinfo.com/api/data?list=englishmonarchs&format=json). i've waited day , issue still there. there resolve 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 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 @ java.net.socketinputstream.socketread0(native method) @ java.net.socketinputstream.socketread(unknown source) @ java.net.socketinputstream.read(unknown source) @ java.net.socketinputstream.read(unknown source) @ sun.security.ssl.inputrecord.readfully(unknown source) @ sun.security.ssl.inputrecord.read(unknown source) @ sun.security.ssl.sslsocketimpl.readrecord(unknown source) @ sun.security.ssl.sslsocketimpl.readdatarecord(unknown source) @ sun.security.ssl.appinputstream.read(unknown source) @ java.io.bufferedinputstream.fill(unknown source) @ java.io.bufferedinputstream.read1(unknown source) @ java.io.bufferedinputstream.read(unknown source) @ sun.net.www.http.httpclient.parsehttpheader(unknown source) @ sun.net.www.http.httpclient.parsehttp(unknown source) @ sun.net.www.protocol.http.httpurlconnection.getinputstream0(unknown source) @ sun.net.www.protocol.http.httpurlconnection.getinputstream(unknown source) @ sun.net.www.protocol.https.httpsurlconnectionimpl.getinputstream(unknown source) @ sockettimeouterror.main(sockettimeouterror.java:38)
---edit---
i've been doing on work machine. tried code on home computer , works fine. also, company work has google work license. don't understand how network issue, though, between company , google servers, because i'm able view json in chrome browser.
i've figured out issue was. had connect through proxy (which our browsers automatically , explains why got response when used web browser). had modify code in following way:
proxy proxy = new proxy(proxy.type.http, new inetsocketaddress("proxy.url.com", 80)); urlconnection myurlconnection = myurl.openconnection(proxy);
Comments
Post a Comment