Can´t see Counchdb via Android´s webview - android

Is it possible to get a Android WebView to open a port like: 5984?
I need to get some jquery from the 'assets-folder' to $.get() my counchdb json-data via the webview.
But first I just make a little test to see if android´s webview could get a response from counchdb:
webview.loadUrl("http://127.0.0.1:5984");
If I use the android´s build-in browser then I get:
{"couchdb":"Welcome","version":"1.2.0a-7b47329-git"}
But NOT via Android´s WebView, I just get a blank screen(see code) :(
A little update:
I look like if I switch for Vertical to horizontal, then
I get the data from couchdb(see log).
So maybe couchdb need to be in it own thread..

it look like couchdb must have some time to startup... so if you just put all your couchdb request inside, func like jquerys onclick(), or keyup()... then all is god.

Related

Xamarin Forms get WebView content as string

Is it possible to get the content of a WebView as a string?
I tried using:
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(Constants.LOGIN_URL);
HttpWebResponse response = (HttpWebResponse)await httpWebRequest.GetResponseAsync();
But I didn't get what i wanted because the webview stores the sessions etc.
I want a method to get the data of a logged in user from a website if this is possible.
Since you need to download the content of the WebView in the context of a logged in user, you may want to try injecting JavaScript into the WebView and then having the JavaScript send code back to a C# method.
An example of something like this can be found in XLabs' Hybrid WebView (which is the example I used to call JavaScript from C# methods).
Have a look at the URL below. You will need to create a custom renderer for each platform that you support but that should just be a matter of copying and pasting from the link below.
Once you have you custom renderer all set, you can simply pull all of the HTML using a JavaScript method.
https://github.com/XLabs/Xamarin-Forms-Labs/wiki/HybridWebView
Reply back if you run into any road blocks.

oAuth support in a WebView

So I've created a simple WebView application that wraps an already existing mobile friendly site and displays it on the device. I've enabled javascript, supported screen orientation changes, etc...
I've run into an issue with the oAuth support though. Accessing the site from the chrome browser on the device, everything runs fine.
If I try to access the site from the app/WebView, it will push me over to the oAuth screen, let me input credentials and everything, but the moment it tries to push me back to the website and log me in, I get this:
Failed to recognize URL query:
https://exittix.com/frontend/login/redirect.html#access_token=******************************&expires_in=********&state=****client_id=******************network*****facebook*****display***popup****callback****_hellojs_agj27sx5****state****oauth_proxy***https***auth-server.herokuapp.com%2Fproxy***scope***basic_profile***email***basic***oauth***version***auth***https***facebook.com%2Foauth***
The * is used to protect data.
So, any ideas why oAuth isn't working inside a JS enabled webview but works fine in the mobile chrome browser for android?
Thanks in advance for your help!
EDIT:
Ok, so I've tracked the error I'm getting back to redirect.html.
This page calls some javascript. If the javascript fails to redirect, then it displays that error I have above instead.
The javascript being called to handle the oAuth is Andrew Dodson's hello.js script.
You can see it HERE.
I've concluded that the second half of the error's url is indeed the unhandled JSON.
Here's what the returned data looks like after I've decoded it from the URL encoded characters:
{"client_id":"************.apps.googleusercontent.com","network":"google","display":"popup","callback":"_hellojs_********","state":"","oauth_proxy":"https://auth-server.herokuapp.com/proxy","scope":["https://www.googleapis.com/auth/userinfo.profile","https://www.googleapis.com/auth/userinfo.email","basic"],"oauth":{"version":2,"auth":"https://accounts.google.com/o/oauth2/auth"}}&access_token=***.*.*****_*********************************************************&token_type=Bearer&expires_in=3600
Any ideas why this isn't getting handled properly in the WebView?

Accessing JS code from an android Native app

This might seem to be a weird problem, but I am curious to know if it would work. I am working on a POC, and hence have to either prove or disprove that this works or not.
The UI in the Android app would be native (Java + XML layouts) + some other device features access like (Camera/File system etc).
There is a JS library that I have built, that has a few functions which do Ajax post and get requests.
In the app, I have an invisible Webview, where I load a blank HTML (referencing this JS library). And into that WebView, I have injected a JavascripInterface. So, essentially, the UI would be native, and you would never see the Webview. That's just a host which provides access to my JS library to the native code.
Now, on some action on my UI, I call the JS functions on the Webview, which in turn tries to make an ajax call (loadUrl calls ex. javascipt:functionName()). But, those calls fail, without any visible errors.
Note: This same HTML file works, if I load it up on my desktop browser. The AJAX calls succeed.
But, when I initiate Ajax calls through the JavascriptInterface(or webview.loadUrl() calls), they fail, with a reponse status 0.
Things apart from AJAX, like simple function calls, alerts, and callbacks through javascript interface work fine though.
Q: I know this is a weird and an unpractical way to do things. But, would it/should it work?
Update: Even after setting the setBlockNetworkLoads(false), it still doesn't work.
I tried logging the JS calls and errors, and got this error.
Request header field X-Requested-With is not allowed by
Access-Control-Allow-Headers.
Any idea how to solve this?
It seems that your are trying to do a cross domain ajax request.
Cross domain requests are not allowed by same origin policy and so the requests will be blocked. If you are loading a local file in webView and then sending ajax requests from it to other domains, this will be the case.
You if that is the case and it is the same origin policy causing you trouble then you might want to look at Cross-origin Resource Sharing (CORS) or JSONP to workaround it.
Given the error you get it seems that your problem is similar to one discussed here:
Cross-Domain AJAX doesn't send X-Requested-With header
You might want to change server settings to allow X-Requested-With header.
Also it seems that from API level 16, webSettings added a method setAllowFileAccessFromFileURLs(). Setting this to true for the webView might solve the problem as well.
I had a similar issue where I was loading a "web-app" locally into a WebView, just doing Ajax remotely. I observed a similar problem where Javascript alerts etc worked fine, but AJAX calls didn't. It turned out that by default the WebView blocks "network loads".
Make sure you do this:
webView.getSettings().setBlockNetworkLoads(false);
That did it for me. Just to clarify, I wasn't using a Javascriptinterface - just loading a web-app as-is using webView.loadDataWithBaseUrl() - the baseUrl parameter passed to this method was where I perform all my AJAX calls (since this method respects the same origin policy)

what the reason behind <GATE-M>DEV_ACTION_ERROR</GATE-M> android logs

My application is using web view and every thing is working fine(no force close,no visible error) but in log cat i am getting unlimited and continuous logs like.
09-06 09:05:27.464: I/GATE(3471): <GATE-M>DEV_ACTION_ERROR</GATE-M>
my web view renders a local html file which resides in asset folder.From Android native code i am sending some string to webview to call a java script function.
because of unlimited logs i am not able to see my custom log message.
Can any please tell me the reason so can eliminate if i did any thing wrong.
I am loading the WebView using the Url from server. In my case it is not html file. Just check the input stream from the server. if any content is there then set webview otherwise dont.

Android webview.loadUrl works with one URL GET string but not Another

I am new at Java and Android so thank for you help.
I wrote a simple Android WebView app to view my home webcam. I can get webview to return a snapshot of the webcam but not send controls to pan etc.
The line that works is
webview.loadUrl("http://myhost.com/snapshot.cgi?user=myusername&pwd=mypassword");
This works and returns a snapshot of my current webcam a FOSCAM
This line DOES NOT WORK!
webview.loadUrl("http://myhost.com/decoder_control.cgi?command=28?user=myusername&pwd=mypassword");
The above line uses that same username / password but returns a HTTP 401 Unauthorized error
WHAT is going on.. they both work fine in a browser? and in CURL
It's likely the second question mark... Why is that there? I imagine it should be an ampersand unless you have some weird protocol.

Categories

Resources