朋友!!!我是Java的新手,我已经写了以下第一篇代码,以通过Overpass API获取数据。
但是Overpass API服务器没有响应Java_Servler_request(因此发生连接超时)。
主代码调用类
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));
类调用覆盖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