【问题标题】:"http" and "?" are getting cutoff in android webview mailto links“http”和“?”在 android webview mailto 链接中被截断
【发布时间】:2011-12-16 19:04:38
【问题描述】:
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
    setContentView(R.layout.main);

    // Don't create another webview reference here,
    // just use the one you declared at class level.
    webview = (WebView) findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("http://www.example.com");

    webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)
        {
            activity.setTitle("Loading...");
            activity.setProgress(progress * 100);

            if(progress == 100)
                activity.setTitle(R.string.app_name);
        }
    });

    webview.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String     description, String failingUrl)
        {
        // Handle the error
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
          if(url.startsWith("mailto:")){
              MailTo mt = MailTo.parse(url);
              Intent i = newEmailIntent(HelloWorld.this, mt.getBody(), mt.getSubject());
              startActivity(i);
              view.reload();
              return true;
        }

        view.loadUrl(url);
        return true;
    }

   });
}
public static Intent newEmailIntent(Context context, String body, String subject ) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_EMAIL, new String[] {});
    intent.putExtra(Intent.EXTRA_TEXT, body);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.setType("rfc2368/html");
    return intent;
   }
} 

尝试将 mailto 添加到 webview 应用程序。当您运行应用程序并单击 mailto 链接时,它会打开信使。出于某种原因,“http”和“?”被截断并且无法被mailto识别。相同的 mailto 链接在设备普通浏览器中完美运行。我需要获取的唯一字段是主题和正文。

【问题讨论】:

  • 你能确定 mailto 链接在哪一点被破坏了吗?
  • 电子邮件 从这里例如,mailto 消息正文中显示的唯一内容是“/exampletype”/ 之前的所有内容,并从 ?和失踪后。
  • 对不起,我应该更清楚。 在您的代码中什么时候链接会搞砸,要弄清楚这一点,您应该使用 logcat 在所有使用它的地方显示 URL 字符串。

标签: android mailto


【解决方案1】:

好赌:确保 mailto 链接主题/正文字段中的任何内容在到达 MailTo 解析代码时都经过 URL 转义(没有文字斜线、& 符号等)。 “?”尤其是Mailto RFC 中的标头的保留字符。

也就是说,不是

mailto://...subject=http://foo.com?x=y

而是尝试

mailto://...subject=http%3A%2F%2Ffoo.com%3Fx%3Dy

【讨论】:

  • 我用正确的 url 编码更改了正斜杠和问号,但仍然没有预填充正文和主题。
猜你喜欢
  • 2018-10-26
  • 2023-03-23
  • 1970-01-01
  • 2012-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-18
  • 1970-01-01
相关资源
最近更新 更多