How to obtain data from webpage forms in Android

Source: Internet
Author: User

The specific implementation code for getting the data in the form on the webpage in Android is as follows. If you are interested, refer to the following code. I hope to help you with MainActivity as follows:
The Code is as follows:
Package cn. testjavascript;
Import java. util. StringTokenizer;
Import android. OS. Bundle;
Import android. webkit. WebView;
Import android. app. Activity;
/**
* Demo description:
* Get the data in the form on the webpage in Android
*/
Public class MainActivity extends Activity {
Private WebView mWebView;
Private String date = null;
Private String email = null;
Private String username = null;
Private String sex = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Init ();
}
Private void init (){
MWebView = (WebView) findViewById (R. id. webView );
InitWebViewSettings ();
MWebView. loadUrl ("file: // android_asset/form.html ");
// Note the second parameter in the addJavascriptInterface Method
// It represents the alias of our java object javaClass.
// In this way, Javascript can use this alias to call methods in Android.
// Window. testform. send (date + "|" + email + "|" + name + "|" + sex) in Javascript code );
// Send is the method name.
// Testform is an alias
MWebView. addJavascriptInterface (new Object (){
Public void send (String userInfo ){
StringTokenizer userInfoStringTokenizer = new StringTokenizer (userInfo, "| ");
Date = userInfoStringTokenizer. nextToken ();
Email = userInfoStringTokenizer. nextToken ();
Username = userInfoStringTokenizer. nextToken ();
Sex = userInfoStringTokenizer. nextToken ();
System. out. println ("userInfoStringTokenizer =" + userInfoStringTokenizer. toString ());
System. out. println ("date =" + date );
System. out. println ("email =" + email );
System. out. println ("username =" + username );
System. out. println ("sex =" + sex );
};
}, "Testform ");

}
Private void initWebViewSettings (){
MWebView. setVerticalScrollBarEnabled (false );
MWebView. setHorizontalScrollBarEnabled (false );
MWebView. getSettings (). setJavaScriptEnabled (true );
MWebView. getSettings (). setsuppzoom zoom (true );
MWebView. getSettings (). setDomStorageEnabled (true );
MWebView. getSettings (). setPluginsEnabled (true );
MWebView. requestFocus ();
MWebView. getSettings (). setUseWideViewPort (true );
MWebView. getSettings (). setLoadWithOverviewMode (true );
MWebView. getSettings (). setsuppzoom zoom (true );
MWebView. getSettings (). setBuiltInZoomControls (true );
}

}
Main. xml is as follows:
The Code is as follows:
<RelativeLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
>
<WebView
Android: id = "@ + id/webView"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: layout_centerInParent = "true"
/>
</RelativeLayout>
Form.html is as follows:
The Code is as follows:
<Body>
<Form action = "" method = "post">
Time: <br>
<Select id = "shijian" name = "date">
<Option value = "2011"> 2011 </option>
<Option value = "2012"> 2012 </option>
<Option value = "2013"> 2013 </option>
<Option value = "2014"> 2014 </option>
<Option value = "2015"> 2015 </option>
</Select> <br>
Email:
<Input id = "email" type = "text" name = "emailID"/>
<Br>
Nickname:
<Input id = "name" type = "text" name = "username"/>
<Br>
Gender: <br>
<Input id = "men" type = "radio" name = "sex" value = "men"/> male
<Input id = "women" type = "radio" name = "sex" value = "women"/> female
<Br>
<Input type = "submit" value = "register" onclick = "f ()"/>
<Input type = "button" value = "cancel"/>
</Form>
</Body>
<Script type = "text/JavaScript" language = "javascript">
Function f (){
Var email = document. getElementById ('email '). value;
Var name = document. getElementById ('name'). value;
Var date = document. getElementById ('shijian '). value;
If (document. getElementById ('men'). checked &&! Document. getElementById ('women'). checked ){
Var sex = document. getElementById ('men'). value;
} Else if (! Document. getElementById ('men'). checked & document. getElementById ('women'). checked ){
Var sex = document. getElementById ('women'). value;
}
Window. testform. send (date + "|" + email + "|" + name + "|" + sex );
}
</Script>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.