Android Network and Communication (i)

Source: Internet
Author: User

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)

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.