【问题标题】:How to create a Twitter share button in Android?如何在 Android 中创建 Twitter 分享按钮?
【发布时间】:2012-12-31 09:48:34
【问题描述】:

我正在创建一个 android 应用程序,其中我想添加 Twitter 分享按钮,点击 Twitter 按钮将在 Twitter 上分享一些东西(标题 + url)。我快到了,但链接有问题。当我用“&”给两个参数时,它不起作用。请让我知道是否有人有我可以实施的合适答案。非常感谢您的回答。

//Full URL: http://www.mywebsite.com/index.php?option=com_content&catid=40&id=12546&view=article**

String title = "Hello title";
String catid = "40";
String id = "12546";

Uri.Builder b = Uri.parse("http://www.mywebsite.com/").buildUpon();
    b.path("index.php");
    b.appendQueryParameter("option=com_content&catid", catid);
    b.appendQueryParameter("&id", id);
    b.appendQueryParameter("&view=article", null);
    b.build();

String url = b.build().toString(); 

String tweetUrl = "https://twitter.com/intent/tweet?text=" + title + "&url=" + url;

Uri uri = Uri.parse(tweetUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));

输出:=> “http://www.mywebsite.com/index.php?option=com_content&catid=40”

但 => “&id=12546&view=article” 不见了。

【问题讨论】:

    标签: android twitter parameters


    【解决方案1】:

    您不应该在“appendQueryParameter”的第一个参数中提供与号(&),它将自动为您添加。将其用作:

    b.appendQueryParameter("id", id); // instead of ("*&*id", id);
    b.appendQueryParameter("view=article", null); // instead of ("*&*view=article", null);
    

    输出将是您期望的输出

    https://twitter.com/intent/tweet?text=Hello title&url=http://www.mywebsite.com/index.php?option%3Dcom_content%26catid=40&id=12546&view%3Darticle=null
    

    参见here 的用法示例。

    【讨论】:

    • Eduardo,我按照你说的做了,但还是不行 Uri.Builder b = Uri.parse("mywebsite.com/").buildUpon(); b.path("index.php"); b.appendQueryParameter( "option=com_content&catid", catid); b.appendQueryParameter("id", id); // 代替 ("*&*id", id); b.appendQueryParameter("view=article", null); //而不是 ("*&*view=article", null); b.build(); 输出 => 还是一样的
    • 有趣,我正在使用最新的 API 来测试它,它在这里工作。我不知道发生了什么:(
    • @Osman 您能否将我的答案标记为正确答案?谢谢。
    猜你喜欢
    • 2016-04-07
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    • 2012-01-08
    相关资源
    最近更新 更多