java 使用 Google map API 和 eclipse IDE 中的连接被拒绝错误,而相同的 URL 在浏览器中给出了结果

Connection refused error in java using Google map API with eclipse IDE while the same URL is giving me the result in browsers

我将下面的代码用于一个简单的 java 应用程序,使用 Google 地图 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;
   }
  }
 }
}

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

在调试模式下调试此代码时会引发错误提示 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,我会收到所需的 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);
}

感谢您的回复。