Personal andriod practice, personal online notebook

Source: Internet
Author: User
Personal online laptop

Personal information: He is a senior in software engineering at yanda University;

My blog: Google searches for "cqs_2012;

My hobbies: I love data structures and algorithms and hope to make my own contribution to algorithm work in the future;

Programming Language: Java;

Programming Environment: Windows 7 Professional Edition x64;

Programming tools: JDK, Eclipse, AVD;

Drawing tools: Office 2010 PowerPoint;

Hardware information: 7g-3 Notebook;


Introduction

The birth of my first online andriod work and the emergence of andriod career milestones.

Main technical points

Communication socket technology, multi-thread communication.

In socket use, once open, once closed, and once data transmission.

Switch between the socket and the main thread of the application;

The main thread is shackled. After data transmission is complete, the main thread is awakened in the socket thread to prevent the main thread from starting execution without waiting for data communication to complete.

Set a lock for the main thread;

// Thread lock (a global variable is defined in myapplication)
Public static string loginthreadstate = "off ";

Lock Operation

Myapplication. loginthreadstate = "off ";

Check Lock Operation

// Determine whether the current thread is awake

While (myapplication. loginthreadstate. Equals ("off "));

Unlock and wakeup operations

// Wake up the login process
Myapplication. loginthreadstate = "on ";
Myapplication. loginthreadstate. Y ();

Then we can implement communication, and wait for the data returned by the communication to be processed.

Personal Notes server socket implementation

