Muhammad zubair
Muhammad zubair

Reputation: 261

How to get current URL from Web View and save it to string

i have write the simple code for Web View and i open the YouTube URL inside the web view. when i open my app YouTube home page is open and when i select any video from YouTube home page i want to save that current URL from web view to any string. whenever i change video i want to save the video URL to string and want it the URL to override to string.

here is my code:

webView.setWebViewClient(new OurViewClient());// class which i have mention below
        try {
            webView.loadUrl("http://www.youtube.com");
        }catch (Exception e){
            e.printStackTrace();
        }

    }

OurViewClient class code:

@Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
       view.loadUrl(url);
        return true;
    }

i want to save URL which is passed above when the video is selected from YouTube.When video is changed URL link is also changed in String
can anybody help me with this

Upvotes: 1

Views: 4633

Answers (3)

Prasad Kadu
Prasad Kadu

Reputation: 21

@Override
public void onLoadResource(WebView view, String url) {
    super.onLoadResource(view, url);
    string urldata=view.getURL();
}

Upvotes: 2

Jins Lukose
Jins Lukose

Reputation: 707

private void startWebView(String url)
{
        webview.loadUrl(url);

webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)
        {
            url_.setText(view.getUrl());//here you get url
        }
    });
}

Upvotes: 0

jwir3
jwir3

Reputation: 6219

Perhaps I'm not correctly understanding your question, but it sounds like you want to save the url parameter that is being loaded by your OurViewClient every time the url changes, correct? I think you can do this with the following:

public class OurWebViewClient extends WebViewClient {
  private String currentUrl;

  // ... Existing code ...

  @Override
  public void onLoadResource(WebView view, String url) {
    super.onLoadResource(view, url);
    this.currentUrl = url;
  }


  // Since the variable is private, we provide an accessor
  public String getUrl() {
    return this.currentUrl;
  }
}

This is pretty much taken directly from the WebViewClient docs, and there are a number of other observer methods you can override.

Upvotes: 0

Related Questions