Android Network Programming tutorial, based on socket and HTTP protocol instance

Source: Internet
Author: User

Based on the use of sockets

Server side:

Start a server-side socket first

ServerSocket svr = new ServerSocket (8989);

Start listening for requests

Socket s = svr.accept ();

Get input and output

DataInputStream dis = new DataInputStream (S.getinputstream ());

DataOutputStream dos = new DataOutputStream (S.getoutputstream ());

The interaction of the socket is done through the stream, which means that the byte stream is transmitted, so any file can be transmitted above. Whoever opens it, remember to close it.

The Datainputstream/dataoutputstream are packaged because we want them to read and write the basic data type Readint (), Writeint (), readUTF (), writeUTF (), and so on.

Client:

Initiate a socket connection

Socket s = new socket ("192.168.1.200", 8989);

Get input and output

DataInputStream dis = new DataInputStream (S.getinputstream ());

DataOutputStream dos = new DataOutputStream (S.getoutputstream ());

Then we can communicate with each other. Whoever opens it, remember to close it.

based on HTTP protocol

Typically, you send a request to an application server. The httpurlconnection is needed at this time.

Get HttpURLConnection First

Urlconn = new URL ("http://www.google.com"). OpenConnection ();

Set flag

Urlconn.setdooutput (TRUE); Urlconn.setdoinput (TRUE);

You need to set Dooutput to true in case of post

Urlconn.setrequestmethod ("POST");

Urlconn.setusecache (false);//setting whether to use caching

Set Content-type

Urlconn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");

Get the output stream to make it easier to send information to the server.

DataOutputStream dos = new DataOutputStream (Urlconn.getoutputstream ());

Look in the stream inside write request parameters

Dos.writebytes ("Name=" +urlencoder.encode ("Chenmouren", "gb2312");

Dos.flush ();d os.close ()//close immediately after sending.

Get input stream, fetch data

Bufferreader reader = new BufferedReader (New InputStreamReader (Urlconn.getinputstream ()));

Reader.readline ()//use!=null to determine whether the end

Reader.close ();

Finish reading remember to close connection urlconn.disconnect ();

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.