1

I have written a code to access Google API. The URL when hit from a browser gives immediate response but when hitting the same URl from Eclipse using a OkHttpClient leads to a connection time out. Have tried using various methods to hit the URL still its a connection time out every time. I even tried using HTTP instead of HTTPS but still no luck. I have no much knowledge in networking but it seems issue is due to office network since code works fine with my home network. Also I have tries setting a higher timeout value still it never connects. So please let me know what might be the probable reason and how to overcome the office proxy or whatever is creating the issue. Below is the code which I am using currently

public static String getAPIData(String finalURL) throws Exception {
    // TODO Auto-generated method stub

    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(finalURL).build();

    Response response = client.newCall(request).execute();
    return response.body().string();

}
Kunal
  • 11
  • 4
  • Check [this](https://stackoverflow.com/questions/15608499/getting-java-net-sockettimeoutexception-connection-timed-out-in-android) out . – Swati Mar 01 '19 at 05:01
  • https://stackoverflow.com/questions/5662283/java-net-connectexception-connection-timed-out-connect – Akash Shah Mar 01 '19 at 05:26
  • I have no connectivity issue or network slowness at my office. Also I have already tried setting the time out still it never connects. – Kunal Mar 01 '19 at 05:34
  • @Kunal https://stackoverflow.com/questions/54918971/not-able-to-download-a-remote-file-using-java#comment96605298_54918971 read comment may help you. – Akash Shah Mar 01 '19 at 05:57
  • @SHAHAKASH That comment is not showing how to get the thing working. They are just assumptions of network configuration. I want to learn how to make this thing working with some steps to the procedure. – Kunal Mar 04 '19 at 05:55

1 Answers1

1

I had the same issue before, and I solved it by setting Java VM arguments. I had some proxy setting in my network, so I set the same proxy in the Java VM arguments.

Click Run -> Run Configurations -> Click Arguments tab on the right -> Write on VM arguments:
-Dhttp.proxyHost=[your proxy server] -Dhttp.proxyPort=[your proxy port]

For example, if your PC proxy's server is exmaple.proxy.com, and port is 8080, just input below in the VM arguments:
-Dhttp.proxyHost=exmaple.proxy.com -Dhttp.proxyPort=8080