Go on with our study.
Believe me, the first day of work is the most important, through these work, we have to develop the environment, the basic conditions of Android must be configured, I believe there are many problems, but, according to my experience, you will quickly solve these problems. At the end of the first day, we finally ran the first app, "Hello, World."
Theoretical knowledge
Below we learn some theoretical knowledge, understand the Android system architecture, components, have some basic knowledge, will not feel confused.
First look at the entire project directory, each directory has a specific role, the following:
- SRC directory, where the source code files are stored.
- Gen directory, the R.java file generated automatically by the ADT plugin.
- Assets directory, storing the resource file directory. Files in this directory do not generate a resource ID in R.java, are not compiled into binary, must be accessed as a file using the relative path started by/assets, and can be accessed using Assetmanager in conjunction with other classes.
- Res directory, storing the resource file directory, each file or value will generate an ID (variable) in the R.java; res/drawable-xxxx is the directory where the picture is stored; res/layout is the directory where the layout file (XML file) is placed. Each activity corresponds to an XML file; Res/values is a directory of files (XML) that store values, and Res/values/strings.xml store key-value pairs, typically used in multi-language versions of programs (multiple files, keys, and values); res/ Values/dimens.xml size; Res/values/styles.xml style
- Androidmanifest.xml is the entire application configuration file that stores some package names, version numbers, program icons, program tags, and so on.
- Project.Properties is automatically generated by the ADT plugin and cannot be modified (the modification will be deleted).
With a preliminary understanding of the project structure, we can begin our work with a purpose. For the next 10 days, we'll step through a complete weather forecast application, please note: It's complete, not just demo.
Our final page effect is as follows:
Task decomposition
Don't be frightened, no matter how complex it may seem now, it will come from our hands. In order to achieve such an application, we need to be divided into a small project, so that it does not look like a monster, we are in the heart of the full.
If you have done project management related work, then for WBS (work breakdown structure) must be very familiar with, our application scale is not large, can be decomposed into the following small items:
1. Learn to invoke the HTTP interface, encapsulate the returned JSON data as Java classes, and eventually display the weather data on the page
2. Learn to use the Baidu Map SDK to locate the current location and save the current location information as local data
3. If there is no network, can we not see the weather? This is of course problematic, we want to save the weather data to the local database, and display the local data by default
4. Beautify the UI
In this short 10 days, we have to finish all this work, do not be afraid, follow me!!!
A journey begins
Create a new project, application name fill weather,package name com.demo.weather,sdk the options for each project are as follows:
Once you've chosen, just click "Next" until our project is built, and the project should look like this:
Open the Res/layout/activity_main.xml, which should look like this in eclipse:
Note that there are two tab tabs below the view, "Graphical Layout" is a graphical view, "Activity_main.xml" is a code view, we switch to Code view, add a line of code at TextView,
Android:id= "@+id/weather"
In this way, we can use the Findviewbyid method in the code to find the TextView control, but for the sake of the beauty of the code, we will use a different approach.
Using third-party components
As we all know, Android is open source, so there are a lot of dedicated cattle people to share their experience and harvest to everyone, this is a lot of open source code, components, frameworks.
Here we introduce the first open source Component--xutils, which can be viewed here in detail in Https://github.com/wyouflf/xUtils.
First of all, to download the jar package and import it into our project, it is quite simple to say that it is troublesome to do it. : Https://github.com/wyouflf/xUtils/blob/master/xUtils-2.6.11.jar, then put the downloaded files into the Libs folder on the project.
How do I use it in a project?
If everyone is too lazy to read Xutils's documentation, then I'll do it one step at a glance.
Open the SRC directory, com.demo.weather the mainactivity file, add a variable that can be named Txtweather.
@ViewInject (r.id.weather) private TextView txtweather;
In this way, we can use the TextView control in our code.
Next, let's take a look at the OnCreate method, which now has only two lines of code,
Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);
The first line, calling the parent class's OnCreate method to draw the interface, the second line is the loading interface, the two lines of code in the activity of the subclass are required to call, unless you do not display anything in the interface.
The OnCreate method has a parameter savedinstancestate, about which we'll talk about this later, first, the second line of code.
Setcontentview (R.layout.activity_main);
R.layout.activity_main points to the Res/layout/activity_main.xml file, so the interface knows that the interface file should be loaded. Maybe we all have activity is what still some doubts, do not matter, these theoretical knowledge we say tomorrow, today our task is to show the weather data to the interface. Of course, we do not know the weather forecast data, but the almighty Internet has everything, Baidu provides this data.
Official documents: Http://developer.baidu.com/map/index.php?title=car/api/weather,
An example: http://api.map.baidu.com/telematics/v3/weather?location=%E5%8C%97%E4%BA%AC&output=json&ak= Ykngmxiopugt7yrnrg955yls
All right, all ready, just owe the code. How to display the weather to the interface, a total of three steps:
1. Place a textview on the interface to display the text, which we have done.
2. Call Baidu Map API, get weather data, this we will do next.
3. Display the data to the interface.
Relax, have a cup of tea, rest for 10 minutes, and there will be a large section of code waiting for you to complete.
Rest well, we'll start right away.
Add the following code below the Setcontentview of the OnCreate method,
Viewutils.inject (this); Httputils http = new Httputils (); Requestparams params = new Requestparams (); Params.addquerystringparameter ("Location", "Beijing"); Params.addquerystringparameter ("Output", "json"); Params.addquerystringparameter ("AK", "ykngmxiopugt7yrnrg955yls"); Http.send (Httpmethod.get, "Http://api.map.baidu.com/telematics/v3/weather", params, new requestcallback<string > () { @Override public void onsuccess (responseinfo<string> responseinfo) { String Weather = Responseinfo.result; Txtweather.settext (weather); } @Override public void OnFailure (HttpException arg0, string arg1) { string weather = Responseinfo.result ; Txtweather.settext (weather); } } );
and delete the Oncreateoptionsmenu method.
Save it, a large section of the code, a large red fork, hehe, not anxious, slowly, that is because we use the third-party components of the reason, one of the missing references to add in it. If you are in trouble, then copy these lines of code.
Import Com.lidroid.xutils.httputils;import Com.lidroid.xutils.viewutils;import Com.lidroid.xutils.exception.httpexception;import Com.lidroid.xutils.http.requestparams;import Com.lidroid.xutils.http.responseinfo;import Com.lidroid.xutils.http.callback.requestcallback;import Com.lidroid.xutils.http.client.httprequest.httpmethod;import Com.lidroid.xutils.view.annotation.ViewInject;
Everyone is satisfied, everything looks so smooth and perfect, come on, run the program.
What you see may be something like this:
Java.io.IOException:Permission denied (missing INTERNET Permission?)
Why? Why? Why?
Worked hard, XX x, you let me see this.
No hurry, no hurry, the above is written very clearly, no access to the network, then, in the project found Androidmanifest.xml this file, add a line of code can.
<uses-permission android:name= "Android.permission.INTERNET"/>
Add this line of code to the front of the <application, OK, everything is OK. Run it.
This next screen shows a lot of weather data.
If the display is not correct, then please carefully check if there is missing code, whether it can really connect to the network.
If you can correctly display the above-mentioned large pile of characters, and can correctly understand the meaning of the large pile of characters, and there is no requirement for the interface and so on, then our weather app can be used for their own exclusive use. It's a very fulfilling feeling, isn't it, but who uses it in addition to its own? I'm afraid I don't even want to see myself.
If you have more requirements, then tomorrow we continue.
The attachment is the project document, click Download.
10 days to learn Android-the next day