Android apps 02-Android IM: a similar mobile QQ Instant Messaging Open Source implementation, android02-android

Source: Internet
Author: User

Android apps 02-Android IM: a similar mobile QQ Instant Messaging Open Source implementation, android02-android
Android apps 02-Android IM: an open-source instant messaging implementation similar to mobile QQ


This is a simple IM application running on Android. When the application sends an HTTP request to the server, it verifies, registers, and obtains the status and data of other friends in PHP and MySQL, then it communicates with other applications of other devices through the socket interface.

 

1. There are only two tables in the database: Friend table and user table:
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. 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 = 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. Other information:

Android instant messaging applications implemented using http request and socket
Source: https://code.google.com/p/simple-android-instant-messaging-application/
Latest source code: https://github.com/Pirngruber/AndroidIM
Source code download: http://download.csdn.net/user/yangzhenping
Download source code from the author: http://download.csdn.net/detail/yangzhenping/8397989

 


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.