amolbabu / geocoder-java

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

Using http client 4.1 #2

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
Successful request method to use with google app engine using url fetch. (sorry 
somehow my browser created a duplicate bug). 

public GeocodeResponse geocode(final GeocoderRequest geocoderRequest) {

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

    String json = null;
    try {
      URL url = new URL(surl);
      BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
      String line;
      json = null;
      while ((line = reader.readLine()) != null) {
        if (json == null) {
          json = "";
        }
        json += line;
      }
      reader.close();
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

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

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

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

GoogleCodeExporter commented 9 years ago
Issue 3 has been merged into this issue.

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

GoogleCodeExporter commented 9 years ago

Original comment by panchmp on 3 Nov 2011 at 7:57