四方API和Java [英] foursquare API and java

查看:103
本文介绍了四方API和Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


  1. 这是否能在Java中完成?

  2. 如何调用这样的在java中 http://api.foursquare.com/v1/venues 并获得结果?我知道我可以使用Xpath来解析结果。我只是不确定如何在java中调用这些后期
    方法并将参数传递给它。

  3. 是否有任何使用foursquare API的示例java代码? / li>
  4. 如果foursquare API不在手机上使用,它可以工作吗?

对不起,所有noob问题。

解决方案


  1. 是的,这是可能的,但我不太了解FourSquare API以100%的可信度回答。

  2. 建立与该URL的URL连接,并创建符合其API的GET或POST请求。是你的朋友。

  3. 你当然可以发出HTTP请求。 b


    更新:

    我没有在FourSquare注册。尝试一下,让我知道它是否适用于您:

      package foursquare; 

    import java.io. *;
    import java.net.Authenticator;
    import java.net.URL;
    $ b / **
    * FourSquareDemo
    *用户:Michael
    *日期:10/19/10
    *时间: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 =新网址(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;

    尝试
    {
    InputStream is = url.openConnection()。getInputStream();

    br = new BufferedReader(new InputStreamReader(is));
    字符串行;
    String newline = System.getProperty(line.separator); ((line = br.readLine())!= null)
    {
    contents.append(line).append(newline);


    finally

    try

    if(br!= null)
    {
    br.close();


    catch(IOException e)
    {
    e.printStackTrace();
    }
    }

    return contents.toString();
    }
    }


    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. 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 = "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();
        }
    }
    

    这篇关于四方API和Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