In Android, WebView and WebSocket are used to implement group chat and push functions.

Source: Internet
Author: User

In Android, WebView and WebSocket are used to implement group chat and push functions.

WebSocket is a new product of the web era. It is used to make up for some shortcomings of the HTTP protocol. However, the real relationship between them is the sibling relationship, which further encapsulates the socket, currently, server push and chat are the most intuitive features. For more information, see: how to understand the relationship among TCP/IP, SPDY, and WebSocket?

Today's focus is on how to use WebSocket in Android without WebView, rather than in Web browsers. If it is used in Web browsers, there are already too many tutorials, frameworks, and demos on the Internet, there is no need to talk about it.

So far, I personally think that the WebSocketClient for Android is autobahn, AndroidAsync, and Java-WebSocket. It depends on actual needs. Here I select Java-WebSocket.

1. Create an Android client (using the Java-WebSocket Library ):

1. In fact, you only need to master one class, WebSocketClient.

    

2. Specify the IP/domain name and port to connect to the server. The onMessage method will be called back when the server receives a notification.

    

3. Call the connect Method for connection.

    

4. After the connection, you can send the message. sending the message is also very simple. In addition to sending String messages, you can also send byte messages, the client is finished happily (for detailed code, see the demo package later ).

    

 

Ii. Server creation:

1-1,Create a Java Application server (using the Java-WebSocket Library)In fact, it is also very simple to inherit a class WebSocketServer:

      

 

 

1-2. Enable the server in the main method. Now, you can use the Android client to connect to chat and receive and push messages. This is really easy.
    

 

2-1,Create a Java Web (tomcat) server. The Java-WebSocket library is not used here.Java API javax. websocket-related classes in the WebSocket package (note that Java APIs only implement the standard RFC 6455 (JSR256). If you have to select other early drafts, you must use Java-WebSocket, in Java-WebSocket, the connection protocol "Draft_17" is the standard RFC 6455 (JSR256). In addition, you must use Java API javax. the websocket-related classes in the WebSocket package require JDK 7 and later, Tomcat 7.0.49 and later ):

    

2-2. start tomcat and use the Android client to connect to chat and receive and push messages.

 

Iii. Related:

1-1. Java Background:

  

1-2. Android client corresponding to the Java background

  

2-1. Java Web Background:

  

2-2. Android client corresponding to the Java Web Background

   

2-3. html front-end (browser ):

  

  

  Download demo code: websockettings test demo.zip

 

Original essay, reprinted to indicate the source.

 

Reprinted with address: http://www.chengxuyuans.com/Android/97798.html

Q: one-click call to programmer Q & A artifacts, one-to-one service, developer programming required official website: www.wenaaa.com

QQ Group 290551701 has gathered many Internet elites, Technical Directors, architects, and project managers! Open-source technology research, welcome to the industry, Daniel and beginners interested in IT industry personnel!

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.