arv
arv

Reputation: 86

How to fetch the values present in a url from a webview in android?

I have a url where there are ids (such as order id, product id, etc) in a webview. How can I fetch these ids and put them into strings? Say for example, the url is

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

I need the values present for &asins, &orderid, &purchase_id and pass them to another url. How can I fetch them? All of this is happening inside a webview.

Upvotes: 1

Views: 1984

Answers (2)

Jonas Czech
Jonas Czech

Reputation: 12338

Just saw the answer by @rax, it is probably better.

Try something like this:

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"));
    }

Output:

B00F0G8K
404-35644-70307
404-2849-9658 

Hope this helps, and do comment if you have any questions.

Upvotes: 0

Rax
Rax

Reputation: 796

Get the URL from WebView, parse it as Uri and get individual query parameters from it.

Example:

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

Upvotes: 4

Related Questions