Android with webview with remote website - android

My android app has a webview which directly goes to the remote PHP site and shows user the registration form.
My question is, once user get registered successfully I want to close the webview and start another activity which is in native view. But how can I know whether he/she got registered successfully or not?
Thanks

You can do it this way:
In your PHP code, when the user gets registered successfully, redirect user to a unique link(this link will be used in your android code). Let's assume the link you redirect user to is "https://blablabla.com/successfully_registered"
In the WebViewClient's OnPageStarted method used by your WebView, Get the URL and check if it's similar to your redirect URL we assumed you used in your PHP code.
For Example:
WebView wv = findViewById(R.id.webview);
wv.setWebViewClient(new WebViewClient(){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
//check if URL is similar to redirect URL
if(url.contains("https://blablabla.com/successfully_registered")){
view.stopLoading();
//user have been successfully registered. Start another activity or do something else...
}
}
});

Related

Auto login on web page

I am trying to write an app I want it to open a web page and auto login I am not sure how to go about sending the info to the browser from the app code.
So basically you are going to need to load in the webpage within a WebView (You can find instructions for that here and then probably push javascript into the WebView that will fill in the fields and load the page.
In your activity's onCreate:
WebView webview = new WebView(this);
setContentView(webview);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean onPageFinished(WebView view, String url) {
// Check here if url is equal to your site URL.
}
});
webview.loadUrl("http://yourwebsite.com/");
This line enables javascript in your WebView:
webView.getSettings().setJavaScriptEnabled(true);
Then you can use the WebViewClient to detect when the page you want has fully loaded. When that happens, you can use:
webView.loadUrl("javascript:document.getElementsByName('username').value = 'username'");
webView.loadUrl("javascript:document.getElementsByName('password').value = 'password'");
webView.loadUrl("javascript:document.forms['login'].submit()");
And it should automatically log you in. It's worth noting that this generally isn't easy to do on a lot of sites since they will randomize the login control ids and it also doesn't generally sit well with users if an application is logging into a website automatically for them.

Flickr-Android API : Oauth is not redirecting to 'grant-access' page

I'm developing an Android application which requires Flickr API integration. In previous days, I was able to successfully complete the oauth process. But now its not working, and I strongly believe it happend after flickr APIs changed from http:// to https://
Let me explain the situation..
I'm following the steps explained in Flickr API Doc
As a result of the call to http://www.flickr.com/services/oauth/request_token API, I'm successfully receiving the oauth_token.
After this step, I'm presenting Flickr authorization page in a webview with the url specified in API doc (which is somthing similar like, https://www.flickr.com/services/oauth/authorize?oauth_token=72157626737672178-022bbd2f4c2f3432, and of-course, with the oauth_token which I received in the previous step)
Few days before this call was working when I'm using http:// instead of https://. But now, both http:// and https:// are not working. The page is displaying the login screen, but after successful login, the page is not redirecting to the grant access page, instead it is just redirecting to the Flickr Home page. And hence, I'm unable to grant access and unable to receive the oauth_verifier.
Hope I'm well explaining the situation which I'm facing now. But in short, Flickr Login for my application is not working, and I'm running out of time... :(
So, Geeks, Please give some light on the issue...
--Thanks in advance.
I got the same problem. I was using the connection in a WebView and my code was using the "http" protocol in the request.
I could resolve the issue by first using the "https" protocol for the authentification URL. As you say just doing that did not work. So I change the call of the url that was in the WebView and used the ACTION_VIEW intent and it worked.
Calling :
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(AuthentificationPath)));
Don't forget to implement the OnResume() function of your Activity to get the data of the returned intent.
Of course it is just a workaround but it does the job if you have to be fast.
Hope it helps!
Still the issue is there, After oauth Flickr is not redirecting to grant_access page. But I need to get the oauth process successfully completed in my on-going project, as soon as possible. So I have done a workaround to solve the issue.
To solve the 'not redirecting' issue, I have done a little bit change in my WebView's WebViewClient implementation. It's nothing great, but just redirecting to authorize page again, after successful login.
Authorize URL Example : https://www.flickr.com/services/oauth/authorize?oauth_token=72157626737672178-022bbd2f4c2f3432
So, given below is the detailed implementation of WebViewClient:
mAuthWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
showProgressDialog();
/** TODO: This part should be deleted after flickr fixes the oauth redirection issue */
if(url.endsWith("flickr.com/")) {
mAuthWebView.loadUrl("https://www.flickr.com/services/oauth/authorize?oauth_token=72157626737672178-022bbd2f4c2f3432");
return;
}
/* After successful login the redirected url will contain a parameter named 'oauth_verifier' */
if (url.indexOf("oauth_verifier=") > -1) {
/* Get the 'oauth_verifier' parameter from url and do next steps */
performLoginSuccess();
}
}
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (url.contains(_my_redirect_url_) && !url.contains("oauth_verifier=")) {
/* User not authenticated our app */
finish();
} else if (!url.contains("oauth_verifier=")) {
killProgressDialog();
}
}
});
After loading the authorization url, the web view will take the user to login page, and after user successfully loged into Flickr, with web vew client implementation we are again loading the authorization url, to present the grant access page.
mAuthWebView.loadUrl("https://www.flickr.com/services/oauth/authorize?oauth_token=72157626737672178-022bbd2f4c2f3432");
I know, this is not the right way to do this...
Any better solutions are always welcome.... :)

