Android:Facebook Applink cURL to java

时间:2015-05-10 09:30:08

标签: android facebook facebook-graph-api curl

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

我下载了cURL,然后在Windows中输入以下内容并运行并返回了一个applink ID:

  

curl -k -g https://graph.facebook.com/app/app_link_hosts -F   access_token =" INSERTED MY OWN APP_TOKEN" -F name =" Android App Link   对象示例" -F   机器人=' [{" URL":" sharesample://故事/ 1234""包":" com.facebook.samples .sharesample"" APP_NAME":" ShareSample",},]'   -F web =' {" should_fallback" :false,}'

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

另外,我想知道是否有办法找到为特定包名创建的所有applink,以便我可以看到所有创建的内容?

谢谢!

1 个答案:

答案 0 :(得分:2)

我花了几个小时研究这个,最后发现了1997年的这段代码,我认为可能不再适用,因为方法被弃用并修改为facebook applinks: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();
            }

      }