1

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.

blalasaadri
  • 5,990
  • 5
  • 38
  • 58
arv
  • 86
  • 8

2 Answers2

4

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");
Rax
  • 785
  • 4
  • 14
0

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.

Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
  • One more doubt Jonas, How to get multiple ids? ie, if there are multiple orderids, how to fetch it? Also, How to reload the ids to another URL? – arv Apr 14 '15 at 13:54
  • @arv, In that case, try the answer by rax, or update your question with an example of a URL string you want to get the order ids from, and I will update my answer. – Jonas Czech Apr 14 '15 at 13:56
  • Okay, Consider the url in the above question as 1st url. The 2nd url is : http://sndbx.abc.com/mob#?path=confirmOrder&oid= &pid= &asins= . Here the values of the orderid(multiple) from the first url should be filled here in the 2nd url ie, 2nd url should be http://sndbx.abc.com/mob#?path=confirmOrder&oid=404-35644-70307&pid=404-2849-9658 &asins=B00F0G8K – arv Apr 15 '15 at 05:55
  • @arv, I will have a look. – Jonas Czech Apr 15 '15 at 06:33
  • @arv, will probably be a few hours, as i have other stuff to do. – Jonas Czech Apr 15 '15 at 07:58
  • @arv, sorry for taking so long, as i'm _very_ busy today, did you try the answer by rax ? Otherwise, you can do it with a separate regex. If you want an answer faster, ask a new question, and tag it with "regex". – Jonas Czech Apr 15 '15 at 12:41
  • oh :( I tried it, but i'm not getting the ids. Its not working. I checked it with LogCat. – arv Apr 15 '15 at 13:08
  • @arv, Could you edit some example url's into your question, as the comment system seems to have mangled the one you posted. – Jonas Czech Apr 15 '15 at 18:13
  • @arv, you could also use `String.split()`, as per [here](http://stackoverflow.com/a/13691481/4428462). – Jonas Czech Apr 15 '15 at 21:23
  • Ya okay.. Got it Jonas. Thanks ! I used rax's answer. – arv Apr 16 '15 at 06:28
  • How to reload the 2nd url in the webview? – arv Apr 16 '15 at 06:43
  • @arv, you can use `webview.reload()` – Jonas Czech Apr 16 '15 at 06:59