To request data from the server:
Two request modes: GET and POST
Get is generally used to obtain data from the server, post is generally used to submit data to the server;
How to get data by get:
Note: After the android4.0 version, the main thread (UI thread) does not support the network request, the reason is probably the main thread, the speed is too slow, easy card machine, so need to open a new thread request data;
//HttpURLConnection /*** 1. Instantiate a URL Object * 2. Get HttpURLConnection Object * 3. Set request Connection Properties * 4. Get the response code to determine if the connection was successful * 5. Read input stream and parse*/ //parameters: The address of the interface you want to access NewThread () {@Override Public voidrun () {Try{URL URL=NewURL ("http://www.imooc.com/api/teacher?type=2"); HttpURLConnection Coon=(HttpURLConnection) url.openconnection (); Coon.setrequestmethod ("GET"); Coon.setreadtimeout (6000); //Get the response code if(Coon.getresponsecode () = = 200){ //Get input streamInputStream in =Coon.getinputstream (); byte[] B =New byte[1024*512]; intLen = 0; //Create a cache stream to hold the byte array readBytearrayoutputstream BAOs =NewBytearrayoutputstream (); while(len = In.read (b)) >-1) {Baos.write (b,0, Len); } String msg=baos.tostring ();}. Start
Parsing JSON common data
Parsed data
{ "status": 1, "data":{ "title": "Tony Teacher chats shell--environment variable profile", " Author ":" Tony ", " content ":" This is the third of the class Tony teacher talk Shell series that brings you the use of commonly used environment variable profiles. " }, " MSG ":" Success "}
Using Jsonobject parsing:
1 //parsing of JSON data:2Jsonobject obj =Newjsonobject (msg);3 intStatus = Obj.getint ("status");4String MSG2 = obj.getstring ("MSG");5LOG.E ("TAG", Status + "" +msg2);6 //Parse data array first get data object7Jsonobject jsonobject = obj.getjsonobject ("Data"));8String title = jsonobject.getstring ("title");9String author = jsonobject.getstring ("Author");TenString content = jsonobject.getstring ("Content"); OneLOG.E ("TAG", "title:" + title + "," + author + ", Contents:" + content ");
Using Gson to quickly parse array data
Data:
1 {2 "status": 1,3 "Data": [{4 "id": 1,5 " name": "Tony teacher talk shell--environment variable profile",6 " Picsmall": "Http://img.mukewang.com/55237dcc0001128c06000338-300-170.jpg",7 " picbig": "Http://img.mukewang.com/55237dcc0001128c06000338.jpg",8 " description": "Bring you the environment variable configuration file in the shell",9 "learner": 12312Ten }, { One "id": 2, A " name": "The application of Mathematical knowledge in CSS animation", - " Picsmall": "Http://img.mukewang.com/55249cf30001ae8a06000338-300-170.jpg", - " picbig": "Http://img.mukewang.com/55249cf30001ae8a06000338.jpg", the " description": "Mathematical knowledge and CSS to achieve cool effect", - "learner": 45625 - }, { - "id": 3, + " name": "PL/SQL foundation of Oracle Database Development Essentials", - " Picsmall": "Http://img.mukewang.com/5523711700016d1606000338-300-170.jpg", + " picbig": "Http://img.mukewang.com/5523711700016d1606000338.jpg", A " description": "An essential foundation for advanced Oracle database development." ", at "learner": 41236 - }, { - "id": 4, - "name": "Android witness message push time Advanced article", - " Picsmall": "Http://img.mukewang.com/551e470500018dd806000338-300-170.jpg", - " picbig": "Http://img.mukewang.com/551e470500018dd806000338.jpg", in " description": "Android message push is in front, Come on", - "learner": 45456 to }, { + "id": 5, - " name": "Avalon Exploration Tour Basic tutorial-complex binding", the " Picsmall": "Http://img.mukewang.com/551de0570001134f06000338-300-170.jpg", * " picbig": "Http://img.mukewang.com/551de0570001134f06000338.jpg", $ " description": "Front-end fan your MVVM framework, Avalon Complex Binding attribute chapter." ",Panax Notoginseng "learner": 56556 - }, { the "id": 6, + " name": "Android-service series of breakpoints continue to download", A " Picsmall": "Http://img.mukewang.com/552640c300018a9606000338-300-170.jpg", the " picbig": "Http://img.mukewang.com/552640c300018a9606000338.jpg", + " description": "Want to raise a salary?" This chapter course you deserve to have ", - "learner": 48996 $ }, { $ "id": 7, - "name": "Junit-java Unit Test Prerequisite Tool", - " Picsmall": "Http://img.mukewang.com/551b92340001c9f206000338-300-170.jpg", the " picbig": "Http://img.mukewang.com/551b92340001c9f206000338.jpg", - " description": "Java Unit Test Weapon!",Wuyi "learner": 13210 the }, { - "id": 8, Wu " name": "Elaborate on the memory visibility of Java multi-thread", - " Picsmall": "Http://img.mukewang.com/5518c3d7000175af06000338-300-170.jpg", About " picbig": "Http://img.mukewang.com/5518c3d7000175af06000338.jpg", $ " description": "Achieve memory visibility in two ways", - "learner": 15051 - }, { - "id": 9, A " name": "Practical tips for CSS animations", + " Picsmall": "Http://img.mukewang.com/551b98ae0001e57906000338-300-170.jpg", the " picbig": "Http://img.mukewang.com/551b98ae0001e57906000338.jpg", - " description": "Teach you to use CSS to achieve stunning animations!" ", $ "learner": 15210 the }, { the "id": Ten, the " name": "The launch of the C + + expedition", the " Picsmall": "Http://img.mukewang.com/550b86560001009406000338-300-170.jpg", - " picbig": "Http://img.mukewang.com/550b86560001009406000338.jpg", in " description": "C + + Highlights in it", the "learner": 84545 the }], About " msg": "Success" the}
Using Gson to quickly get data arrays
//1. Create a Gson objectGson Gson =NewGson (); //parameter 1: String that satisfies the format of the JSON objectString data = obj.getstring ("Data"); //the Data entity class is used to store thearraylist<data> list = Gson.fromjson (Data,NewTypetoken<arraylist<data>>() {}.gettype ()); for(inti = 0; I < list.size (); i++) {Data a=List.get (i); LOG.E (TAG,"id =" + a.getid () + "name=" +a.getname ()); }
Using Gson, you can quickly get the data from the array in the JSON data without needing one to get the Jsonobject object and then one for the data.
ANDROID_ Network operation