WebView not loading URL

I want to load a web page in my webView.
Tried placing the webView.loadurl("") in AsyncTask's doinbackground / onpostexecute
and in the onresume.
The url is correct but nothing happens it just shows a white page. In the android manifest file internet access is enabled.
What else needs to be done to load a webview?
The application does not crash or show any error.
In my emulator I set the proxy with my user name and password.
Here is the code I use to load the URL:
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClientSubClass());
webView.loadUrl(promoURL);
I would recommend you to check what callbacks your webViewClient is getting. I'm guessing that the site requests an authentication, so override onReceivedHttpAuthRequest and do something like this
#Override
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm) {
Log.d(TAG, "onReceivedHttpAuthRequest"));
handler.proceed(username, password);
}
Or if the authentication isn't the problem you can always overide onReceivedSslError to see if there is some certificate problem.
As the initial step though, I would recommend you to use the browser to see if you can load the page. I'm having some trouble with an https site that requires authentication, I enter my credentials and the site can't load(this is on android 2.3)
myVideoView = (WebView) findViewById(R.id.webView1);
myVideoView.setWebViewClient(new WebViewClientSubClass());
myVideoView.getSettings().setJavaScriptEnabled(true);
myVideoView.setPersistentDrawingCache(0);
myVideoView.getSettings().setPluginsEnabled(true);
myVideoView.requestFocus(View.FOCUS_DOWN);
myVideoView.loadUrl(promoUrl);
try this should work and also check whether it opens with https in normal browser.

How do I open another app in my WebView app?

I have an Android app with that displays a mobile website (WebView), in the mobile website there are links redirecting to a PDF, Excel and video files.
When try to open it in my regular browser my phone asks to open it with another app or it start a download, so I can open it afterwards.
But in my WebView app it either doesn't work, no response or it displays a "Page unavailable" error.
Is it even possible?
To handle links in WebView, you can use the shouldOverrideUrlLoading method of WebViewClient class. Consider the following example;
WebView webView = (WebView) findViewById(R.id.infoView);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Assuming you are giving link to some PDF file.
if (url.contains(".pdf")) {
// Now do what you want to with the url here
}
return true;
}
}
This way, you can intercept any link tapped in WebView and then do whatever you want.

Receive post data from a 3rd party within a webview

Stuck on an issue with http post data.
So we've created a webview to work in.
Then called a third party webservice this ask's to provide a postback url and some parameters, the webservice then processes the paramaters and redirects the user (our app) to the postback url we provided, with some data as a http post.
We want to get the post data from this postback url but within the webview or within our app.
Tried intercepting the url load with this code:
final WebView webview = new WebView(this);
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Grab data here...
return super.shouldOverrideUrlLoading(view, url);
}
});
However can't seem to find any objects or methods to gain access to the data within this method.
The other attempt was to give them the postback url as an intent within our app like:
myscheme://someText/someParam
this would have started a new activity when the postback is called (as we have set up an intent-filter within our android-manifest), this intent fires up our app activity within a browser but not within our webview, but anyway again we can't see how to access the post data from this.
Thanks for looking,
Any ideas?
Didn't find a way to do this. Basically can't get at data posted at a webview or a browser intent.
Instead the solution we went with is:
Postback to a server, get the postdata here (PHP) and save in a DB, then retrieve the data from this DB within the phone.

Categories

Resources