Android learning diary 14-Network Communication

Source: Internet
Author: User

I. Android Network Communication android network communication generally has three types: java.net. * (standard Java Interface), org. apache interface (based on http Protocol) and android.net. * (Android Network Interface), including stream, socket, Internet protocol, and common Http processing. 1. A Socket is usually called a "Socket". It is used to describe the IP address and port and is a communication chain handle. Android Socket development and JAVA Socket development are similar to creating a Socket server to communicate with the Socket Client. 1 try {2 // create a server Socket 3 ServerSocket ss = new ServerSocket (8888); 4 System. out. println ("Listening... "); 5 while (true) {6 // whether the listener has a Client Connected to 7 Socket = ss. accept (); 8 System. out. println ("Client Connected... "); 9 DataOutputStream dout = new DataOutputStream (socket. getOutputStream (); 10 Date d = new Date (); 11 // demonstrate transferring the current time to the client 12 dout. writeUTF (d. toLocaleString (); 13 dout. close (); 14 socket. close (); 15} 16} 17 catch (Exception e) {18 e. printStackTrace (); 19} 20} 1 public void connectToServer () {// method: connect to the Client 2 try {3 Socket socket = new Socket ("10.0.2.2", 8888 ); // create Socket object 4 DataInputStream din = new DataInputStream (socket. getInputStream (); // obtain the DataInputStream object 5 String msg = din. readUTF (); // read the message sent from the server. 6 EditText et = (EditText) findViewById (R. id. et); // get the EditText object 7 if (null! = Msg) {8 et. setText (msg); // set EditText object 9} else {10 et. setText ("error data"); 11} 12 13 14} 15 catch (Exception e) {// capture and print Exception 16 e. printStackTrace (); 17} 18} the server is a common JAVA project. Start the server first, and then start the Android project of the client. Connect the client to the server and send the current time data from the server to the client. Note: possible causes of the failure to connect the server to the client are as follows: a and AndroidManifest do not have the permission to access the network: <uses-permission android: name = "android. permission. INTERNET "> </uses-permission> B. IP Address: 10.0.2.2. localhostc cannot be used. Proxy cannot be configured on the simulator. 2. Htt is used. P communication uses HttpClient to access the Web on the Android client. html knows that there are two ways to submit a form: get and post. The Android client also accesses the Web. Create a web application on the local machine for demonstration ). Data. jsp code: 1 <% @ page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> 2

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.