Android Apps Analysis 02-android im: an open-source implementation of instant messaging similar to mobile QQ
This is a simple IM application running on Android when the application issues HTTP requests to the server, in PHP and MySQL, to verify, register and get other friends ' status and data to implement, then it communicates with other devices through the socket interface.
1. Database as long as 2 tables: Friends table and User table:
CREATE TABLE ' friends ' (' Id ' int (ten) unsigned NOT null auto_increment, ' providerid ' int (ten) unsigned NOT NULL default ' 0 ', ' requestid ' int (ten) unsigned NOT null default ' 0 ', ' status ' binary (1) is not null default ' 0 ', PRIMARY KEY (' Id '), UNIQUE key ' Index_3 ' (' ProviderID ', ' RequestID '), key ' Index_2 ' (' ProviderID ', ' RequestID ', ' status ')) Engine=innodb DEFAULT charset=latin1 comment= ' ProviderID is the Id of the users who wish to be friend with '; CREATE TABLE ' users ' (' Id ' int () unsigned not NULL auto_increment, ' username ' varchar "NOT null default ' ', ' pas Sword ' varchar (+) NOT null default ', ' email ' varchar (") NOT null default ' ', ' date ' datetime NOT NULL default ' 0000- 00-00 00:00:00 ', ' Status ' tinyint (3) unsigned NOT NULL default ' 0 ', ' authenticationtime ' datetime NOT NULL default ' 0000 -00-00 00:00:00 ', ' userkey ' varchar (+) NOT null default ' ', ' IP ' varchar ($) NOT null default ' ', ' Port ' int (Ten) Unsig Ned not NULL default ' 0 ', PRIMARY key (' Id '), UNIQUE key ' Index_2 ' (' username '), KEY ' index_3 ' (' Authenticationtime ')) Engine=innodb DEFAULT Charset=utf8;
2. The core code is to send HTTP request and socket:
public string Sendhttprequest (string params) {URL url; string result = new String (); try {url = new URL (authentication_server_address); HttpURLConnection connection; Connection = (httpurlconnection) url.openconnection (); Connection.setdooutput (TRUE); PrintWriter out = new PrintWriter (Connection.getoutputstream ()); OUT.PRINTLN (params); Out.close (); BufferedReader in = new BufferedReader (New InputStreamReader ( Connection.getinputstream ())); String Inputline; while ((Inputline = In.readline ()) = null) { result = Result.concat (inputline); } in.close (); } catch (Malformedurlexception e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); } if (result.length () = = 0) {result = HTT p_request_failed; } return result; } public boolean SendMessage (String message, string IP, int port) {try { string[] str = ip.split ("\ \"); byte[] IP = new Byte[str.length]; for (int i= 0; i < str.length; i++) {Ip[i] = (byte) integer.parseint (Str[i]); } Socket socket = Getsocket (inetaddress.getbyaddre SS (IP), port); if (socket = = NULL) {return false; } printwriter out = null; out = new PrintWriter (Socket.getoutputstream (), true); OUT.PRINTLN (message); } catch (Unknownhostexception e) {return false; E.printstacktrace (); } catch (IOException e) {return false; E.printstacktrace (); } return true; }
3. Other information:
Android Instant messaging app using HTTP request and socket
Original source:https://code.google.com/p/simple-android-instant-messaging-application/
Latest Source:Https://github.com/Pirngruber/AndroidIM
Source Download: http://download.csdn.net/user/yangzhenping
source download provided by the initial author: http://download.csdn.net/detail/yangzhenping/8397989
Android Apps Analysis 02-android im: an open-source implementation of instant messaging similar to mobile QQ