Android mobile phone registration and logon (client part) using simple Tomcat server combined with MysqL
Package com. example. java4androidmysql; import java. io. IOException; import java. io. unsupportedEncodingException; import java. util. arrayList; import java. util. list; import org. apache. http. httpResponse; import org. apache. http. httpStatus; import org. apache. http. client. clientProtocolException; import org. apache. http. client. httpClient; import org. apache. http. client. entity. urlEncodedFormEntity; import org. apache. http. client. methods. httpPost; import org. apache. http. conn. params. connManagerParams; import org. apache. http. impl. client. defaultHttpClient; import org. apache. http. message. basicNameValuePair; import org. apache. http. params. basicHttpParams; import org. apache. http. params. httpConnectionParams; import org. apache. http. params. httpParams; import org. apache. http. protocol. HTTP; import org. apache. http. util. entityUtils; import android. OS. asyncTask; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. view. menu; import android. view. view; import android. view. window; import android. widget. button; import android. widget. editText; import android. widget. toast; public class MainActivity extends Activity {public static final String URL =" http://192.168.2.1:8080/JavaRegisterMysql/login "; Public static String res =" "; public static String name =" "; public static String code =" "; public static String phone = ""; public static String chose = "0"; Button connectButton = null; Button registerButton = null; EditText noteUser = null; EditText notePassword = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. activity_main); connectButton = (Button) findViewById (R. id. btnConnect); registerButton = (Button) findViewById (R. id. btnRegister); noteUser = (EditText) findViewById (R. id. editUserName); notePassword = (EditText) findViewById (R. id. editPassword); // Note Class DriverManager Statement ResultSet connectButton. setOnClickListener (new View. onClickListener () {public void onClick (View v) {Toast. makeText (MainActivity. this, "logging in", Toast. LENGTH_SHORT ). show (); name = noteUser. getText (). toString (). trim (); code = notePassword. getText (). toString (). trim (); chose = "1"; new submitasynctask(.exe cute (URL); Toast. makeText (MainActivity. this, "res =" + res, Toast. LENGTH_SHORT ). show (); if (res. equals ("2") {Toast. makeText (MainActivity. this, "logon successful", Toast. LENGTH_SHORT ). show (); // Intent intent = new Intent (); // intent. setClass (MainActivity. this, WriteNote. class); // MainActivity. this. startActivity (intent);} else if (res. equals ("1") {Toast. makeText (MainActivity. this, "incorrect user name or password", Toast. LENGTH_SHORT ). show ();} else if (res. equals ("3") {Toast. makeText (MainActivity. this, "registration failed", Toast. LENGTH_SHORT ). show ();} else if (res. equals ("4") {Toast. makeText (MainActivity. this, "registration successful", Toast. LENGTH_SHORT ). show ();} else if (res. equals ("-1") {Toast. makeText (MainActivity. this, "network exception", Toast. LENGTH_SHORT ). show () ;}}); noteUser. setText ("AB"); notePassword. setText ("123456"); registerButton. setOnClickListener (new View. onClickListener () {public void onClick (View v) {Intent intent = new Intent (); intent. setClass (MainActivity. this, Register. class); MainActivity. this. startActivity (intent) ;}}) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
The above is the function call on the mobile phone's main interface.
Package com. example. java4androidmysql; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. window; import android. widget. button; import android. widget. editText; import android. widget. toast; public class Register extends Activity {public static final String URL =" http://192.168.2.1:8080/JavaRegisterMysql/login "; Button backButton = null; Button registerButton = null; EditText user = null; EditText password = null; EditText phone = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. register); backButton = (Button) findViewById (R. id. btnBack); registerButton = (Button) findViewById (R. id. btnRegister); user = (EditText) findViewById (R. id. editUserName); password = (EditText) findViewById (R. id. editPassword); phone = (EditText) findViewById (R. id. editPhone); // Note Class DriverManager Statement ResultSet registerButton. setOnClickListener (new View. onClickListener () {public void onClick (View v) {MainActivity. name = user. getText (). toString (). trim (); MainActivity. code = password. getText (). toString (). trim (); MainActivity. phone = phone. getText (). toString (). trim (); MainActivity. chose = "2"; new submitasynctask(cmd.exe cute (URL); Toast. makeText (Register. this, "res =" + MainActivity. res, Toast. LENGTH_SHORT ). show (); if (MainActivity. res. equals ("2") {Toast. makeText (Register. this, "logon successful", Toast. LENGTH_SHORT ). show (); // Intent intent = new Intent (); // intent. setClass (MainActivity. this, WriteNote. class); // MainActivity. this. startActivity (intent);} else if (MainActivity. res. equals ("1") {Toast. makeText (Register. this, "incorrect user name or password", Toast. LENGTH_SHORT ). show ();} else if (MainActivity. res. equals ("3") {Toast. makeText (Register. this, "registration failed", Toast. LENGTH_SHORT ). show ();} else if (MainActivity. res. equals ("4") {Toast. makeText (Register. this, "registration successful", Toast. LENGTH_SHORT ). show ();} else if (MainActivity. res. equals ("-1") {Toast. makeText (Register. this, "network exception", Toast. LENGTH_SHORT ). show () ;}}); user. setText ("gjw"); password. setText ("123"); phone. setText ("13570236302 ");}}
The above is the registration interface
Package com. example. java4androidmysql; import java. io. IOException; import java. io. unsupportedEncodingException; import java. util. arrayList; import java. util. list; import org. apache. http. httpResponse; import org. apache. http. httpStatus; import org. apache. http. client. clientProtocolException; import org. apache. http. client. httpClient; import org. apache. http. client. entity. urlEncodedFormEntity; import org. apache. http. client. methods. httpPost; import org. apache. http. conn. params. connManagerParams; import org. apache. http. impl. client. defaultHttpClient; import org. apache. http. message. basicNameValuePair; import org. apache. http. params. basicHttpParams; import org. apache. http. params. httpConnectionParams; import org. apache. http. params. httpParams; import org. apache. http. protocol. HTTP; import org. apache. http. util. entityUtils; import android. OS. asyncTask; public class SubmitAsyncTask extends AsyncTask
{String info = ""; protected String doInBackground (String... params) {// TODO Auto-generated method stub String url = params [0]; String reps = ""; reps = doPost (url); return reps ;} protected void onPostExecute (String result) {// TODO Auto-generated method stub MainActivity. res = result. trim (); super. onPostExecute (result);}/*** use the Post method to transmit data to the server * @ param url * @ return */private String doPost (String url) {String responseStr = ""; try {// send a post request HttpPost httpRequest = new HttpPost (url); HttpParams params = new BasicHttpParams (); ConnManagerParams. setTimeout (params, 1000); // Obtain the connection timeout value from the connection pool HttpConnectionParams. setConnectionTimeout (params, 3000); // timeout value for establishing a connection with the server over the network HttpConnectionParams. setSoTimeout (params, 5000); // The timeout value of the read response data. httpRequest. setParams (params); // The following describes how to transmit data with the server and use BasicNameValuePair List.
ParamsList = new ArrayList
(); ParamsList. add (new BasicNameValuePair ("NAME", MainActivity. name); paramsList. add (new BasicNameValuePair ("CODE", MainActivity. code); paramsList. add (new BasicNameValuePair ("PHONE", MainActivity. phone); paramsList. add (new BasicNameValuePair ("CHOSE", MainActivity. chose); UrlEncodedFormEntity mUrlEncodeFormEntity = new UrlEncodedFormEntity (paramsList, HTTP. UTF_8); httpRequest. setEntity (mUrlEncodeFormEntity ); //////////////////////////////////////// /////// HttpClient httpClient = new DefaultHttpClient (); httpResponse httpResponse = httpClient.exe cute (httpRequest); final int ret = httpResponse. getStatusLine (). getStatusCode (); if (ret = HttpStatus. SC _ OK) {responseStr = EntityUtils. toString (httpResponse. getEntity (), HTTP. UTF_8);} else {responseStr = "-1" ;}} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (ClientProtocolException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} return responseStr ;}}
The above is an interface class, but it may be written incorrectly (for example, the last called variable is returned). I am a little busy and try again later.
"
"
The above two sections of code are the layout file code.