java - 使用带有 Eclipse IDE 的 Google map API 在 Java 中连接被拒绝错误,而相同的 URL 在浏览器中给我结果

标签 java eclipse exception google-maps-api-3 google-maps-api-2

我将下面的代码用于一个简单的 Java 应用程序,该应用程序使用 Google map API 来获取 JASON 响应。

import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.mapsengine.MapsEngine;
import com.google.api.services.mapsengine.MapsEngineRequestInitializer;
import com.google.api.services.mapsengine.model.Feature;
import com.google.api.services.mapsengine.model.FeaturesListResponse;
import com.google.api.services.mapsengine.model.GeoJsonPoint;
import java.io.IOException;

/** Java "Hello, world!" example using the client library */
public class HelloWorld {
  static final String SAMPLE_TABLE_ID = "12421761926155747447-06672618218968397709";
  static final String PUBLIC_API_KEY = "**MYAPIKEY**";

public static void main(String[] args) throws Exception {
    HttpTransport transport = new NetHttpTransport();
    JsonFactory jsonFactory = new GsonFactory();

    // This request initializer will ensure the API key is sent with every HTTP request.
MapsEngineRequestInitializer apiKeyInitializer =
    new MapsEngineRequestInitializer(PUBLIC_API_KEY);

MapsEngine engine = new MapsEngine.Builder(transport, jsonFactory, null)
    .setMapsEngineRequestInitializer(apiKeyInitializer)
    .setApplicationName("Google-MapsEngineSample/1.0")
    .build();

readFeaturesFromTable(engine);
  }

  public static void readFeaturesFromTable(MapsEngine me) throws IOException {
// Query the table for offices in WA that are within 100km of Perth.
FeaturesListResponse featResp =  me.tables().features().list(SAMPLE_TABLE_ID)
    .setVersion("published")
    .setWhere("State='WA' AND ST_DISTANCE(geometry,ST_POINT(115.8589,-31.9522)) < 100000")
    .execute();

for (Feature feat : featResp.getFeatures()) {
  System.out.println(
      "Properties: " + feat.getProperties().toString() + "\n\t" +
      "Name: " + feat.getProperties().get("Fcilty_nam") + "\n\t" +
      "Geometry Type: " + feat.getGeometry().getType());

  if (feat.getGeometry() instanceof GeoJsonPoint)  {
    GeoJsonPoint point = (GeoJsonPoint) feat.getGeometry();
    System.out.println("\t" +
        "Longitude: " + point.getCoordinates().get(0) + ", " +
        "Latitude: " +  point.getCoordinates().get(1));
  } else {
    System.out.println("Only points are expected in this table!");
    return;
   }
  }
 }
}

我在这方面没有错误,因为我已经按照每条说明为谷歌地图 API 配置 eclipse IDE 并使用 jar 进行编译。 (通过引用此 Link 。)

在 Debug模式下调试此代码时会引发错误提示 java.net.ConnectException:连接被拒绝:连接 来自代码

FeaturesListResponse featResp = me.tables().features().list(SAMPLE_TABLE_ID) .setVersion("published") .setWhere("State='WA' AND ST_DISTANCE(geometry,ST_POINT(115.8589,-31.9522)) < 100000") .execute();

由此产生的URL是

"https://www.googleapis.com/mapsengine/v1/tables/12421761926155747447-06672618218968397709/features?key=APIKEY&version=published&where=State%3D'WA'%20AND%20ST_DISTANCE(geometry,ST_POINT(115.8589,-31.9522))%20%3C%20100000"

如果我将 URL 粘贴到某些浏览器上,我会收到 Needed JASON 响应。

相同的代码在不同的环境中运行。

帮我解决这个问题。

谢谢。

最佳答案

我通过在发送请求之前在 HttpTransport 上设置代理解决了这个问题。

所以我在发送请求之前创建HttpTransport类型的对象之前设置了代理。

代码如下

public static void main(String[] args) throws Exception {
  Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("YOUR PROXY IP", 80));   
HttpTransport transport = new NetHttpTransport.Builder().setProxy(proxy).build();
JsonFactory jsonFactory = new GsonFactory();
// This request initializer will ensure the API key is sent with every HTTP request.
MapsEngineRequestInitializer apiKeyInitializer =
    new MapsEngineRequestInitializer(PUBLIC_API_KEY);
}

感谢您的回复。

关于java - 使用带有 Eclipse IDE 的 Google map API 在 Java 中连接被拒绝错误,而相同的 URL 在浏览器中给我结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27838607/

相关文章:

eclipse - 在 Eclipse 中查找替换热键

java - Android Volley imageloader 下载图像时进度百分比

尝试使用 MongoDB BulkWriteOperation 时出现 java.lang.IllegalStateException

c# - 处理基类异常

java - jboss 5.1中seam和mdb的问题

java - 正则表达式匹配一个词而不是另一个词

java - 使用 AWS Lambda 从 S3 上的目录创建 Tar 存档

java - 如何让 Eclipse 智能感知自动完成成员名称?

c# - 比较没有实例的类类型

java - 使用 RestFB 时出现 Unicode 错误