Android, YUI, Moodle - java

The problem I'm having is the following:
I have an app with two separate modes: A WebView for browsing and a custom Canvas. The custom Canvas captures handwriting samples for language placement exams. Here's how it works. A user logs in to Moodle via the WebView. After they log in, they navigate to a Quiz inside Moodle. They click a link on one of the Quiz's questions and this launches an Intent which hides the WebView and shows the Canvas. The user then writes (using a stylus) on the Canvas. When a user is finished writing their essay (or whatever), they press a button that uploads an image file to Moodle. I am able to upload images to a point, it's getting them to show up in the HTML page that the user clicked the link in originally (see above) and to get Moodle to commit them to permanent storage that is the problem. Normally this is all accomplished through AJAX (really AJAJ since it's JavaScript and JSON) and when the user drops a file on this one component, the component refreshes and uploads the file.
Here is the problem: I need the WebView so that students can log in to Moodle through Shibboleth. But because the underlying JavaScript in the browser makes AJAX calls to the Moodle server and since the Java side of Android doesn't have access to the DOM, I have use the Apache HTTP components library to make some of the connections below basically to preserve the state of the HTML page in WebView.
In a desktop browser on, say, Windows, I use WebScarab to monitor the browser's requests and this is what I see: the browser uploads a file to Moodle via five successive calls to the following scripts:
POST https://[moodle website]/repository/repository_ajax.php [posts multipart form data]
POST https://[moodle website]/repository/draftfiles_ajax.php [posts some params]
GET https://[moodle website]/draftfile.php/[some_id]/user/draft/[some_id]/[somefilename.png] [returns an icon of the image for a filepicker from YUI]
POST https://[moodle website]/mod/quiz/processattempt.php [returns HTML page]
GET https://[moodle website]/mod/quiz/summary.php [returns HTML page]
Some of these scripts return, as you'd expect, JSON data since they're AJAX and not HTML. The final two calls (4 & 5) return HTML. Now, I can make all of those calls in succession in either the WebView or the Apache HTTP library, but if I do so with WebView, only JSON data is returned to the WebView in calls 1-3 (WebView treats the JSON data as a page and displays it wiping out whatever HTML page was displayed in it). If I capture and process the JSON data using the Apache HTTP library in Java, then the JavaScript components internal to the page do not get updated. If I split the calls so that I send only calls 4 & 5 to the WebView, the HTML merely returns WebView to the first question of the exam and Moodle acts as if I haven't uploaded anything.
I can verify that files are uploading if I manually refresh (press a link) the JavaScript UI elements in the page. I can't expect students to do this, though, because the link to do so is very tiny and it's not obvious that it does a refresh. I need a way to programmatically refresh this one element (it's part of YUI) or to get Android and the Java side to play more nicely with the JavaScript/DOM side.
My question is: does anyone know a way to 1) fire off a drag and drop event using YUI to an element inside an HTML page or 2) a way to consume the JSON data and pass it to an element inside the HTML page.
I'm banging my head against a wall trying to figure this out.
OK, so I figured out that: javascript:document.getElementsByClassName(\"[name of link here]\")[0].click() works in Chrome on the desktop but doesn't work if I pass it to WebView.loadURL(). I just need to be able to simulate that click event reliably in WebView. It appears not to support click(). Anyone have any ideas?

The winning code is:
el = document.getElementsByClassName("[some element]")[0];
var event = document.createEvent("MouseEvents");
event.initEvent("click", true, true);
el.dispatchEvent(event);
This selects the link at [some element] and thereby fires an AJAX request that refreshes the FilePicker. For those working with Moodle, I had to add the above code to the same quiz question that handles so it is invoked by putting that code in its own function and calling it with WebView.loadURL("javascript:myRefreshFunction()").

Related

how to pass data from activity to webview with external website

I am making an app for tracking courier. the user will enter tracking no.in apps textview and click submit this will open couriers website tracking page i.e fedex in a webview inside the App.
Now i want a way to pass those tracking no from my app to webview's website input field and click the submit button on the website automatically
I don't think this the way you should do that, you should look for a url that takes the tracking location number as a query and return the proper page
something like that http://example.com/over/there?number=441222
Update
I've looked at the page you've mentioned in the comments and all I can get is that it submits a post request as a form input
POST url: https://www.myutiitsl.com/PAN_ONLINE/PANTrackerSearch.action
parameter names are
appNo
panNo
I wish I had more knowledge as a web developer but this the way that could work with you try to make sure the post url is correct and the parameters I mentioned also correct and it should work with you

Download in java a webpage which makes AJAX call to load

I am trying to get the Hotel reviews from different websites.
For Simple plain HTML Web Pages(like TripAdvisor ) i used JSoup and did like this
Jsoup.connect("foo.html").get();
For Pages which used Javascript for Loading (like Expedia ) i used a Selenium WebDriver and did something like
driver.get("foo.html");
driver.manage().timeouts().pageLoadTimeout(10,TimeUnit.SECONDS)
They were fine because they had links and i could use those links to crawl and get more reviews.
and
The problem i face is when Downloading from pages which make AJAX calls (like MakeMyTrip ).
Here i do not know how to download the web page as the hotels list which is there on the page and keeps coming as we scroll down.
Any Suggestions would be of great help.
Solved it by using the url to which ajax call is done.
Example:
For a hotel with Id 200703241029455940 ( which comes from the main page) we get the review from the url.
http://hotelz.makemytrip.com/makemytrip/site/hotels/detail/responsive/hotelMmtReviews?hotelId=200703241029455940&start=10&rows=10&reviewsType=all
A GET request to the URL returns back a JSON array of reviews and thus i could extract the hotel reviews .

call java function from JSP page's button on click event

There is an application that stores photos in oracle db (oracle multimedia format). There is a JAVA servlet page that handle the connection and the SQLs ..., and there are JSP pages to show the images in browser.
There is a delete function in the JAVA page, when i click one thumbnail it navigates to the an other JSP and show the full size image. Here i would like to call the
public void deleteRowById( String selectId ) function which is in the JAVA servlet page. i would like to use a button, and i'm not realy know what is the proper way to call this function.
Any suggestions are welcome.
In the mean time i solved the situation. There is a java class that contains SQL statements -taht's what i would like to call with a form button.
1st JSP page show the button like delete, it navigates to an other JSP page via passing a parameter with post method, in this 2nd JSP page i call the main java class method that execute a delete statement then from the 2nd JSP page i navigate back with <jsp:forward page="1st_page.jsp"/>
Maybe not a cutting edge solution but works like charm :)

