Development log-detailed link server code (LOGIN), log Link

Source: Internet
Author: User

Development log-detailed link server code (LOGIN), log Link

Connect to the server, enter the password account, and the login page is the basic functions of the APP, summarized below:

User Logon Interface

/*** @ Author think * User Logon interface */public class LoginUi extends Activity implements HttpCallBack {private EditText emailET; private EditText passwordET; private ImageButton loginIB; private TextView registerTV; private TextView pwdForgetTV; private User user; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); setContentV Iew (R. layout. activity_login); initViews (); setListener () ;}< span style = "background-color: rgb (255,204,255);"> private void setListener () {loginIB. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {UserLogin userlogin = null; try {userlogin = (UserLogin) BeanFactory. getInstance ("UserLogin");} catch (Exception e) {BasicUtil. showMassage (LoginUi. this, "sorry, class not found ");} GetUserData (); if (user = null) {return;} userlogin. loginResult (user, LoginUi. this) ;}}); </span> registerTV. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {startActivity (RegisterUi. class) ;}}); pwdForgetTV. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {startActivity (FindPasswordUi. class) ;}}) ;}/ *** initialization component */private void initViews () {e MailET = (EditText) findViewById (R. id. et_login_email); passwordET = (EditText) findViewById (R. id. et_login_password); loginIB = (ImageButton) findViewById (R. id. ib_login_submit); registerTV = (TextView) findViewById (R. id. TV _login_register); pwdForgetTV = (TextView) findViewById (R. id. TV _login_forgetpassord); // TODO initialization component}/*** get user login data */private void getUserData () {String username = emailET. getText (). toString (); Str Ing password = passwordET. getText (). toString (); if (TextUtils. isEmpty (username) {BasicUtil. showMassage (this, "email cannot be blank"); return;} if (TextUtils. isEmpty (password) {BasicUtil. showMassage (this, "password cannot be blank"); return;} user = new User (username, password);} private void startActivity (Class <?> Clazz) {Intent intent = new Intent (this, clazz); startActivity (intent) ;}@ Overridepublic boolean onMenuItemSelected (int featureId, MenuItem item) {return super. onMenuItemSelected (featureId, item) ;}@ Overrideprotected void finalize () throws Throwable {super. finalize () ;}@ Override <span style = "background-color: rgb (255,204,255);"> public void callBack (Object object Object) {Message message = handler. obtainMessage (); message. what = 1; message. obj = object; handler. sendMessage (message);} Handler handler = new Handler () {@ Overridepublic void handleMessage (android. OS. message msg) {// TODO Auto-generated method stubswitch (msg. what) {case 1: System. out. println ("Result:" + msg. obj); // here is the result you get. You can modify the UI if (Boolean) msg here. obj = true) {startActivity (MainUi. class);} else {BasicUtil. showMassage (LoginUi. this, "Logon Failed") ;}break; default: break ;}};</span>}

Callback Function interface HttpCallBack

public interface HttpCallBack {void callBack(Object object);}

User Logon Service UserLogin

/*** @ Author think User Logon Service class */public class UserLogin {/*** User Logon Service ** @ param userinfo * @ return logon result */public void loginResult (User userinfo, httpCallBack httpCallBack) {ArrayList <NameValuePair> pairs = getPairs (userinfo); String uri = "http: //" + ConstValue. IP + ":" + ConstValue. PORT; try {ApacheHttpClient. httpPost (uri, pairs, httpCallBack);} catch (Exception e) {e. printStackTrace () ;}}/*** @ param userinfo * encapsulate User logon information */private ArrayList <NameValuePair> getPairs (User userinfo) {ArrayList <NameValuePair> pairs = new ArrayList <NameValuePair> (); NameValuePair pair = null; pair = new BasicNameValuePair ("username", userinfo. getEmail (); pairs. add (pair); pair = new BasicNameValuePair ("password", userinfo. getPassword (); pairs. add (pair); return pairs ;}}

Send Http request ApacheHttpClient

/*** @ Author think sends an Http request */public class ApacheHttpClient {/*** @ return **/public static String httpGet (String uri) {String response = null; // response HttpClient httpClient = new DefaultHttpClient (); // create an HttpGet object HttpGet httpGet = new HttpGet (uri); HttpResponse httpResponse; try {// use the execute method to send an http get request, and return the HttpResponse object httpResponse = httpClient.exe cute (httpGet); int statusCode = httpResponse. getStatusLine (). getStatusCode (); // return code //, if (statusCode = HttpStatus. SC _ OK) {// get the returned result response = EntityUtils. toString (httpResponse. getEntity ();} else {response = "return code:" + statusCode;} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace ();} System. out. println (response); return response;}/*** send the request in Post mode ** @ param url * request address * @ param params * parameter, the Post method must use the NameValuePair [] Array Storage parameter * @ return * @ throws Exception */<span style = "background-color: rgb (255,204,255 ); "> public static void httpPost (final String uri, final List <NameValuePair> params, final HttpCallBack httpCallBack) throws Exception {new Thread (new Runnable () {public void run () {String response = null; HttpClient httpClient = new DefaultHttpClient (); HttpPost httpPost = new HttpPost (uri); try {// set the httpPost request parameter httpPost. setEntity (new UrlEncodedFormEntity (params, HTTP. UTF_8); // use the execute method to send an HTTP Post request and return the HttpResponse object. There will be a delay waiting for HttpResponse httpResponse = httpClient.exe cute (httpPost); int statusCode = httpResponse. getStatusLine (). getStatusCode (); // return code, if (statusCode = HttpStatus. SC _ OK) {response = EntityUtils. toString (httpResponse. getEntity (); JSONObject obj = new JSONObject (response); Boolean result = (Boolean) obj. opt ("success"); Log. I ("----", "result =" + result); httpCallBack. callBack (result); // callBack} else {response = "return code:" + statusCode;} catch (Exception e) {e. printStackTrace ();}}}). start (); // get --> opt // ToDo: parse obj }}</span>



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.