Search code examples
javaeclipseexceptiongoogle-maps-api-3google-maps-api-2

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


I am using the bellow code for a simple java application using Google map APIs to get a JASON response.

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

I have no error in this, as i have followed every instruction to configure the eclipse IDE for Google map APIs and used jars for compiling. (By referring this Link.)

While Debugging this code in debugging mode it raises the error saying java.net.ConnectException: Connection refused: connect from the code

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

the URL produced by this is

"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"

If I paste the URL on some browser I am getting the Needed JASON response.

The same code is running in different environment.

Help me in solving this.

Thanks.


Solution

  • I solved the problem by setting proxy on the HttpTransport before sending the request.

    So I set the proxy before creating the object of type HttpTransport before sending the request.

    The code is as follows

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

    Thanks for the responses.