Iam loading a url into a WebView. It loads a login page. After login the user gets redirected to different urls. When he reaches the last url he gets authenticated. I want to load another activity when the last url matches a String that I am forming.
Any help on how this can be achieved?
You seem to have asked this question 2-3 times now my friend.
I am not sure if i have a proper answer to your issue.
If i was in your place i would try to use some tools to fetch all the url's occurring due to the redirection.
Try using tools like
http://redirectdetective.com/
http://www.internetofficer.com/seo-tool/redirect-check/
and in your onPageFinished
get the current url of the webview using
String webUrl = webView.getUrl();
and compare it with the final url after redirection
and then launch your intent
If you want to check the urls try below code:
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("yourURL.com")){
//Intent intent = new Intent(this, Test.class);
//startActivity(intent);
webView.stopLoading(); //this line is to stop loading the webview
}
return true;
}
});
Related
I need to show a login webpage link in webview and the link is something like below
url = "https://test-dev.test.com/as/authorization.oauth2?client_id=com.test.td&response=code&value=id test mail&redirect_uri=com.test.ap://oauth2/test";
[Modifed the actual URL with different names]
On this page, we have to enter username and password. Clicking on login will take you to OTP screen, after entering the OTP result will be a url response and from this I need to read the code. Using this code I have to make a request to get the authentication token for the session. For token request, response will be Json.
Now I need help in resolving the below:
Currently its opening in browser and not in webview. But other links are opening in webview within the app except the above link.
Which is the call back method for handling response from this transaction.
Below is my code:
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new MyWebViewClient());
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
webView.loadUrl(getAuthorizationURL());
webView.setHorizontalScrollBarEnabled(false);
webView.setWebContentsDebuggingEnabled(true);
// webView.addJavascriptInterface(new MyJavaScriptInterface(this), "HtmlViewer");
}
private String getAuthURL() {
Uri.Builder builder = new Uri.Builder();
builder.scheme("https")
.authority("test-dev.test.com")
.appendPath("ta/authorization.oauth2")
.appendQueryParameter("client_id", "com.test.td")
.appendQueryParameter("response", "code")
.appendQueryParameter("value", "id test mail").appendQueryParameter("redirect_uri", "com.test.ap://oauth2/test")
String url = Uri.decode(builder.build().toString());
//url = "https://www.google.com/";
// url = "https://developers.google.com/webmaster-tools/search-console-api/about";
return url;
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
System.out.println(" shouldOverrideUrlLoading :============ " + Uri.parse(url).getHost() + " url " + url);
// if(url.contains("dev.test.com")){
// webView.loadUrl(url);
// return false;
// }
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
return true;
}
}
This sounds like you're doing an OpenID authentication. You're implementation of shouldOverrideUrlLoading() is both firing an intent which will most likely go to the default web browser, and returning true. The return value tells Android whether you're taking control of loading the page (true) or leaving it to the web view (false). If you don't take control of handling the URL, the web view will try to load it like a normal page.
You're also immediately directing the web view to your authorization URL in your onCreate code. I'm thinking this is part of the confusion over the navigation behavior you're seeing.
For this kind of authentication process, you don't want to override loading the OTP page. Let the web view handle that just like anything else. What you want to do is capture the URL response. Your if statement within shouldOverrideUrlLoading should trap the response URL, consume the contents, and respond true to let the web view know you've taken care of that URL. Everything else should be handled by the web view.
Finally, the commented version of the code looks to me like it would send you into an infinite loop, the way you have it written. You're telling the web view to load the authorization URL, which will lead back to your web client code, which will trap the URL, causing it to load the URL...
I have a webview with custom webview client. I'm intercepting requests with:
#Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
// ...
}
sometimes I'm getting informations about custom events from webview in a form of a link:
xyz://do-something
When it happens I want webview to ignore it. But 'shouldInterceptRequest' has to return something and when I'm returning 'null' instead of my page it shows: 'unknown url xyz://do-something'. How can I deal with that? How can I intercept the link but disable action on webview side?
You just need to do a string comparison from the site that you are in and the site that you want to go to.
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
Im using a webview to display a website.
The page iam opening using loadurl method is the login page,
on successful login redirection occurs to a particular page .
I want to skip that page i.e dont want to show that page rather show the webpage after that
eg If my webview starts with webpage A
on login success it shows webpage B
and then next page is webpage C
i want after Login success in page A it should go automatically to webpage C
current code im using
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url)
.getHost()
.equals("https://www.test.com")) {
web.loadUrl("https://www.test1.com");
}
return false;
}
so any tips/pointers as to how i do this?
thanks
Read this
You just need to do a string comparison from the site that you are in and the site that you want to go to.
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
When a user clicks on a link in the webview. I want to show a dialog asking whether the user wants to view it in default browser, if he opts YES then he should be taken to default browser otherwise it shouldn't load the link at all.
But, the issue I am facing is, I could able to show the dialog inside run() of WebViewClient's overridden method shouldOverrideUrlLoading(). When the user opts YES I am doing startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(url))); to show it in default browser. But, irrespective of the user selects for YES/NO it is showing the link in webview which I want to avoid. Any suggestions appreciated...TIA
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
http://developer.android.com/guide/webapps/webview.html
I resolved the issue, by just reloading the same URL inside shouldOverrideUrlLoading() method. Thanks for your suggestions
Android has changed its default method for opening clicks, it now opens them in the webview instead of a new browser. This has already been asked here but every thing I have tried opens the links in the WebView. Can someone give me details on capturing the clicks so I con force the link to open in the default browser.
use this in your button onclick:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
For it to parse it needs a http:// at the start.
Finally working don't know if it is the best way but it works. I placed the following code inside the onCreate. The string strSiteUrl set to the page I want the WebView to show.
/* Load WebView in memory */
WebView webv = (WebView) findViewById(R.id.webv);
webv.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent browserIntent = new Intent();
browserIntent.setAction(Intent.ACTION_VIEW);
browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
browserIntent.setData(Uri.parse(url));
startActivity(browserIntent);
return false;
}
}); //End webv.setVewView
/* Configure WebView */
WebSettings webSettings = webv.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);
webv.loadUrl(strSiteUrl);
When a user clicked on a link in the WebView page it would open the default browser and show the linked page. However, after clicking the back button depending on which link was clicked the WebView would return to the original page or show the linked page. This is not what I wanted, I only wanted the WebView to show the original page. I don't know why some links didn't return correctly, maybe those links were redirects? So to get around this problem I used the onStart call. I made the view webv global by placing
WebView webv;
in my global declarations. Changed the webv assignment to
webv = (WebView) findViewById(R.id.webv);
Then created the following onStart
#Override
public void onStart() {
super.onStart();
String strReturnUrl = String.valueOf(webv.getUrl());
Log.i("URL!", strReturnUrl);
if (!strReturnUrl.contentEquals(strSiteUrl)) {
webv.loadUrl(strSiteUrl);
}
}
Writing to the log the returned url proved that when the back button was pressed it returned with different urls depending on which link was clicked. I used the if statement to prevent unnecessary reloading of the original url.