How to Modify content of resource response android webview - android

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 ;(

Related

how to modify the value of the key 'X-requested-with' in the request headers of resource request in Android webview, but not a page request?

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.

Android webView; Not displaying image having relative path

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)

Android, Webview, get submitted form data

I want my Android application to use Webview to display an html form. When the user presses the Submit button, it want the name/value pairs sent back to my program. How do I do this?
I have looked at other similar questions but not of the responses tell how, specifically, to do this.
Look into the use of Javascript interfaces within WebView. Please see this area of the Android Developers Guide. Basically, you should be able to communicate from the page to the device with the Javascript interface and let your WebView Activity know when the submit button is pressed and send its values over.
I have found what I needed. If I set action='FORM" in the form html, then I can intercept the action with:
public void onLoadResource (WebView view, String url){
int index = url.indexOf("FORM?");
if (index != -1){
String d = URLDecoder.decode(url.substring(index+5));
}
}
The name/value pairs are in String d.
If you want to save Name/Value pairs and are willing to use jQuery then it's quite simple.
Assuming you put jQuery in your assets folder you can read it into a file from:
getResources().getAssets()
In the WebViewClient that you set (assuming you overrode the client) you can do:
view.loadUrl("javascript:" + escapedJqueryStringHere);
Then, make sure you have a JavascriptInterface configured for your webview with a method, for example, called 'ReturnJSONFromForm(String json)' configured for "MyJInterface" then load the form into the page and then try this:
webview.loadUrl("javascript:MyJInterface.ReturnJSONFromForm($(\"form\").serialize());");
Now in your interface, just create a JSONObject from the resultant string and there you go! Name/Value pairs from your form.
Serialize won't capture disabled fields or submit buttons.. if you need that, then a more complex javascript solution exists but it's far more difficult.

How to use an XML data from an URL link?

The title maybe a little confusing so I shall explain a lot more details here. First off, I have an URL link that uses HTTPS secured site. I already managed a successful attempt to use a WebView to connect to the site, using loadDataWithBaseURL method.
The URL link does not end with .xml, so I am stuck with this problem. What are the procedures to do in order I can use the xml data on an URL link that does not ends with .xml?
EDIT:
<MGMT>
<NET>
<HEAD>
<ClientID>99999999</ClientID>
<ServerID>WEB_01</ServerID>
<Rsp>00</Rsp>
<Auth></Auth>
</HEAD>
<STAT>
<IP>192.168.5.158</IP>
<Status>OK, Success!</Status>
</NET>
</MGMT>
I do have XML parsing knowledge. But that is if the url link returns a XML data or I have an XML file. This is NOT THE ACTUAL LINK but it is similar, I changed a few values in it.
https://192.168.0.254/?ClientID=999999&Cert=0f7a248e3b017effec2b36cf53912b0f&IP=192.168.5.128
Ok. As i come to know about your question, you have to gain some knowledge about xml parsing which convert the fetched XML data in to specific variables.
Here in below examples, the project get the Http responce from the server which is in the formate of the xml file. (Which URL not having .XML extention). And that data are stored in to specific variable.
You have to fetch various data based on the tab as per your xml structure. as like "MGMT", "NET" . . .etc etc. . .
See this: Example 1
Also see this: Exapmple 2
Hope you got the Logic to do it.

Determine element of url from webview shouldOverRideUrlLoading

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().

Categories

Resources