While trying to use loadUrl(String url, Map additionalHttpHeaders) method of webView, the response that I receive is not able to display the image. Instead of the image, a question mark substitute is used.
So, I tried to hit the same URL using Java code. The HTML code received during this process showed that the tag's src attribute was using a relative location.
On passing this same HTML code to webView's loadData() method, I get the same output as mentioned in the first case.
Anyway to fix this issue? (In the additionalHttpHeaders I was passing the authorization header)
Related
What I'm trying to do is to intercept the response data and modify the response from a particular URL.
For example, when loadUrl("http://foo.bar") is called to a WebView, let's say that it will load "page.html", "data.json", "someimage.png", and so on. In this case, how can I modify the contents of data.json when the resource is fetched, and pass the modified file to the WebView?
I think shouldInterceptRequest might be the answer, but I can't figure out how to get a WebResourceResponse from a WebResourceRequest.
I'm sorry if my English is awkward ;(
I'm trying to load an static map using this url:
http://maps.googleapis.com/maps/api/staticmap?center=43.137022,13.067162&zoom=16&size=600x400&maptype=roadmap&sensor=true&markers=color:blue|43.137022,13.067162
I'm doing it with Square's Picasso, but it fails to load.
With some tests I've to the conclusion that the char | is the one messing up Picasso. Any idea on how to overcome this issue?
Picasso appears to expect a URL-encoded URL. This means that the values of form variables need to be URL-encoded, the way they would if this were a submitted HTML form.
Alphanumeric characters do not need escaping, which is why most of your URL is fine. However, the markers parameter contains special characters, particularly that |, which need to be converted into URL-encoded values.
If you were programmatically generating the URL by pieces, you might use URLEncoder and encode() to handle this conversion for you.
I have met a problem when I capture the http package when my webView visit a url. the package shows that the request send by ajax has a 'X-requested-with' key in the headers, which has the value of my app package name like 'com.xxx'. I don't want the url I visited to know the request came from my app, so I need to replace the key 'X-requested-with' to another value.but I tried the sloution in Add custom headers to WebView resource requests - android, all of them do not work.
loadUrl(Strring url, Map<String, String> extraHeaders) will be called only in the page request, not in the resource request send by ajax.shouldOverrideUrlLoading(WebView view, String url) and WebViewClient.shouldInterceptRequest(android.webkit.WebView view, java.lang.String url) will be called in every request, but I can't modify headers in them.
So any solutions to the question? Thank you so much.
well, finally I solve the question. I can't find out the way to modify the headers in the request send by ajax in an loading page still, but I chose to override public String getPackageName() in Application. I look up the callingstack, if the calling comes from android.webkit, I return another string instead of my package name. So my issues solved. But the basic question still leaves.
Im making a simple app for some friends to use on there android phone that shows my website with images. Im using a webview to show the website inside the app. I want the users, to be able to save the actual image. Either by Hold down on the image or actually clicking a button. Been looking around on googles docs ands cant seem to find anything for this.
I haven't actually played around with this yet, but from the docs it appears you have at least two (non-deprecated) options:
getHitTestResult()
requestImageRef(Message msg)
According to the documentation you can use the first option to test for IMAGE_TYPE as result, and a url to the image is provided as well:
If a HTML::img tag is found, the HitTestResult type is set to
IMAGE_TYPE and the url is set in the "extra" field.
The second option will give you a similar result:
Request the url of the image last touched by the user. msg will be
sent to its target with a String representing the url as its object.
Not sure if these options are compatible with a 'long click' too though.
Alternatively, how did you have in mind to link clicking a button to a specific image? Depending on your solution for this, you may also be able to simply capture all loaded image resources using onLoadResource(WebView view, String url), build a list of image references and download the one that button click refers too.
I am displaying a html in a custom activity with a webview. I am overriding
public boolean shouldOverrideUrlLoading(WebView view, String url) {
to intercept certain urls. Now I would like to be able to figure out some more details about the html around that url.
In my case I would like to get the html element that contains the url. E.g. if the url comes from
Manfred Moser
I would like to be able to somehow retrieve the value "Manfred Moser" from within the anchor tag. Is there a way to do that. I found that you do not have access to the DOM from Java.
One way I can think of would be to download the page separately and parse it all before even loading it in webview. However that is an ugly hack at best. Is there a better way?
There is a dirty hack:
Bind some Java object so that it can be called from Javascript with WebView:
addJavascriptInterface(javaObjectExposed, "JSname")
Force execute javascript within an existing page by
WebView.loadUrl("javascript:window.JSname.passData("some data");");
Described here: http://lexandera.com/2009/01/extracting-html-from-a-webview/
Note: this is a security risk - any JS code in this web page could access/call your binded Java object. Best to pass some one-time cookies to loadUrl() and pass them back your Java object to check that it's your code making the call.
In addition to #Peter Knego's approach, there is the inverse:
Use addJavascriptInterface(), per his step #1
For your link, use an onClick attribute on your <a> tag to call out some method on the Java object from step #1, where the Java object turns around and loads the URL into the WebView, and supplies whatever sort of identifying information you want.
Suffice it to say, there's no way to get the information you want from shouldOverrideUrlLoading().