繁体   English   中英

Foursquare API和Java

[英]foursquare API and java

我打算开发一个利用Web服务(特别是Foursquare API和Google Map Web Service)的非常简单的Java应用程序(不是移动应用程序,而是桌面应用程序)。 我计划获取用户地址,然后使用Google Map将地址转换为纬度和经度,然后将此结果提供给foursquare API方法Venue方法(附近和搜索)以获取附近场所的结果并将其显示给用户。 问题是:

  1. 这一切都可以用Java完成吗?
  2. 如何在Java中调用此类http://api.foursquare.com/v1/venues并获得结果? 我知道我可以使用Xpath解析结果。 我只是不确定如何在Java中调用这些post方法并将参数传递给它。
  3. 有没有使用Foursquare API的示例Java代码?
  4. 如果在非移动设备上使用foursquare API,它将起作用吗?

对不起所有菜鸟问题。

  1. 是的,这是可能的,但是我对FourSquare API不够了解,无法以100%的置信度回答。
  2. 建立与该URL的URL连接,并创建一个符合其API的GET或POST请求。
  3. Google是您的朋友。
  4. 您当然可以发出HTTP请求。 您如何处理结果是另一回事。

更新:

我尚未在FourSquare注册。 试试看,让我知道它是否适合您:

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 = "you@gmail.com";
    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();
    }
}

Foursqare API使用简单的HTTP get方法(又名RESTful Web服务)。 有许多方法可以用Java调用RESTful Web服务,请参阅:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM