The Android platform is available in three network interfaces: java.net.* (Standard Java interface), Org.apache interface, and android.net.* (Android network interface). The functions and roles of these interfaces are described below.
1. Standard Java interface
Java.net.* provides networking-related classes, including streams, packet sockets, Internet protocols, common HTTP processing, and more. For example: Create URLs, and Urlconnection/httpurlconnection objects, set link parameters, link to the server, write data to the server, read data from the server, and other communications. These are involved in Java network programming, we look at a simple socket programming, to implement server postback client information.
Service side:
Public class Server implements runnable{
@Override
Public void Run () {
Socket socket = null;
try {
ServerSocket Server = new ServerSocket (18888);
Loop Listener Client Link request
while (true) {
System.out.println ("Start ...");
Receiving requests
Socket = Server.accept ();
System.out.println ("Accept ...");
Receiving client messages
BufferedReader in = new BufferedReader (new InputStreamReader (Socket.getinputstream ()));
String message = In.readline ();
Send a message to the client
PrintWriter out = new PrintWriter (NewBufferedWriter (socket.getoutputstream ())), true);
Out.println ("Server:" + message);
Close the stream
In.close ();
Out.close ();
}
} catch (IOException e) {
E.printstacktrace ();
}finally{
if (null! = socket) {
try {
Socket.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}
Start the server
Public static void Main (string[] args) {
Thread server = new Thread (new Server ());
Server.start ();
}
}
Client, Mainactivity
Public class Mainactivity extends Activity {
Private EditText EditText;
Private button button;
/** called when the activity is first created. */
@Override
Public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
EditText = (editText) Findviewbyid (R.ID.EDITTEXT1);
Button = (button) Findviewbyid (R.id.button1);
Button.setonclicklistener (new Onclicklistener () {
@Override
Public void OnClick (View v) {
Socket socket = null;
String message = Edittext.gettext (). toString () + "\ r \ n";
try {
Create a client socket, note: You cannot use localhost or the 127.0.0.1,android emulator as a localhost
Socket = New socket ("<span style=" font-weight:bold;" >10.0.2.2</span> ",18888);
PrintWriter out = new PrintWriter (Newbufferedwriter (new OutputStreamWriter
(Socket.getoutputstream ())),true);
Send data
OUT.PRINTLN (message);
Receive data
BufferedReader in = new BufferedReader (new InputStreamReader (Socket.getinputstream ()));
String msg = In.readline ();
if (null! = msg) {
Edittext.settext (msg);
SYSTEM.OUT.PRINTLN (msg);
}
else{
Edittext.settext ("Data Error");
}
Out.close ();
In.close ();
} catch (Unknownhostexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
finally{
try {
if (null! = socket) {
Socket.close ();
}
} catch (IOException e) {
E.printstacktrace ();
}
}
}
});
}
}
Android Network and Communication (i)