1. Code for checking the network status
ConnectivityManager cm = (ConnectivityManager) Context. getSystemService (Context. CONNECTIVITY_SERVICE );
NetworkInfo netInfo = cm. getActivityNetworkInfo ();
NetInfo. toString ();
2. Use Network Permissions
<Uses-permission android: name = "android. permission. INTERNET"/>
3. Get data (text and images) through URL + HttpURLConnection)
URL url = new URL ("http://www.sohu.com ");
HttpURLConnection conn = (HttpURLConnectioni) url. openConnection ();
Conn. setConnectionTimeout (5*1000 );
Conn. setRequestMethod ("GET ");
Conn. getResponseMethod ("GET ");
(Conn. getResponseCode ()! = 200) throw ...;
InputStream is = conn. getInputStream ();
String result = readData (is, "GEK ");
Conn. disconnect ();
// Obtain the image.
1. Send name-value pairs in GET Mode
// Http: // xxxx/xxx. action? Name = tom & age = 12
URL realUrl = new URL (requestUrl );
HttpURLConnection conn = (HttpURLConnection) realUrl. openConnection ();
Conn. setRequestMethod ("GET ");
Conn. setConnectionTimeout (5000 );
// Data is sent to the server until this time
If (conn. getResponseCode () = 200 ){
String result = readAsString (conn. getInputStream (), "UTF-8 ");
OutStream. close ();
System. out. println (result );
}
Note: Chinese garbled characters.
// UTF-8 indicates the server-side Encoding
1. If the get method is used to send Chinese characters, transcoding is required. UrlEncode ("Chinese", "UTF-8 ");
2. transcode the tomcat client (get mode ).
If ("GET". equals (request. getMethod ())){
String v = request. getParameter ("name ");
New String (v. getBytes ("ISO8859-1"), "UTF-8 ");
}
1. POST sends common text content
URL realUrl = new URL (requestUrl );
HttpURLConncection conn = (HttpURLConnection) realUrl. openConnection ();
Conn. setDoOutput (true );
Conn. setUseCaches (false );
Conn. setUseCaches (false );
Conn. setRequestMethod ();
Conn. setRequestProperty ("Connection", "Keep-Alive"); // maintain a persistent Connection
Conn. setRequestProperty ("Charset", "UTF-8 ");
Con. setRequestProperty ("Content-Length", String. valueOf (data. length ));
Con. setRequestProperty ("Content-Type", "application/x-www-form-urlencoded ");
String str = "name = xxx & age = 12 ";
Con. getOutputStream (). write (str. getBytes );
If (conn. getOutputStream (). write (Str. getBytes )){
If (conn. getResponseCode () = 200 ){
String result = readAsString (conn. getInputStream (), "UTF-8 ");
OutStream. close ();
System. out. println (result ):
}
}
1. POST sends xml
StringBuilder xml = new StringBuilder ();
Xml. append ("<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> ");
Xml. append ("<M1 V = 10000> ");
Xml. append ("<u I = 1 D = \" N73 \ "> China </U> ");
Xml. append ("</M1> ");
Byte [] xmlbyte = xml. toString (). getBytes ("UTF-8 ");
URL url = new URL ("http://xxx.do? Method = readxml ");
HttpURLConnection conn = (HttpURLConnection) url. openConnection ();
Conn. setConnectTimeout (5*1000 );
Conn. setDoOutput (true); // allow output
Conn. setUseCaches (false); // No Cache is used.
Conn. setRequestMethod ("POST ");
Conn. setRequestProperty ("Connection", "Keep-Alive"); // maintain a persistent Connection
Conn. setRequestProperty ("Charset", "UTF-8 ");
Conn. setRequestProperty ("Content-Length", String. valueOf (xmlbyte. length ));
// You must set the content type. Otherwise, the server cannot recognize the Data Type .www.2cto.com.
Conn. setRequestProperty ("Content-Type", "text/xml; charset = UTF-8 ");
DataOutputStream outStream = new DataOutputStream (conn. getOutputStream ());
OutStream. write (xmlbyte); // send xml data
OutStream. flush ();
Author: to1297488504