【问题标题】:java.net.ConnectException: Failed to connect to overpass-api.de/178.63.48.217:80java.net.ConnectException:无法连接到 overpass-api.de/178.63.48.217:80
【发布时间】:2018-09-15 07:49:14
【问题描述】:

朋友们!!!我是 Java 新手,我已经编写了我的第一个以下代码,以通过 Overpass API 获取数据。

问题陈述

但是 Overpass API 服务器没有响应 Java_Servler_request(因此发生连接超时)。

我的试炼

  1. 我通过 Chrome 手动尝试过,我正在接收数据。这意味着 Overpass Server 工作正常。
  2. 我尝试调用 localhost(而不是 Overpass API),它通过 OkHttp3 响应。这意味着 OkHttp3 工作正常。

主代码调用类

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));

类调用 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;
    }
}

结果抛出

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)

在 Chrome 上测试网址 My Desktop Snap

【问题讨论】:

    标签: java okhttp3 overpass-api


    【解决方案1】:

    我通过 OkHttp 为请求设置代理设置...这解决了我的问题

    来源:OkHTTPClient Proxy authentication how to?

    【讨论】:

    • 这看起来像一个移动应用程序。请注意,您需要遵守 Overpass API 的使用政策,限制每天的最大请求数。如果您因超出配额而被阻止,您可能会遇到类似的错误消息。
    • 我正在学习 + 开发一个概念(不是完整的应用程序).​​.....无论何时推出完整的应用程序,它肯定会获得 API 的商业许可
    • 这是开源的,没有商业许可。但是,如果超出限制,则需要切换到自己的基础架构。
    猜你喜欢
    • 2018-11-02
    • 2016-01-02
    • 2016-08-13
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-30
    相关资源
    最近更新 更多