Android webview not loading images with site using cookie - java

I am loading a SharePoint page into a webview using LoadUrl. All the other content comes down but not the images. The site requires a cookie to login but that part is working fine its just once you get past the login page the next page is correct minus the images.
String myUrl = "https://www.mysite.com/";
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setCookie(myUrl, cook);
CookieSyncManager.getInstance().sync();
webView = (WebView)findViewById(R.id.webView1);
webView.setWebViewClient(new WebViewClient()
{
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error)
{
handler.proceed();
}
public void onProgressChanged(WebView view, int progress)
{
}
});
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true); webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
webView.loadUrl(myUrl);

You might have the same problem as I do.
Is it possible that the picture source require authentication? You can try to open the sources separately and see if you can access them or not.
I did the same and found out that the image sources I wanted to show are protected via a cookie and right now I'm looking for a solution to send this cookie along but couldn't find a way yet.
When I read you question that is exactly the same you try to do.
( sorry to write this here but I don't have enough points to write a comment )

Related

scrolling to the top of a webview (Android)

I have a webview loading a big texts after the user has clicked an object.
I like the text to be shown from the top. But if the user scrolled down in the previous text the next is already scrolled and not shown from the top.
I am using webView.scrollTo(0,0); in my onPageFinished event of the webview.
This works fine in my emulator, but on my phone the text is scrolled in the middle.
when adding a pause with Thread.sleep it also works on my phone.
What am I doing wrong here ?
My Code:
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
// Pause is needed for my physical phone S7. In Emulator there I have no problem.
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
webView.scrollTo(0,0);
super.onPageFinished(view, url);
}
});
Try with this block of code -
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
// Pause is needed for my physical phone S7. In Emulator there I have no problem.
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
view.scrollTo(0,0);
super.onPageFinished(view, url);
}
});
Actually, you're so close to the solution. Just the point is you need to set scrollTo in the returned view of onPageFinished method.

Playing youtube video using webview in an android app

I am trying to play an youtube video inside an android app by using the url of the video.I have tried using the following code:
new Thread(new Runnable() {
#Override
public void run() {
myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebChromeClient(new WebChromeClient());
try {
myWebView.loadUrl(icobj.video_URL);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
It works, but the problem is that there is a black screen shown for about 4 seconds before the page opens.How do i get rid of the black screen before the page opens?
Check this link. You can listen the webview and show a placeholder or progress bar until loaded. It is all up to you.

Android listener for webpage to start loading?

I have a simple webview that loads a website. Once the website is loaded, I have the following to focus/scroll to the login box:
mWebview.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
(I've heard that onPageFinished is deprecated, but it's working for me in 2.2>4.4, so I'm just leaving it for now.
Anyway, I would like some sort of way to monitor for when the user actually logs in, that way when the page is done loading for the second time, I can then call some js and forward them to another page, but I have no idea how to do that. :(
I can just do another onPageFinished after they log in, but I don't know how to start monitoring... In other words, I can't start another onPageFinished immediately after the current, because it just redirects automatically.
Does anyone have any suggestions? I would greatly appreciate it! :)
EDIT:
Here's the entirety (minus the import headers) of my MainActivity.java class.
public class MainActivity extends Activity {
private WebView mWebview ;
#Override
public void onBackPressed() {
if(mWebview.canGoBack() == true){
mWebview.goBack();
}else{
super.onBackPressed();
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.getSettings().setBuiltInZoomControls(true);
if(android.os.Build.VERSION.SDK_INT >= 11) {
mWebview.getSettings().setDisplayZoomControls(false);
}
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://example.com");
setContentView(mWebview );
mWebview.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
}
}
);
}
I think I might be missing a curly bracket, but ignore that for now, haha. So anyway, after mWebview.scrollTo (the last line), the webpage is done loading, and it just chills at the login page. After a user logs in, the page (obviously) starts to load and directs to another post-login URL. I'd like to check the URL with an if statement after the page is done loading for the second time.
Does that make more sense? Sorry, it's confusing me too, trying to explain it.
Usually there will be two separate URLs, one for the post log-in page and one for the pre log-in page. You can have an if statement that checks the URL to make sure you've logged in, and if you have, then it redirects/closes/whatever.
I had a similar issue here:
Closing a Webview in Android
So basically you just want to execute some JavaScript once you've logged in? I don't see why you can't have an if statement to check whether you're logged in or not, and if you're logged in, then execute the js.
Something like this maybe?
wv.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if (url.contains(getString(R.string.loginURL))){
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
}else if(url.contains(getString(R.string.loggedInURL))) {
wv.loadUrl(js goes here);
}
}
}
This will execute every time a page is finished loading in your WebView

webview never call ShuldOverrideUrlloading in webviewclient in android

I have a webview and a link on the URL that I have loaded on that webview, I want such like that if user clicks in this link the app will go to the background and open that link with the default web browser of that device. To do this I have done the following:
web = (WebView) findViewById(R.id.webview01);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
web.getSettings().setUseWideViewPort(true);
web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
web.getSettings().setPluginState(PluginState.ON);
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setDomStorageEnabled(true);
web.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null
&& url.startsWith("http://the6figuremarketer.com/apps/androidfb21/thks.html")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
});
web.loadUrl("http://the6figuremarketer.com/apps/androidfb21/index.html");
but I figured out that the shouldoverrideurl method is nevet get called. what should I do now? can anyone help?
Look at shouldOverrideUrlLoading in WebView for Android not running.
shouldOverrideUrlLoading() is not called when you call loadurl on webview. It is called only when you click on a link in webview.

Can not get any return value from webView.geturl, (code shown below)

public class Browser1Activity extends Activity {
TextView url;
WebView ourBrow;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
ourBrow = (WebView) findViewById(R.id.wvBrowser);
url = (TextView) findViewById(R.id.tvURL);
// cancel the web intent that default in android setting
ourBrow.setWebViewClient(new WebViewClient());
// webView seeting
ourBrow.getSettings().setJavaScriptEnabled(true);
ourBrow.getSettings().setLoadWithOverviewMode(true);
ourBrow.getSettings().setUseWideViewPort(true);
try {
ourBrow.loadUrl("https://www.google.com.au");
} catch (Exception e) {
e.printStackTrace();
}
url.setText(ourBrow.getUrl());
}
}
Why I can't get the return value form webView.geturl? The setting for the TextView is totally fine and the WebView worked as well, but when I launch the emulator, the url didn't show anything. Is there anything wrong with the ourBrow setting?
Their is no problem in your code it was gave you null due to the url is not load at that time when you are trying to set it on Text. you must set the text after load the url on web View to test your code just add a button and onClick of that button do same
url.setText(ourBrow.getUrl());
as you did in your code you got the url on text.
in order for your Activity to access the Internet and load web pages in a WebView, you must add the INTERNET permissions to your Android Manifest file:
<uses-permission android:name="android.permission.INTERNET"/>

Categories