【问题标题】:How to fetch the values present in a url from a webview in android?如何从android中的webview获取url中存在的值?
【发布时间】:2015-04-11 10:07:46
【问题描述】:

我有一个网址,其中在 web 视图中有 ID(例如订单 ID、产品 ID 等)。如何获取这些 id 并将它们放入字符串中? 比如说,网址是

www.xyz.com/buy/thankyou/handlers/display.html?ie=UTF8&asins=B00F0G8K&orderId=404-35644-70307&purchaseId=404-2849-9658

我需要为&asins、&orderid、&purchase_id 提供的值并将它们传递给另一个网址。我怎样才能拿到它们? 所有这些都发生在 webview 中。

【问题讨论】:

  • 使用webView.getUrl(),然后使用正则表达式提取值。
  • 谢谢乔纳斯。也请帮助我处理正则表达式

标签: java android


【解决方案1】:

从WebView 获取 URL,将其解析为 Uri 并从中获取各个查询参数。

示例:

Uri uri=Uri.parse(myWebView.getUrl());
String orderId = uri.getQueryParameter("orderid");

【讨论】:

  • 我应该把这个放在 shouldOverrideUrlLoading() 里面吗?
  • 不,只要你想读取 WebView 的当前 URL,就用这个。只需确保 URL 已加载。
  • 最佳答案,简单直接。
【解决方案2】:

刚刚看到@rax 的回答,应该会更好。

试试这样的:

String regex = "asins=(.*)&orderId=(.*)&purchaseId=(.*)";
String myString = "www.xyz.com/buy/thankyou/handlers/display.html?ie=UTF8&asins=B00F0G8K&orderId=404-35644-70307&purchaseId=404-2849-9658 ";
//use String myString = webView.getUrl(); in your case;
Pattern pattern = Pattern.compile(regex); 
Matcher matcher = pattern.matcher(myString);

while (matcher.find()) {
        System.out.println(matcher.group().replaceAll(regex, "$1"));
        System.out.println(matcher.group().replaceAll(regex, "$2"));
        System.out.println(matcher.group().replaceAll(regex, "$3"));
    }

输出:

B00F0G8K
404-35644-70307
404-2849-9658 

希望对您有所帮助,如果您有任何问题,请发表评论。

【讨论】:

  • 还有一个疑问 Jonas,How to get multiple ids?即,如果有多个orderid,如何获取它?另外,如何将 id 重新加载到另一个 URL?
  • @arv,在这种情况下,请尝试 rax 的答案,或者使用您想要从中获取订单 ID 的 URL 字符串示例更新您的问题,我会更新我的答案。跨度>
  • 好的,将上述问题中的 url 视为第一个 url。第二个网址是:sndbx.abc.com/mob#?path=confirmOrder&oid= &pid= &asins= 。这里来自第一个 url 的 orderid(multiple) 的值应该在第二个 url 中填写,即第二个 url 应该是 sndbx.abc.com/… &asins=B00F0G8K
  • @arv,我去看看。
  • @arv,可能需要几个小时,因为我还有其他事情要做。
猜你喜欢
  • 1970-01-01
  • 2011-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-04
相关资源
最近更新 更多