Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package scratch;
- import com.google.gson.Gson;
- import org.apache.commons.io.IOUtils;
- import org.codehaus.jackson.map.ObjectMapper;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.URL;
- import java.util.concurrent.TimeUnit;
- public class Test2 {
- public static void main (String[] args) {
- 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";
- String str = getResponseByUrl(url);
- System.out.println(str);
- // bean = new Gson().fromJson(response, GeoCodeBean.class);
- GeoZipCodesBean2 geoZipCodesBean2 = new Gson().fromJson(str, GeoZipCodesBean2.class);
- System.out.println(geoZipCodesBean2.getRows().size());
- System.out.println(geoZipCodesBean2.getRows().get(0).getDistance().getText()); // null exception
- try {
- TimeUnit.SECONDS.sleep(3);
- } catch (InterruptedException e) {
- e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
- }
- // GeoZipCodesBean2 geoZipCodesBean21;
- // ObjectMapper mapper = new ObjectMapper();
- // try {
- // geoZipCodesBean21 = mapper.readValue(str, GeoZipCodesBean2.class);
- // } catch (IOException e) {
- // e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
- // }
- }
- private static String getResponseByUrl(String searchUrl) {
- InputStream in = null;
- try {
- in = new URL(searchUrl).openStream();
- return IOUtils.toString(in);
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- finally {
- IOUtils.closeQuietly(in);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement