【问题标题】:Android: Facebook Applink cURL to javaAndroid:Facebook Applink cURL 到 java
【发布时间】:2015-05-14 03:08:04
【问题描述】:

我一直在尝试将 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 应用链接 对象示例”-F android='[{"url":"sharesample://story/1234","package":"com.facebook.samples.sharesample","app_name":"ShareSample",},]' -F web='{"should_fallback" : false,}'

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

我还想知道是否有一种方法可以查询为特定包名称创建的所有应用链接,以便我可以看到创建的所有内容?

谢谢!

【问题讨论】:

  • 无论您使用什么 http 客户端库,请搜索其文档或查看如何发出 multipart/form-data 请求的示例。您显示的 cURL 命令正在发出一个多部分请求,每个 -F 是一个正文部分。

标签: android facebook facebook-graph-api curl


【解决方案1】:

我花了几个小时对此进行研究,最后发现了 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();
            }

      }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-24
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多