Use Netty for communication between Android and Server to send and receive texts and upload images,
Another title:
Let's use netty to achieve text send and receive and image transfer to server based on android and jdk1.7
Let me Rap <MC Hotdog> and use netty to send and receive text on Android, along with the image sending function. After a few days, netty was a little eye-catching and recorded.
The requirement is that the android client uploads a photo to the server, and the server returns the bla after the upload is successful or fails. The requirement is relatively simple. The original use of the traditional post plus multipart/form-data, the average mobile phone photo size 1-2 M, the test is OK, but the actual situation is that some users use 3G network, some 4G, some WIFI also use 2G, the network environment is a little poor upload image for more than 10 minutes (I am sorry to say), and then thought of using socket, after that, I googled netty. Some great gods said that there is no need to access netty. In fact, I just want to learn about socket, netty, channel, and byte [] through this requirement. with regard to NIO, there is always no harm in learning more. Later I can say that I have played a high-performance event-based Asynchronous Network Framework.
Netty. i/O document. After reading TimeServer, I learned about google's other netty knowledge in the world. I started with Netty's authoritative guide version 2. after several chapters, I learned how to use TimeServer, but I have a preliminary understanding of some basic NIO components.
OK. Let's get started. If I want to implement a Demo, I will say it's a Netty chat room (group chat, no single chat function, and the demo is full of flavor), but the client is replaced with Android, if a server is connected to multiple clients and all data passes through the server, an intermediate information exchange component must be defined. Google's Protobuf, MessagePack, and JBoss javasalling middleware have never been played, and it seems a little incomprehensible. My requirements are very simple, for example (Sorry, the keyboard party has been suspended for many years, I can only understand it myself ):
There are three objects in total:
Echo-Pojo, an intermediate object for information exchange. It is used for serialized objects after byte [] is encapsulated on Android and Server.
Let's Chat, Android client, build an EchoMessage or EchoFile object, and then send it to the channel. EchoMessage and EchoFile are used by the server to determine the category of the information object.
Echo-Server, Server. When ChannelRead is used, it determines whether to broadcast to all users or save files.
The ByteBuf object has not been clearly understood, so the above is not used.
Run the android terminal (Xiaomi PAD) Let's Chat:
Let's Chat Xiaomi Pad running
Samsung mobile Let's Chat run:
Let's Chat SamSung mobile phone run
Server (Echo-Server) running:
Echo-Server output logs and descriptions
Uploaded files:
The file name is in the format of build.manufacturew.uid.randomuuid({%}.jpg ". The second image is 12.9 MB, and the upload time is six or seven seconds. There should be room for optimization.
The Echo-Pojo code is not available. You can download and view the Code directly, or you can continue to expand the code. Currently, only images and text are supported.
The above functions are divided into three programs, Echo-Pojo Information Exchange object using eclipse-Mars2 development, Echo-Server using Intellij idea development, Let's Chat using Android Studio2, a little egg pain, but fortunately, we have implemented a lot of functions, especially when the company's computer is different from the jdk of the home computer, one 1.8, one 1.7, because of this many inexplicable errors, it also wastes a lot of time, and it will not be recorded for all kinds of troubles in the middle.
Code comments are relatively small, and many discarded items are not deleted, but functions can be run and not fully organized. If you have any questions, you can reply directly or @ email me.
I used Liu Yuan's NettyFileTransfer for reference, but I changed the client to Android. Then I used the Netty version 4.0.36Final. Thank you.
The rest is to drag the function into the project, Keep Fighting!
Open Source Address: https://github.com/LisonLiou/netty-learning.git
Original address: http://lison.cc/508.html