2

J2EE-App (サーバーサイド) から Facebook にアクセスする必要があります。私は最初にこのプロジェクトを見ました: http://code.google.com/p/facebook-java-api/ ですが、Facebook イベントを作成して人々を招待する必要があるため、これは役に立ちません。

したがって、Graph API を使用する必要があると思いますが、必要な HTTP POST 要求を実行する方法、特に nedded 属性を追加する方法についての手がかりがありません。

4

1 に答える 1

2

java.net.URLConnectionこれには を使用できます。

String url = "http://facebook.com/some/api";
String charset = "UTF-8";
String param1 = URLEncoder.encode("value1", charset);
String param2 = URLEncoder.encode("value2", charset);
String query = String.format("param1=%s&param2=%s", param1, param2);

URLConnection urlConnection = new URL(url).openConnection();
urlConnection.setUseCaches(false);
urlConnection.setDoOutput(true); // Triggers POST.
urlConnection.setRequestProperty("accept-charset", charset);
urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");

OutputStreamWriter writer = null;
try {
    writer = new OutputStreamWriter(urlConnection.getOutputStream(), charset);
    writer.write(query); // Write POST query string (if any needed).
} finally {
    if (writer != null) try { writer.close(); } catch (IOException logOrIgnore) {}
}

InputStream response = urlConnection.getInputStream();
// Now do your thing with the facebook response.

または、より便利なHttpClient APIを使用することもできます。

String url = "http://facebook.com/some/api";
String charset = "UTF-8";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("param1", "value1"));
params.add(new BasicNameValuePair("param2", "value2"));
UrlEncodedFormEntity query = new UrlEncodedFormEntity(params, charset);

HttpClient client = new DefaultHttpClient()
HttpPost post = new HttpPost(url);
post.setEntity(query);
InputStream response = client.execute(post).getEntity().getContent();
// Now do your thing with the facebook response.
于 2010-04-29T15:56:51.100 に答える