Android weather forecast production - quotation display

The statement display function is similar to the weather information display function. When there is a network, the interface is called and the obtained information is passed back to the designated area for display. This function is located on the initial interface. When the interface is switched or the corresponding icon is clicked, the statement will change. The specific operation process is shown in the figure.

Quotations are displayed in the same interface as weather information. Its implementation is similar to obtaining weather information and city information. After defining variables and controls in MainActivity (Figure 3-37), define methods to access the network to obtain statements and transfer statements. Display to the interface.

Figure 3-37 Defining variables and controls

(1)Access to the Internet

First implement the getDataFromServer() method responsible for accessing the network to obtain statements, as shown in Figure 3-37.

Figure 3- 38 getDataFromServer()

The way getDataFromServer() accesses the URL is the same as the way openConnection() accesses the weather information (see Figure 3-2 for details). The design differences exist in the following two points:

1. Different types of objects that store access information

String(result) is used in openConnection(), and StringBuilder(stringBuilder) is used in getDataFromServer(). Although both are used to receive and process string type data, when String splices strings, it first creates each string in memory, and then splices the strings to get a new string and returns it as the result. StringBuilder is a dynamic object, and the string it represents is variable. Therefore, when splicing strings, you can directly add the string to be spliced ​​after the StringBuilder object. There is no need to save the string in another place. Therefore, StringBuilder saves memory resources compared to String, and the splicing efficiency will be higher. The value returned after splicing using StringBuilder objects is also of StringBuilder type. In this case, toString is needed to convert it into String().

2. How to handle after the visit

OpenConnection() in accessing weather information will end the function after returning the result after the access is completed, while getDataFromServer() in accessing quotations will use close() to close the input stream (inputStream) and character stream (bufferedReader) after getting the access result and returning it to release the related ones. system resources, and also use disconnect() to disconnect from the URL. In order to ensure that close() and disconnect() can be called, the try catch finally statement is used in the design. No matter whether an exception occurs in the try or catch statement, the statement in finally will be executed.

(2) The statement is transmitted to the interface

The transfer of statement information occurs in the function initData() that initializes data, as shown in Figure 3-40. The anonymous inner class new Thread (new Runnable() {}) method is used. At this time, the value passed to the Thread constructor is It is an object reference of a class that implements Runnable. New Runnable() {public void run() {...}} includes objects that define this class and instantiate this class. The information accessed in the class using key-value pairs is stored in the bundle and then encapsulated into a message and sent to the main thread. The specific code is shown in Figure 3-39.

Figure 3- 39 initData()

After the data is transferred to the main thread, handleMessage() is called back in the main thread to transfer the acquired data to the interface. First, it is judged whether the transmitted information code is the same as the pre-set one. The same indicates that the obtained data is needed by the system. Next, the data is received from the Message object and stored in data, and the data is transferred to the specified location on the interface. As shown in Figure 3-40.

Figure 3- 40 handleMessage()

If the user wants to change the displayed data, he can click the icon in the lower right corner. The system has set a monitoring button for the icon. Clicking the icon will call the initDate() function that initializes the data to replace the statement.

 

Guess you like

Origin blog.csdn.net/weixin_58963766/article/details/130567891