首页 > 代码库 > Android apps浅析02-Android IM:一个类似手机QQ的即时通讯开源实现

Android apps浅析02-Android IM:一个类似手机QQ的即时通讯开源实现

Android apps浅析02-Android IM:一个类似手机QQ的即时通讯开源实现


这是Android上的一个简单的IM应用程序运行时,应用程序发出HTTP请求到服务器,在PHP和MySQL,验证,注册和得到其他朋友的状态和数据来实现,那么它与其他设备的其他应用程序通过通信套接字接口。

 

1. 数据库只要2个表:朋友表和用户表:

CREATE TABLE  `friends` (
  `Id` int(10) unsigned NOT NULL auto_increment,
  `providerId` int(10) unsigned NOT NULL default ‘0‘,
  `requestId` int(10) unsigned NOT NULL default ‘0‘,
  `status` binary(1) 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(10) unsigned NOT NULL auto_increment,
  `username` varchar(45) NOT NULL default ‘‘,
  `password` varchar(32) NOT NULL default ‘‘,
  `email` varchar(45) 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(32) NOT NULL default ‘‘,
  `IP` varchar(45) NOT NULL default ‘‘,
  `port` int(10) unsigned NOT NULL default ‘0‘,
  PRIMARY KEY  (`Id`),
  UNIQUE KEY `Index_2` (`username`),
  KEY `Index_3` (`authenticationTime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


 

2. 核心代码是发送Http request和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 = HTTP_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.getByAddress(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.其他信息:

使用http request和socket实现的Android即时通讯应用
原始源码:
https://code.google.com/p/simple-android-instant-messaging-application/
最新源码:
https://github.com/Pirngruber/AndroidIM
源码下载:http://download.csdn.net/user/yangzhenping
初始作者提供的源码下载:http://download.csdn.net/detail/yangzhenping/8397989

 

Android apps浅析02-Android IM:一个类似手机QQ的即时通讯开源实现