Import java.net. *; import Java. io. *; import Java. text. simpledateformat; import Java. util. *; import dbdata. dataapplication; import dbdata. notebookclass; import dbdata. user; import dboperation. notedao; import dboperation. userdao; import datachange. notechange; public class myappnotebookinfor extends thread {// link variable Private Static int port; Private Static serversocket; static socket server; private data Inputstream input; private dataoutputstream output; static {try {// set the server port to port = 8899; serversocket = new serversocket (port);} catch (exception e) {e. printstacktrace () ;}} public myappnotebookinfor () throws exception {// system started successfully. out. println ("Accept ready"); server = serversocket. accept (); // The system is successfully started. out. println ("Accept start"); // obtain the data stream input = new datainputstream (server. getinputstream (); Output = new D Ataoutputstream (server. getoutputstream ();} // run the main function public void run () {try {// receives data string commonddata = getcommonddata (); // process data, and send the data if (commonddata! = NULL) selectcommond (commonddata); server. close ();} catch (exception e) {// todo: handle finished tione. printstacktrace () ;}}// the selection function not over Data Processing Public void selectcommond (string data) throws exception {// receives data and processes string formatdata [] = data. split (dataapplication. commondgetdatastyle); // select the operation command switch (integer. parseint (formatdata [0]) {// checkcase dataapplication. commond_check_user: {// data conversion user = new User (); User. setid (formatdata [1]); User. setpwd (formatdata [2]); // data processing boolean result = myappnotebookserver. checkuser (User); // The conversion Format String senddata = result. tostring (); // senddata (senddata);} break; // select-allcase dataapplication. commond_get_my_all_note: {// data processing string userid = formatdata [1]; arraylist <notebookclass> notelist = myappnotebookserver. getusernotelist (userid); string senddata = "Nu Ll "; if (notelist! = NULL & notelist. size ()> 0) // data conversion senddata = notechange. notelisttostringwithsendforselect (notelist); // send data senddata (senddata);} break; // New-notecase dataapplication. commond_insert_note: {// data conversion notebookclass = notechange. stringtonotebookwithgetforinsert (formatdata [1]); // Data Processing Integer result = myappnotebookserver. insertnote (notebookclass); // data conversion string senddata = result. tostring (); // Senddata (senddata);} break; // new usercase dataapplication. commond_insert_user: {// data conversion user = new user (); User. touser (formatdata [1]); // Data Processing Integer result = myappnotebookserver. insertuser (User); // data conversion string senddata = result. tostring (); // senddata (senddata);} break; Case dataapplication. commond_search_note_by_key: {// convert data string [] keyststrings = notechange. datatostringwithgetforselectb Ykey (formatdata [1]); string useridstring = keyststrings [0]; string keywordstring = keyststrings [1]; // arraylist for data processing <notebookclass> notelist = myappnotebookserver. getusernotelistbykeywords (useridstring, keywordstring); // data conversion string senddata = "null"; if (notelist! = NULL & notelist. size ()> 0) senddata = notechange. notelisttostringwithsendforselect (notelist); // send data senddata (senddata);} break; Case dataapplication. commond_alter_note: {// convert data notebookclass = notechange. stringtonotebookwithgetforalter (formatdata [1]); // data processing myappnotebookserver. alternotebook (notebookclass); arraylist <notebookclass> notelist = myappnotebookserver. getusernotelist (noteb Ookclass. getuserid (); string senddata = "null"; if (notelist! = NULL & notelist. size ()> 0) // data conversion senddata = notechange. notelisttostringwithsendforselect (notelist); // send data senddata (senddata);} break; Case dataapplication. commond_delete_note: {// get and convert the data int id = integer. parseint (formatdata [1]); // process data int result = myappnotebookserver. deletenotebookbyid (ID); // convert data string senddata = string. valueof (result); // send data senddata (senddata);} break;} // send information function public void senddata (string data) {try {// send message output. writeutf (data); output. flush ();} catch (exception e) {e. printstacktrace () ;}/// receiving command function Public String getcommonddata () {string data = NULL; try {// receiving message data = input. readutf ();} catch (exception e) {// todo: handle exception} return data;} // main function mainpublic static void main (string ARGs []) throws exception {myappnotebookinfor; while (true) {myappnotebookinfor = new myappnotebookinfor (); myappnotebookinfor. start ();}}}

Individual client socket implementation

Package thread; import Java. io. datainputstream; import Java. io. dataoutputstream; import java.net. socket; import COM. ICSS. myappnotebook. myapplication; public class socektthread extends thread {// set the connection server private string IP = "120.6.72.65"; private int Port = 8899; // link the client program variable private socket clientsocket; // set the data stream public datainputstream input; Public dataoutputstream output; // main thread function public void run () {try {// link server clientsocket = new socket (IP, Port ); // obtain the server input stream input = new datainputstream (clientsocket. getinputstream (); // get the server input stream output = new dataoutputstream (clientsocket. getoutputstream (); Switch (myapplication. commond) {// check-check the user name and password case myapplication. commond_check_user: // send data 2 point output. writeutf (myapplication. senddata); output. flush (); // system successfully sent. out. println ("Send OK"); // receives the data myapplication. getdata = input. readutf (); // wake up the login process myapplication. loginthreadstate = "on"; myapplication. loginthreadstate. Y (); break; // obtain all notescase myapplication. commond_get_my_all_note: // send data at 2 points output. writeutf (myapplication. senddata); output. flush (); // receives data from myapplication. getdata = input. readutf (); // wake up the menu thread myapplication. menuthreadstate = "on"; myapplication. menuthreadstate. running y (); break; Case myapplication. commond_insert_note: // send data output. writeutf (myapplication. senddata); output. flush (); // receives data from myapplication. getdata = input. readutf (); // wake up new thread myapplication. newnotebookthreadstate = "on"; myapplication. newnotebookthreadstate. running y (); break; Case myapplication. commond_insert_user: // send data output. writeutf (myapplication. senddata); output. flush (); // receives data from myapplication. getdata = input. readutf (); // wake up the register thread myapplication. registeruserthreadstate = "on"; myapplication. registeruserthreadstate. running y (); break; Case myapplication. commond_search_note_by_key: // send data output. writeutf (myapplication. senddata); output. flush (); // receives data from myapplication. getdata = input. readutf (); // wake up the search thread myapplication. searchnotebookthreadstate = "on"; myapplication. searchnotebookthreadstate. running y (); break; Case myapplication. commond_alter_note: // send data output. writeutf (myapplication. senddata); output. flush (); // receives data from myapplication. getdata = input. readutf (); // wake up the show one notebook thread myapplication. showonenotethreadstate = "on"; myapplication. showonenotethreadstate. running y (); break; Case myapplication. commond_delete_note: // send data output. writeutf (myapplication. senddata); output. flush (); // receives data from myapplication. getdata = input. readutf (); // wake up the show one notebook thread myapplication. openallthreadstate = "on"; myapplication. openallthreadstate. Y (); break; default: break;} clientsocket. close ();} catch (exception e) {e. printstacktrace ();}}}

I hope it will be helpful for friends who want to implement communication. Thank you!



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.