百分号会导致Twitter应用崩溃

时间:2014-03-29 16:52:48

标签: android twitter

我正在创建一个Android应用程序,允许用户创建推文,然后在Twitter中打开它们,以便他们可以发推特。我使用以下代码获取他们输入的文本并将其发送到Twitter:

String tweetUrl = "https://twitter.com/intent/tweet?text=" + URLEncoder.encode(mTweetTextBox.getText().toString());
Uri uri = Uri.parse(tweetUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));

除了用户输入百分号外,这一切似乎在编码特殊字符方面都很有效。这会导致Twitter应用程序在选择使用Twitter时崩溃,但如果他们选择在Chrome中打开推文,则可以正常工作。任何想法可能是什么问题或我如何解决这个问题?

这是控制台输出:

03-29 12:50:12.705: E/AndroidRuntime(24458):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
03-29 12:50:12.705: E/AndroidRuntime(24458):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
03-29 12:50:12.705: E/AndroidRuntime(24458):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
03-29 12:50:12.705: E/AndroidRuntime(24458):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
03-29 12:50:12.705: E/AndroidRuntime(24458):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-29 12:50:12.705: E/AndroidRuntime(24458):    at android.os.Looper.loop(Looper.java:136)
03-29 12:50:12.705: E/AndroidRuntime(24458):    at android.app.ActivityThread.main(ActivityThread.java:5102)
03-29 12:50:12.705: E/AndroidRuntime(24458):    at java.lang.reflect.Method.invokeNative(Native Method)
03-29 12:50:12.705: E/AndroidRuntime(24458):    at java.lang.reflect.Method.invoke(Method.java:515)
03-29 12:50:12.705: E/AndroidRuntime(24458):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-29 12:50:12.705: E/AndroidRuntime(24458):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-29 12:50:12.705: E/AndroidRuntime(24458):    at dalvik.system.NativeStart.main(Native Method)
03-29 12:50:12.705: E/AndroidRuntime(24458): Caused by: java.lang.IllegalArgumentException: Invalid % sequence at 5: test % tweet
03-29 12:50:12.705: E/AndroidRuntime(24458):    at libcore.net.UriCodec.decode(UriCodec.java:169)
03-29 12:50:12.705: E/AndroidRuntime(24458):    at java.net.URLDecoder.decode(URLDecoder.java:60)
03-29 12:50:12.705: E/AndroidRuntime(24458):    at com.twitter.library.util.Util.b(Twttr:369)
03-29 12:50:12.705: E/AndroidRuntime(24458):    at com.twitter.android.UrlInterpreterActivity.c(Twttr:631)
03-29 12:50:12.705: E/AndroidRuntime(24458):    at com.twitter.android.UrlInterpreterActivity.b(Twttr:237)
03-29 12:50:12.705: E/AndroidRuntime(24458):    at com.twitter.android.client.BaseFragmentActivity.onCreate(Twttr:313)
03-29 12:50:12.705: E/AndroidRuntime(24458):    at android.app.Activity.performCreate(Activity.java:5248)
03-29 12:50:12.705: E/AndroidRuntime(24458):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
03-29 12:50:12.705: E/AndroidRuntime(24458):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
03-29 12:50:12.705: E/AndroidRuntime(24458):    ... 11 more

显然它导致IllegalArgumentException,但我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:2)

The percent sign is used to encode special characters。如果你想推文“test%tweet”,你可以改用special percent encoding

    try {
        final String tweetUrl = "https://twitter.com/intent/tweet?text="
                + URLEncoder.encode("test %25 tweet", "UTF-8");
        final Uri uri = Uri.parse(tweetUrl);
        startActivity(new Intent(Intent.ACTION_VIEW, uri));
    } catch (final UnsupportedEncodingException e) {
        e.printStackTrace();
    }