I am using webview to show google canlender but want to pass google account programmatic-ally,so the setHttpAuthUsernamePassword() function should be what I need but it doesn't work at all. Here is the code:
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setHttpAuthUsernamePassword("www.google.com", "", "email", "password");
webview.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host,String realm){
handler.proceed("email","password");
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
webview.loadUrl("http://www.google.com/calendar/");
If you have any idea to make this worked, please let me know. I tried to search a lot but don't see any useful.
You're trying to use HTTP authentication (http://en.wikipedia.org/wiki/Basic_access_authentication) which is not supported by most web services. You need to have valid Google cookies, which are set only when you manually sign in with your password.
Related
I have an android webview app with user authentication, the app allows users to register their devices by collecting the device's android id. When a user runs the mobile app, it will check if the mobile device is registered then validates the login details associated with the registered device. All works as designed with no issue.
My problem is that when the PHP session expires, users will be directed to the login page. If the user closes the app and re-open it, it will log the user without having to re-enter the login details. PHP session time-out can not be changed due to company policy.
I would like to know if I could detect the login page URL change and send the device android id with the login page URL similar to the app startup process. So, when the PHP session expires, users will be automatically logged back in without having to re-enter their login credentials.
Here is the current code I am using, some irrelevant codes have been removed to reduce the code lines. This is my first android app, and I have very limited experience when it comes to mobile programming and I have been searching for the last couple of day without any luck. Help and directions are highly appreciated.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean isConnct = false;
// code here to check internet connection
// end of internet connection code
if (isConnct){
// internet is ok
final WebView webView = (WebView)findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
findViewById(R.id.imageLoading1).setVisibility(ImageView.GONE);
findViewById(R.id.webView).setVisibility(webView.VISIBLE);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
// show error message
}
});
// using setWebChromeClient to enable javascript
webView.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
// js confirm code
}
#Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
// js alert code
}
});
// enable javascript and other settings
String url = "https://www.domainname/login/" + android.provider.Settings.Secure.getString(getContentResolver(), Secure.ANDROID_ID);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setSupportZoom(true);
webSettings.setDefaultTextEncodingName("utf-8");
webView.loadUrl(url);
} else {
// show error message
}
}
try override shouldOverrideUrlLoading method in WebViewClient, check your login URL and start your auto-login from there
another approach is checking cookies expiration time, you may use CookieManager.getInstance() for this
This is what I ended up doing, and it seems working as expected, when PHP session expires, registered devices will be redirected to the index page with new session without having to re-enter user credentials.
If there is a logout button then you need to consider adding different procedure for the logout to allow users to logout and re-direct to login page.
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(ImageView.GONE);
//show webview
findViewById(R.id.webView).setVisibility(webView.VISIBLE);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
// show error message
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contentEquals("https://www.domainme/login")){
String url2 = "https://www.domainme/login/" + android.provider.Settings.Secure.getString(getContentResolver(), Secure.ANDROID_ID);
webView.loadUrl(url2);
return true;
}else {
return false;
}
}
// code truncated
}
I have some issues with Android WebView and Javascript.
Some of customers of app said that WebView on app is not showing anything.
As I checked - its probably not showing javascript at all (whole webpage is loaded in javascript by react).
That my code:
public void setupWebView(WebView accessWebView) {
accessWebView.setWebViewClient(new WebViewClient() {
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
handleRedirect(accessWebView);
return true;
}
});
accessWebView.getSettings().setJavaScriptEnabled(true);
accessWebView.getSettings().setDomStorageEnabled(true);
accessWebView.loadUrl(URL);
(I have to use WebViewClient, not WebChromeClient, because of the redirect handling)
Is there anything possible to change so the javascript will load on EVERY device with Android +5.0?
Is it possible that updating WebView on device will help some users?
You need to use setWebChromeClient to enable javascript in your WebView. But don't worry, you can use both setWebChromeClient and setWebViewClient in the same time. Just like in official docs:
// Let's display the progress in the activity title bar, like the
// browser app does.
getWindow().requestFeature(Window.FEATURE_PROGRESS);
webview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 1000);
}
});
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
webview.loadUrl("https://developer.android.com/");
https://developer.android.com/reference/android/webkit/WebView.html
Im integrating uber api with my android application. In the case of auth 2.0 what should i give the return url to redirect after successful authorization from uber to enter in to my app. here is my uber LoginActivity
WebView webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setLoadWithOverviewMode(true);
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
webView.setScrollbarFadingEnabled(false);
progressBar = ProgressDialog.show(UberLoginActivity.this, "Please wait", "Loading...");
webView.loadUrl("https://login.uber.com/oauth/v2/authorize?client_id=MDyPSkkFFSZHKcntIg9j2mhM7XP665R1&response_type=code");
SessionConfiguration config = new SessionConfiguration.Builder()
.setClientId("***********") //This is necessary
.setRedirectUri("what should be added ") //This is necessary if you'll be using implicit grant
.setServerToken("*********************")
.setEnvironment(SessionConfiguration.Environment.SANDBOX) //Useful for testing your app in the sandbox environment
.setScopes(Arrays.asList(Scope.PROFILE, Scope.RIDE_WIDGETS)) //Your scopes for authentication here
.build();
webView.setWebViewClient(new WebViewClient()
{
public void onPageFinished(WebView view, String url)
{
if (progressBar.isShowing())
{
progressBar.dismiss();
}
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl)
{
view.loadUrl("about:blank");
Toast.makeText(getApplication(), " Please check network connectivity", Toast.LENGTH_LONG).show();
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
It is better to use SSO for handling on android. Check out the relevant code in the android widget tutorial: https://developer.uber.com/docs/ride-requests/tutorials/widget/android#login
This will use the uber app to authenticate the user, otherwise will redirect to app store. Please follow up if you still prefer to use a webview.
I am new in android. I am trying to make just a sample application to open websites in webview. The problem is, website opens in desktop mode. How can I open websites just like UCBrowser (mobile mode) ?
Try this. It should work.
webview1.getSettings().setJavaScriptEnabled(true);
webview1.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getApplicationContext(), description, Toast.LENGTH_SHORT).show();
}
});
webview1.getSettings().setLoadWithOverviewMode(true);
webview1.getSettings().setUseWideViewPort(true);
webview1.getSettings().setBuiltInZoomControls(true);
webview1 .loadUrl("http://www.matrixsystems.net.in/"); // set url
webview1.requestFocus();
I'm trying to do a webview based application for this website to show it in a mobile application.
when I put any different site the application work great, but in this specific site never show the second page when I clicked in the button to go to the desk page. However, I put a Log statement in the onPageFinished method and log that the page is loaded completely.
My Code Here
final WebView myWebView = (WebView) rootView.findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getActivity(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
#Override
public void onPageFinished(WebView view, String url) {
Log.d("WEBSITE", "Page Loaded.");
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("WEBSITE", url);
myWebView.loadUrl(url);
return false;
}
});
myWebView.loadUrl("https://demo.frappecloud.com/");
I believe the problem is with your shouldOverrideUrlLoading. If you check the documentation you will see that :
This method is not called for requests using the POST "method"
When you are submitting the Form, you are making a POST request, which is basically ignored.
Here is the link: WebViewClient
Also check this reference where it says how you could load a URL with POST data: LOAD A POST REQUEST INTO A WEBVIEW IN ANDROID
Consider checking this thread: Android - how to intercept a form POST