Android: WebView: Change HTML requested by Submit (GET)

I have a website loaded in WebView on Android 1.6 and I want to get the HTML-Code of a Site which was generated when the user hits Submit. The parameter get submitted by GET, if that's important.
Then I want to change the HTML-Code and show it to the user, without showing the real Answer-page first.
Maybe I could use onFormResubmission() from WebViewClient? But I really have no idea how.
Thanks for the help!
This is not possible with WebView's API, because you cannot "get the HTML-Code of a Site" that was retrieved by WebView. You are welcome to perform your own HTTP operations using HttpClient and feed the results to WebView via loadData() or loadDataWithBaseURL().

jquery and java communication

I have jquery pop form to upload a file, after on submit (the page refresh and the pop close) i check something about the file and then if there's something wrong i need to pop up that form again (from the java code?), how could i do that ?
You should use ( or must be using) ajax in jquery with a popup.
When the user hits "submit", control goes to server side code.
The code runs to upload the file.
Whatever the result of upload (success/failure), that message is sent to the popup with ajax automatically.
In case, there is problem in uploading the file then, along with the failure message, you can send in the div which contains your form.
I think rather than refreshing the page to close the popup, allow the user to close the popup with close button.
When "something is wrong" the server-side code (this applies to any language) should include within the HTML content Javascript that will trigger the "form" to be displayed again.
As I feel dizzy presently,can't write the code,but will try to break whole procedure in multiple steps:-
On Trigger(by some event) a pop up form will open from submission,which will have a button which will be calling a OnClick Event,which will be containing an Ajax call for client server communication
till the response don't close or fade out the Pop up box.
from server expect two tags SUCCESS or ERROR
a) On SUCCESS, remove form DIV and fade out/close the pop up
with a success message b)On Error,display a refreshed form DIV
And so on

Categories