views:

41

answers:

2

I am planning to develop a very simple java application (not mobile, but desktop apps) that utilizes web services, specifically Foursquare API and Google Map Web Service. I plan to get the users address and then using Google Map to translate the address to lattitude and longitude then feed this result to foursquare API method Venue methods (Nearby and search) to get a results of nearby venues and show it to users. The question are:

  1. Is this all do able in Java?
  2. How do I call such http://api.foursquare.com/v1/venues in java and get the result? I know I can use Xpath to parse the result. I am just not sure on how to call those post methods in java and pass in parameters to it.
  3. Is there any sample java code that uses foursquare API out there?
  4. Will the foursquare API work if it's used not on mobile?

Sorry for all the noob questions.

+1  A: 
  1. Yes, it's possible, but I don't know the FourSquare API well enough to answer with 100% confidence.
  2. Make a URL connection to that URL and create a GET or POST request that conforms to their API.
  3. Google is your friend.
  4. You can certainly make HTTP requests. It's another matter what you do with the results.

UPDATE:

I'm not signed up at FourSquare. Try this out and let me know if it works for you:

package foursquare;

import java.io.*;
import java.net.Authenticator;
import java.net.URL;

/**
 * FourSquareDemo
 * User: Michael
 * Date: 10/19/10
 * Time: 7:53 PM
 */
public class FourSquareDemo
{
    private static final int DEFAULT_CAPACITY = 4096;
    private static final String DEFAULT_URL = "http://api.foursquare.com/v1/";
    private static final String DEFAULT_EMAIL = "[email protected]";
    private static final String DEFAULT_PASSWORD = "password";

    public static void main(String[] args)
    {
        long startTime = System.currentTimeMillis();
        long endTime = 0L;
        try
        {
            String downloadSite = ((args.length > 0) ? args[0] : DEFAULT_URL);
            URL url = new URL(downloadSite + "history");
            Authenticator.setDefault(new BasicAuthenticator(DEFAULT_EMAIL, DEFAULT_PASSWORD));
            String contents = readFromUrl(url);

            PrintStream ps = ((args.length > 1) ? new PrintStream(new FileOutputStream(new File(args[1]))) : System.out);
            printToStream(ps, contents);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            endTime = System.currentTimeMillis();
            System.out.println("wall time: " + (endTime - startTime) + " ms");
        }
    }

    private static void printToStream(PrintStream ps, String contents) throws IOException
    {
        ps.println(contents.toString());
        ps.close();
    }

    private static String readFromUrl(URL url) throws IOException
    {
        StringBuilder contents = new StringBuilder(DEFAULT_CAPACITY);

        BufferedReader br = null;

        try
        {
            InputStream is = url.openConnection().getInputStream();

            br = new BufferedReader(new InputStreamReader(is));
            String line;
            String newline = System.getProperty("line.separator");
            while ((line = br.readLine()) != null)
            {
                contents.append(line).append(newline);
            }
        }
        finally
        {
            try
            {
                if (br != null)
                {
                    br.close();
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

        return contents.toString();
    }
}
duffymo
I tried to google it already but can't find any... that's why I asked here
EquinoX
duffymo
they don't tell how specifically to call those using Java... it's just a bunch of documentation of methods that foursquare provides
EquinoX
That's what a REST API is. You have to use the URLConnection class that I told you about to connect to FourSquare.
duffymo
I am particularly looking just for a general example of the simplest way of consuming a REST API in Java without using any framework (JAX-RS, etc).
EquinoX
where did you get the foursquare package and the BasicAuthenticator seems to have the wrong parameter
EquinoX
I created the foursquare package. What makes you think BasicAuthenticator is wrong? It compiles and runs for me. I get an HTTP 401 response, which is correct. I'm not signed up to use FourSquare. You'll need to substitute your credentials, recompile, and run to try it.
duffymo
A: 

Foursqare API uses simple HTTP get methods (a.k.a RESTful web service). There are many ways to call RESTful web-services in Java, see:

yshalbar
Is there any way without using JAX-RS and just straight plain Java SE library
EquinoX