.htaccess in iframe in a Webview - android

I have a webview app that I've created that pulls in a password protected camera feed with a .htaccess file protecting it. Of course, it says that it's unauthorised.
I found this bit of code:
private class MyWebViewClient extends WebViewClient {
#Override
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm) {
handler.proceed("user", "password");
}
}
I'm assuming this would work if the whole page was protected but it doesn't seem to work where it's the iframe that's protected.
Is there another way I can do this?

Related

How to get the WebView object inside a custom WebViewClient?

I created a new function in a CustomWebViewClient class that needs to use the webViewObject. However, I dont seem to find a way how to access it without passing the parameter to the constructor.
This is what the class looks like:
public class CustomWebViewClient extends WebViewClient {
public CustomWebViewClient(){
myFunction();
}
private myFunction(){
//needs to use the webView object
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String check_url){
//some code here
return false
}
}
This is how to set the custom class to a webViewObject:
CustomWebViewClient myWebViewClient = new CustomWebViewClient();
webViewObject.setWebViewClient(myWebViewClient);
Notice that shouldOverrideUrlLoading function gets the parameter. So its possible its there somewhere.

If WEB_URL site fails to load in the webview, how to move to another URL - Android

If WEB_URL site fails to load in the webview, how to move to another URL - Android
There's something I don't know when building an Android app.
The URL private final static String WEB_URL = "https://a.html"; ` is received as a URL and displayed in the webview.
private final static String WEB_URL = "https://a.html";
If this URL doesn't exist
private final static String WEB_URL1 = "https://b.html";
private final static String WEB_URL2 = "https://c.html";
private final static String WEB_URL3 = "https://d.html";
If the WEB_URL site fails to load in the webview due to a problem,
Load the web view with WEB_URL1
If it is not WEB_URL1, I want to automatically load the webview to WEB_URL2 and WEB_URL3.enter code here
I would like to know the code or example for this.
Thank you for teaching me.
Thank you.
You could handle this by override onReceivedError method in WebViewClient like so:
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){
view.loadUrl(WEB_URL2);
}

Get type of object when click on webview - Android

I am developing a webview in which i have to define the type of object when i click to this object. For example, when i click to a link, webview understand it is a link and i can get link (returned object); when i click to a image, webview understand it is a image and return image object. I want to get the object type to do some more activities. For example, when i long click to the image, there are some action for me: download, set as background,... when i long click to a link, there are some option like: open in new tab, add to bookmark,... Does anyone know the solution. Thank u very much :)
I think that you cannot have direct visibility to the "downside" html/javasctipt from webview listeners.
(WebView reference here: http://developer.android.com/reference/android/webkit/WebView.html)
However, if you are developing the web application running in the WebView, you can obtain the same result by using JavascriptInterface methods, implementing the javascript methods to "upside" return objects toward android app.
Example interface class:
public class JSUpInterface {
protected Context mContext;
public JSUpInterface(Context c) {
mContext = c;
}
#JavascriptInterface
public void showToast(String message) {
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
}
#JavascriptInterface
public void getLink(String link) {
// shows in a toast the clicked link
Toast.makeText(mContext, link, Toast.LENGTH_SHORT).show();
}
}
WebView attaching:
jsUi = new JSUpInterface(context);
webview.addJavascriptInterface(jsUi, "jsui");
your html:
www.google.com

Dynamically changing css values in a webview without reloading (Android)

I have a WebView based app to which I want to add a 'night-mode'.
My first instinct was to just have the program render the right CSS values each time depending on whether night-mode was on or not, and it works great, but it means that the page has to be reloaded each time the mode is toggled.
Is there any way I can change the css values retro-actively without having to reload the page?
You could inject code into your page's DOM. So you could for example have a reset stylesheet, and then inject a day or night mode class with javascript into the dom as required like this:
public class WebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url)
{
view.loadUrl("javascript:document.getElementById(id).style.property=new style");
}
}
HTH

jquery mobile and phonegap addJavascriptInterface

I am developing an Android app with jQuery Mobile and PhoneGap. This is my activity:
public class xxx extends DroidGap {
/** Called when the activity is first created. */
private String FILENAME = "";
private static final String HTML_ROOT = "file:///android_asset/www/";
private Handler handler = null;
private static WebView webView = null;
public MyTeam team = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
handler = new Handler();
FILENAME = getString(R.string.fileName);
//parsing data from file
String json = readFile();
team = parseJson(json);
appView.addJavascriptInterface(this, "MyTeam");
super.loadUrl("file:///android_asset/www/menu.html");
}
public String HW(){
return team.getTeamName();
}
And this is my Javascript code in menu.html:
$(document).ready(function(){
alert(MyTeam.HW()); // work perfectly
alert("name "+MyTeam.team.getTeamName()); //doesn't work!
});
So the problem is:
I can access the HW function that returns me team.getTeamName();
I don't have access to the team variable name in my activity because MyTeam.team.getTeamName() doesn't work.
It seems that I can access only functions but not directly public variables.
What I am doing wrong?
Thanks.
You can't access the Java objects fields directly. It's right there in the documentation of addJavaScriptInterface. Also, if you are using PhoneGap then you should just write a plugin and let us worry about the cases where addJavaScriptInterface falls down.

Categories

Resources