Jsoup - Android - Parse info from Form data / input - android

I am using Jsoup in my application and I am attempting to parse information from an a few input tags in order to add them to a url and post data automatically.
The portion of HTML I am attempting to parse is as follow:
<div class='theDivClass'>
<form method="post" id="handlePurchase" name="makePurchase" action="/shop.php">
<input type="hidden" name="ProductCode" value="A1223MN" />
<input type="hidden" name="SystemVersion" value="3" >
<input type="hidden" name="ProductClass" value="BOOK" />
</form>
</div>
The desired output would be
x = A1223MN
y = 3
z = BOOK
I am halfway familiar with JSOUP in the sense that I am able to parse out text, images, and urls but for some reason this is not clicking for me.
Any help would be greatly appreciated.

You should be able to use this:
Elements hidden = doc.select("input[type=hidden]");
And then just pull the attr values from each element in hidden. I've just tried it and it seems to work as expected.
For completeness:
Map<String,String> hiddenList = new HashMap<String, String>();
Elements hidden = doc.select("input[type=hidden]");
for (Element el1 : hidden){
hiddenList.put(el1.attr("name"),el1.attr("value");
}
Will give you a Map of all hidden input fields in the document.

Element.select("input[name=productCode]").attr("value");
Element.select("input[name=SystemVersion]").attr("value");
Element.select("input[name=ProductClass]").attr("value");

There's another way I found:
FormElement f = (FormElement) doc.select("form#handlePurchase").first();
System.out.println(f.formData());
Result:
[ProductCode=A1223MN, SystemVersion=3, ProductClass=BOOK]

Closing this question as it appears from all of the research I have done, you cannot pull data from "hidden" input types.

Related

Html5 input type number formatting

I have this page where i use the input type number. so that it will show the numeric keyboard on wp, android and ios device .
But my problem is that if the user use a culture info on the device for. lets say Denmark. the numeric keyboard shown use , instead of .
But the input number won't accept , only .
Anyone got a idea how this can be done?
Thanks for your time.
Try setting the lang attribute to en in the <html> element or the <input> element itself to force decimal point as input syntax. For example:
<input type="number" name="myNum" id="myNum" lang="en">
However the user's environment may even override this, in which case the only alternative would be to include a message requiring decimal point rather than comma.
This is a good write-up of locale handling for the number input type:
https://www.aeyoun.com/posts/html5-input-number-localization.html
UPDATE:
Another alternative would be to stop the form from validating the input by using the novalidate attribute in a <form> element, like so:
<form novalidate>
<input type="number" name="myNum" id="myNum">
<button id="submit">Submit</button>
</form>
This would allow anything, even non-numbers, so you'd have to set up the JavaScript code to check and handle the value.
UPDATE AGAIN:
Just thought of one more alternative — use the pattern attribute to do a regular expression check and use the plain old text input type. For example:
<input type="text" name="myNum" id="myNum" pattern="[0-9.]*">
Advantage: You get to control exactly what users can input.
Disadvantage: The keyboard on mobile devices will not be the numeric keypad.
Note that HTML input types can be spoofed so you still need to do server-side validation.
When retrieving the value with jquery it is returned with a . decimal separator. Try this:
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<input type="number" name="myNum" id="myNum">
<button id="submit">Submit</button>
<hr/>
<code></code>
<script>
$("#submit").on("click",function() {
var numericValue = $("#myNum").val();
$("code").html(numericValue);
});
</script>
</body>
</html>
http://output.jsbin.com/xixunewowe

WebView extract and edit html

I am creating a very simple WebView application on android. However, I want to edit the html file before displaying it in the WebView.
For example, if the original html source looked like :
<html>
<body>
<h1> abc </h1>
<h2> abc </h2>
......
<h6> abc </h6>
</body>
</html>
And I want to change it to:
<html>
<body>
<h1> cba </h1>
<h2> cba </h2>
......
<h6> cba </h6>
</body>
</html>
(all "abc" become "cba")
And then, I want to display that new code in my WebView. How can I do this? thanks
I am not sure why do you need this and what kind of app it is to need this. But if you have to do it check foll code:
$(function() {
for(var i =0;i<101;i++) {
if(jQuery('h'+i).length)
jQuery('h'+i).html(jQuery('h'+i).html().split("").reverse().join(""));
}
});
First, a note on your header tags: <h100> is a common misconception for newcomers. <h_> tags are simply an organizational item for your page, and only go out to <h6> You can have multiple <h1> tags on the same page, which are just headings for that section of content (with <h2> implying a subsection of <h1>, etc).
From there, when you say "original source", I assume you mean this is your own code, correct? Not a WebView sourced from another site? If this is the case, and you are only looking to change a specific instance of a specific string in your own code, a Find and Replace should be sufficient via any text or code editor you are using.
But if this is the case, you might want to look into first learning HTML and being able to render it in a basic web browser before moving on to also trying to learn Android.

JSOUP - troubles with extracting values from two divs

Hi I trying to get values from two hidden inputs. (__VIEWSTATE and __EVENTVALIDATION)
<form name="FormLogin" method="post" action="Same.aspx" id="FormLogin">
<div>
<input type="hidden" name="__OTHER" id="__OTHER" value="SOME not importent value" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/someLongValuewhatIwant=" />
</div>
<div>
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/someOtherValueWhatIwant" />
</div>
</form>
My code
doc = Jsoup.connect("http://example.com/index.aspx").get();
Elements input = doc.select("input[type=hidden]");
Element viewst = input.select("#__VIEWSTATE").get(0);
Element eventvd = input.select("#__EVENTVALIDATION]").get(0);
viewstate = viewst.val();
eventvalidation = eventvd.val();
But I always got only __VIEWSTATE value and my app crashed when i try to get __EVENTVALIDATION value. Can someone please explain me why ? and How to make it work ?
Jsoup always crashes android when the select matching expression cannot match any element in the given doc which in your case#__EVENTVALIDATION in not on your input element.
Check in your Elements input if #__EVENTVALIDATION exists.
Btw:In your code you can directly access any element by selecting #id tag. for example
doc = Jsoup.connect("http://instantgram.ic.cz/index.html").get();
Elements eventvd = doc.select("input[id =__EVENTVALIDATION");

Where is the inner implementation of "form submit" in webkit of Android

In a browser, if I want to submit a form containing a username and a password input, I only need to add an "action" attribute and set the "method" attribute to "post":
<form method="post" name="form" action="https://www.xxx">
<input id="username" type="text" value="xxxxxx" name="username">
<input id="password" type="password" autocomplete="off" name="password">
<input type="submit" value="submit" >
</form>
then the browser will handle the post and concatenate the password and username as the request and send to the server.
My question is: in the webkit (what I concern is the Android webkit, but I think others will be ok),where is the code of handling such process? Can I find the code that get the text from the input element, concatenate them, and then send to the server?
Thanks
where there's no answer, I finally find it.
For webkit, there's mainly four directory we need to consider in android:
for java part:
framework/base/core/java/androud/webkit
for native c part
external/webkit/Source/WebCore
external/webkit/Source/WebKit
external/webkit/Source/JavaScriptCore
for the form submit, the java part webkit will send a key event which will be handled by C WebCore.
we can see the code from
WebCore/html/HTMLFormElement.cpp
WebCore/loader/FormSubmission.cpp
WebCore/loader/FrameLoader.cpp

have form act as a simple redirect (HTML/PhoneGap)

I'm making a prototype and the manipulation of the url when submitting a form is causing me issues. I just want to redirect to the link but keep my styling.
<form method = "link" action="portal.html" class="forms">
<label>Username:</label>
<input type="text" name="username" />
<label>Password:</label>
<input type="password" name="password" />
<input type="submit" class="submit" />
</form>
I want the submit to act a a straight redirect.. w/ appending the ?username=&password= to the redirect.
If it makes a difference this is an html5 project for android using phonegap.
If I understood correctly, you just want to submit the form and go to another page, while appending the form's fields to the URL as a query string (?foo=bar&foo=bar). For that you have to declare your form's method as GET, like so:
<form method="GET" action="portal.html" class="forms">
EDIT: turns out the desired behavior is exactly the opposite.
To don't append a query string, you could set your form's method to POST
<form method="POST" action="portal.html" class="forms">

Categories

Resources