This article address:http://www.cnblogs.com/wuyudong/p/5900384.html, reprint please indicate source address.
Transfer of data
Client: Send HTTP request http://www.oxx.com/index.jsp?key=value
Server: After receiving the request, send data to the client, (json,xml), JSON data read from the database, read data stitching JSON, grammar rules, structure
Get Server version number (client sends request, service side to response, (Json,xml))
Http://www.oxxx.com/update.json?key=value return 200 request succeeded, stream the way to read the data down
The contents of JSON include:
/* Version name of the updated version
* New version of the description information
* Server version number
* New version apk*/
Server-side JSON data Update.json, the file contents are as follows:
{ "DownloadURL": "http://www.wuyudong.com/XXX.apk", "Versioncode": "2"; " Versiondes ":" 2.0 version released, hurry to download Ah " " versionname ":" 2.0 "}
Note: Save text as "UTF-8 no BOM" format
Turn on Tomcat and put the above Update.json file in the root folder
New Tool class Streamtostring.java, the main role: the conversion of the string. The code is as follows:
Packagecom.wuyudong.mobilesafe.Utils;ImportJava.io.ByteArrayOutputStream;Importjava.io.IOException;ImportJava.io.InputStream; Public classStreamutil {/*** Transfer to a String *@paramis Stream Object *@returnthe converted string returns Null to represent the exception*/ Public StaticString streamtostring (InputStream is) {//1, during the read process, the read content is stored in the value cache and then converted into a string once to returnBytearrayoutputstream BOS =NewBytearrayoutputstream (); //2, read stream operation, read to No so far (loop) byte[] buffer =New byte[1024]; //3. Record temporary variables for read content inttemp =-1; Try { while(temp = is.read (buffer))! =-1) {bos.write (buffer,0, temp); } //return read Data returnbos.tostring (); } Catch(IOException e) {e.printstacktrace (); } finally { Try{is.close (); Bos.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } return NULL; }}
Then add the request network data code in (because the request Network service is a time-consuming operation, all new threads are completed):
/*** Check Version number*/ Private voidcheckversion () {NewThread () { Public voidrun () {//send request fetch data, parameter is the link address of request JSON// http://169.254.13.99: 8080/update.json test phase is not optimal//Simulator access to computer tomcat only Try { //1, Package URL addressURL url =NewURL ("Http://10.0.2.2:8080/update.json"); //2, open a linkHttpURLConnection connection =(HttpURLConnection) url.openconnection (); //3, set common request parameters (request header)//Request timed outConnection.setconnecttimeout (2000); //Read TimeoutConnection.setreadtimeout (2000); //The default is GET request mode//Connection.setrequestmethod ("POST"); //4, get the request success Response code if(Connection.getresponsecode () = = 200) { //5, in the form of streams, to get down the dataInputStream is =Connection.getinputstream (); //6, change the flow to a string (tool class package)String JSON =streamutil.streamtostring (IS); //Remember to add permissions-->android.permission.internetLOG.I (tag, JSON); } } Catch(malformedurlexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }; }.start (); }
After running, you can print the following information in Logcat:
09-23 03:36:36.935:i/splashactivity (14884): {
09-23 03:36:36.935:i/splashactivity (14884): "DownloadURL": "http://www.wuyudong.com/XXX.apk",
09-23 03:36:36.935:i/splashactivity (14884): "Versioncode": "2",
09-23 03:36:36.935:i/splashactivity (14884): "Versiondes": "2.0 release, hurry to download Ah",
09-23 03:36:36.935:i/splashactivity (14884): "Versionname": "2.0"
09-23 03:36:36.935:i/splashactivity (14884):}
Description of the request server JSON data success
Android Phone Defender--build service-side JSON, request network data