- 是的,这是可能的,但我对 FourSquare API 的了解还不够好,无法以 100% 的信心回答。
- 与该 URL 建立 URL 连接并创建符合其 API 的 GET 或 POST 请求。
- 谷歌是你的朋友。
- 您当然可以发出 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();
}
}