import java.net.*;
import java.io.*;
import java.util.*;

/** 
 *   PlaceEngine API Sample in Java
 *   LocalDB APIs
 *   2007.6.1
 *   @author Jun Rekimoto
 **/
public class LocalTest {
    public static final String appk = "S7Rk0Bm3DXEG11uCRG2LHjCjNMwF55iE8BE7fc9Z3.dZXgOXZL8Q5rQQn7TUgY13yVDtIAKdZi3cAhoQFT4sUmdveq9k0TOaUfBR8-QtDXMdskouu4CZnm6.m2528z5U-KIy5LRxvnIUjoE3MvvRr1BMg86yFxWqI6GHNJU.Wysqf8YH78BUHJ8s5WQl6lYvMWj6cl2srvvNN5rc3btE21lJpgZR4Tu2FWW.DOQ7IzNy5o7mO3fr-kLbKEMy49Q.eVHRZQCpqQ4z5aLWyFdJLuyYu1OVqza.PIrUvtQU6ql-fgQ5wNaDYRGl5N8agB-pRgyWvCxJC.rJWmj1LZ67kQ__,amF2YQ__,UGxhY2VFbmdpbmUgVGVzdA__";

    public static String getURL(String urlstr) throws MalformedURLException, ProtocolException, IOException {
	URL url = new URL(urlstr);
	HttpURLConnection con = (HttpURLConnection)url.openConnection();
	con.connect();
	BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
	String s = "";
	for (String line = null; (line = reader.readLine()) != null; ) {
	    s += line;
	}
	reader.close();
	con.disconnect();
	return s;
    }

    public static void main(String[] argv) throws MalformedURLException, ProtocolException, IOException {
	long t = System.currentTimeMillis() / 1000;
	String res = null;
	String req = null;

	// get a list of local databases
        req = "http://localhost:5448/listdb?t=" + t + "&appk=" + appk;
	System.out.println("URL->" + req);
	res = getURL(req);
	System.out.println("Response->" + res + "\n");

	// get current location
        req = "http://localhost:5448/locdb?t=" + t + "&appk=" + appk;
	System.out.println("URL->" + req);
	res = getURL(req);
	System.out.println("Response->" + res + "\n");
	// you need JSON parsing to fetch latitude longitude values from 'res'
    }
}
