Using Web Services in Your Applications
Because we want to access the web service through an Android application, the first step is to create an Android application. The user interface of the Android application will comprise two text boxes and a button control. To learn the weather information of any city, the user will enter the city and country names in the two text boxes and click the button. The country name isn't mandatory; the user can leave its text box blank. After the user clicks the button control, current weather information for the specified city will be displayed. The weather information will include latitude, longitude, cloud description, humidity, atmospheric pressure, temperature, and wind speed.
Follow these steps to create an Android project:
- Launch Android Studio and click the New Project icon to create a new Android project.
- In the New Project dialog, enter information for the new project, as shown in Figure 1. In the Application Name box, enter the name of the Android project, AccessWebServiceApp. The Module Name entry is assigned automatically; by default, it's the same as the application name.
- To identify this project uniquely, assign the package name com.pearsonarticles.accesswebserviceapp.
- In the Project Location box, specify the location where you want to store the Android project.
- From the Minimum Required SDK drop-down list, select API 8: Android 2.2 (Froyo) to indicate that the application requires at least API level 8 to run.
- From the Target SDK drop-down list, select API 17: Android 4.2 (Jelly Bean), as we expect this to be the version commonly used by the target audience. From the Compile With drop-down list, select API 17: Android 4.2 (Jelly Bean) as the platform to compile the project.
- The default theme assigned to the project is Holo Light with Dark Action Bar. Leave the Theme setting unchanged.
- The Create Custom Launcher Icon checkbox is checked by default; leave it checked. This feature can be used to configure the launcher icon for the application.
- The Create Activity checkbox is also checked by default because usually it's the activity file where the Java code for the project is written. Leave this option checked.
- Click the Next button.
- The next dialog is Configure Launcher Icon, which is used for configuring the icon for the application. Because we want to use the default icon for our application, keep the default options selected in the dialog box, and click Next.
- The next dialog prompts you to create an activity. Because we want to create a blank activity, select the BlankActivity option from the list and click Next.
- The next dialog asks for information about the newly created activity. Name the activity AccessWebServiceActivity. The layout file is assigned the default name activity_main.xml. Keeping that default name, click Finish to create the Android project.
- Android Studio will automatically create several files and folders for your new Android project. For this web service project, we just need to work with two files:
Figure 1 The New Project dialog box requests information for the new Android project.
- activity_main.xml (also known as the activity layout file) is located in the following node:
- AccessWebServiceApp > src > main > res > layout
- In this XML file, you'll define the controls (TextView, EditText, Button, and so on) for your Android app's graphical user interface. Through this file, the user interacts with the Android project.
- AccessWebServiceActivity.java (the Java activity file) is located in the following node:
- AccessWebServiceApp > src > main > java > com.pearsonarticles.accesswebserviceapp
- This Java file loads the controls defined in the layout file activity_main.xml, listens for various events, and runs the required code if any of those events occur.