Advertisement
Guest User

Untitled

a guest
Oct 28th, 2011
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. package scratch;
  2.  
  3. import com.google.gson.Gson;
  4. import org.apache.commons.io.IOUtils;
  5. import org.codehaus.jackson.map.ObjectMapper;
  6.  
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.net.URL;
  10. import java.util.concurrent.TimeUnit;
  11.  
  12.  
  13. public class Test2 {
  14.  
  15.     public static void main (String[] args) {
  16.         String url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=97401+OR|95124+CA&destinations=92626+CA&mode=driving&language=en-EN&units=imperial&sensor=false";
  17.        String str = getResponseByUrl(url);
  18.        
  19.        System.out.println(str);
  20.                               // bean = new Gson().fromJson(response, GeoCodeBean.class);
  21.         GeoZipCodesBean2 geoZipCodesBean2 = new Gson().fromJson(str, GeoZipCodesBean2.class);
  22.  
  23.  
  24.  
  25.          System.out.println(geoZipCodesBean2.getRows().size());
  26.          System.out.println(geoZipCodesBean2.getRows().get(0).getDistance().getText()); // null exception
  27.  
  28.         try {
  29.             TimeUnit.SECONDS.sleep(3);
  30.         } catch (InterruptedException e) {
  31.             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
  32.         }
  33. //        GeoZipCodesBean2 geoZipCodesBean21;
  34. //        ObjectMapper mapper = new ObjectMapper();
  35. //        try {
  36. //             geoZipCodesBean21 = mapper.readValue(str, GeoZipCodesBean2.class);
  37. //        } catch (IOException e) {
  38. //            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
  39. //        }
  40.  
  41.  
  42.  
  43.  
  44.     }
  45.  
  46.     private static String getResponseByUrl(String searchUrl) {
  47.         InputStream in = null;
  48.         try {
  49.             in = new URL(searchUrl).openStream();
  50.             return IOUtils.toString(in);
  51.         } catch (Exception e) {
  52.             e.printStackTrace();
  53.             return null;
  54.         }
  55.         finally {
  56.             IOUtils.closeQuietly(in);
  57.         }
  58.  
  59.     }
  60. }
  61.  
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement