fesaab / geocoder-java

Automatically exported from code.google.com/p/geocoder-java
0 stars 0 forks source link

Using http client 4.1 #3

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Just a suggestion in using apache client 4.1. I haven't tested this throughly 
yet, b/c I can't use your request method on google app engine. I'm setting up a 
URL Fetcher to do the json request. 

public GeocodeResponse geocode(final GeocoderRequest geocoderRequest) {

    String url = null;
    try {
      url = getURL(geocoderRequest);
    } catch (UnsupportedEncodingException e1) {
      e1.printStackTrace();
    }

    if (url == null) {
      System.err.println("Geocoder.geocode(GeocoderRequest): Error url null");
      return null;
    }

    HttpClient client = new DefaultHttpClient();
    client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    HttpGet post = new HttpGet(url);

    //debug
    System.out.println("executing request " + post.getRequestLine());

    HttpResponse response = null;
    try {
      response = client.execute(post);
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    HttpEntity resEntity = response.getEntity();

    String json = "";
    if (resEntity != null) {
      try {
        json = EntityUtils.toString(resEntity); // this has a limit, but is one way to do it.
      } catch (ParseException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    client.getConnectionManager().shutdown();

    final Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();

    return gson.fromJson(json, GeocodeResponse.class);
  }

Thanks for the library. 
Brandon Donnelson
http://gwt-examples.googlecode.com

Original issue reported on code.google.com by branflak...@gmail.com on 6 Feb 2011 at 8:01

GoogleCodeExporter commented 9 years ago
sorry somehow my browser created this duplicate bug of issue 2

Original comment by branflak...@gmail.com on 6 Feb 2011 at 8:09

GoogleCodeExporter commented 9 years ago

Original comment by panchmp on 19 Mar 2011 at 5:21