0

Friends !!! I am new to Java & I have written my first following coding to get data thru Overpass API.

Problem Statement

But Overpass API server is not responding to Java_Servler_request (Hence Connection Timeout happens).

My Trials

  1. I tried thru Chrome Manually & I am receiving data. It means Overpass Server is working fine.
  2. I tried to call localhost (instead of Overpass API), it is responding thru OkHttp3. It means OkHttp3 is working fine.

Main code calling class

URL="http://overpass-api.de/api/interpreter";
value ="?data=way(around:100,22.36432,73.19266)[power~\"\"];(._;>;);out;";
HttpCaller API =new HttpCaller();
response.getWriter().println(API.APIGet(URL+value));

Class Calling Overpass API

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HttpCaller
{
    OkHttpClient client;
    String rspns;

    public HttpCaller()
    {
        try
        {
            client = new OkHttpClient();
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            builder.connectTimeout(30, TimeUnit.SECONDS); 
            builder.readTimeout(30, TimeUnit.SECONDS); 
            builder.writeTimeout(30, TimeUnit.SECONDS); 
            client = builder.build();
        }
        catch (Exception e) {e.printStackTrace();System.out.println(e);}
    }

    public String APIGet(String url) throws IOException
    {       
        Request request = new Request.Builder()
            .url(url)
            .build();
        System.out.println(request.url());

        try 
        { 
            Response response = null;
            System.out.println("new Call...");
            response = client.newCall(request).execute();
            System.out.println(response.code());
            rspns =  response.body().string(); 
        } catch (Exception e) {e.printStackTrace();System.out.println("Exception is "+e);} 

        return rspns;
    }
}

Result thrown

http://overpass-api.de/api/interpreter?data=way(around:100,22.36432,73.19266)[power~%22%22];(._;%3E;);out;
new Call...
java.net.ConnectException: Failed to connect to overpass-api.de/178.63.48.217:80
    at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:247)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:165)
    at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257)
    at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
    at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)Exception is java.net.ConnectException: Failed to connect to overpass-api.de/178.63.48.217:80
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
    at okhttp3.RealCall.execute(RealCall.java:77)

Testing URL on Chrome My Desktop Snap

Amit Panasara
  • 600
  • 8
  • 16

1 Answers1

0

I solved my problem by setting the proxy setting for the request through OkHttp.

Source : OkHTTPClient Proxy authentication how to?

Amit Panasara
  • 600
  • 8
  • 16
  • 1
    This looks like a mobile app. Please be aware that there's a usage policy for Overpass API you need to adhere to, limiting the maximum number of requests per day. You could experience similar error message in case you get blocked by exceeding your quota. – mmd Sep 18 '18 at 19:46
  • i am learning + developing a concept (not full fledge app)... whenever the full-fledge app will be rolled out, it will be definitely with commercial licences for APIs – Amit Panasara Sep 24 '18 at 06:09
  • This is open source, there are no commercial licenses. However, if you exceed limits, you need to switch to your own infrastructure. – mmd Sep 24 '18 at 08:20