Android:Facebook将cURL应用到java



我一直在尝试将cURL命令翻译成java代码,以便创建一个新的AppLink对象:https://developers.facebook.com/docs/applinks/hosting-api

我下载了cURL,然后在Windows中键入以下内容,这很有效,并返回了一个applink ID:

curl-k-ghttps://graph.facebook.com/app/app_link_hosts-Faccess_token="插入我自己的应用程序_token"-F name="Android应用程序链接对象示例"-Fandroid='[{"url":"sharesample://story/1234","package":"com.facebook.samples.sharesample","app_name":"sharesample",},]'-F web='"should_fallback":false,}'

有人知道如何将curl代码转换为Java,以便我可以在服务器上使用它吗?

此外,我想知道是否有一种方法可以查询为特定包名称创建的所有应用程序链接,这样我就可以看到创建的所有内容?

谢谢!

我花了几个小时研究这个问题,最终发现了这段1997年的代码,我认为它可能不再有效,因为方法被弃用,并为facebook应用程序修改了它:http://www.javaworld.com/article/2077532/learn-java/java-tip-34--posting-via-java.html

然后我用Spring生成了这个端点,现在它工作了,并返回了一个应用程序链接id:

@RequestMapping(value="/applink", method=RequestMethod.GET)
public void applink() {
URL url;
URLConnection urlConn;
DataOutputStream printout;
DataInputStream input;
try {
url = new URL ("https://graph.facebook.com/app/app_link_hosts");
// URL connection channel.
urlConn = url.openConnection();
// Let the run-time system (RTS) know that we want input.
urlConn.setDoInput (true);
// Let the RTS know that we want to do output.
urlConn.setDoOutput (true);
// No caching, we want the real thing.
urlConn.setUseCaches (false);
// Specify the content type.
urlConn.setRequestProperty
("Content-Type", "application/x-www-form-urlencoded");
// Send POST output.
printout = new DataOutputStream (urlConn.getOutputStream ());
String content =
"access_token=" + URLEncoder.encode("INSERT APP ACCESS TOKEN", "UTF-8") +
"&name=" + URLEncoder.encode("Android App Link Object Example", "UTF-8") +
"&android=" + URLEncoder.encode("[{'url':'sharesample://story/1234', 'package':'com.facebook.samples.sharesample','app_name':'ShareSample'}]", "UTF-8") +
"&web=" + URLEncoder.encode("{'should_fallback' : false}", "UTF-8");
printout.writeBytes(content);
printout.flush ();
printout.close ();
// Get response data.
input = new DataInputStream (urlConn.getInputStream ());
BufferedReader d = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
String str;
while (null != ((str = d.readLine())))
{
System.out.println (str);
//textArea.appendText (str + "n");
}
input.close ();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

最新更新