In network transmission, there are three Data Organization Methods: xml, json, and http. Here we will talk about the most frequently used json organization method for data parsing in Android.
Json format, which saves more space than xml format.
First, we use a virtual server to simulate a mobile phone to download resources from the network.
Again, we set the data hypothesis to four attributes: App Image, app name, description, and download volume. Place the users in a file named json.txt, which is stored as a json-specific key-value pair)
First, create a class JsonTask extends AsyncTask <String, Void, String> to asynchronously load json data.
Then JsonTask jsonTask = new JsonTask ();
JsonTask.exe cute ("http: // 10.0.2.2: 8080/json.txt ");
Parse the json file to get the four required attributes. In this case, the app Image is obtained, instead of the real image. To get the real image, you need to enable asynchronous loading, to obtain the image of the app. For details, see Appendix 1.
After the image and text are downloaded from the virtual server, you will find a problem: when you drag several lines or dozens of lines down quickly, for example: your ListView displays 6 rows on each page. When you drag down to the eighth row, the layout is reused. When you drag down to the second row, you will find that, when you set the row to 29th, the image of the row's app will change constantly. If you change it to 4 times, it will actually display the image of 29th rows.) Why?
Here we will optimize the code in Appendix 1 and remove the disadvantages of improper layout reuse. Appendix 2 provides detailed instructions.
Here, the example of downloading data from the virtual server and parsing the data is coming to an end, but there is still a problem: when the server cannot be connected or the current network is not powerful, you cannot keep the app in the read or connection status. At this time, you should set a read data response time and a connection response time. When the program goes through a set time, if the data is still not read, an exception is thrown and a prompt is displayed, allowing you to clearly know whether the data is read or the connection is successful. Add a reconnect button to allow the user to connect to the data again and read the data. This is also an optimization for the code in Appendix 2. For the optimization process and method, Appendix 3 provides detailed instructions.
Here, an application simulating downloading app images and descriptions from the server is complete. Thank you.
Learn from cainiao.
This article from the "black humor" blog, please be sure to keep this source http://7813345.blog.51cto.com/7803345/1297947