JSON Query Example (Weather):
http://api.openweathermap.org/data/2.5/forecast/daily?zip=92127,us&mode=JSON&units=metric&cnt=7&appid=APIKEYHERE
Parameters:
* 7 day forecast * JSON * Metric units * 92127, USA * My personal API Key
Two clients available: HttpUrlConnection or HttpClient
adb logcat prints errors and also exists on the Android Device Monitor Can filter out error tags in DM
Error Logging
Log Levels | Log API |
---|---|
Error | Log.e(String tag, String msg) |
Warn | Log.w (…) |
Info | Log.i (…) |
Debug | Log.d (…) |
Verbose | Log.v (…) |
Main Thread
* UI thread + user input and output * Cannot run network requests
Background Thread
* Long running work * Read and write from/to databases
Android uses the AsyncTask class to simply background thread creation and UI thread synchronization Documentation
The three types used by an asynchronous task are the following:
Not all types are always used by an asynchronous task. To mark a type as unused, simply use the type Void:
private class MyTask extends AsyncTask<Void, Void, Void> { … } doInBackground() is required but onPreExecute(), onProgressUpdate(), onPostExecute() are not required.
When an asynchronous task is executed, the task goes through 4 steps:
With the ability to send messages to server and run background tasks, there is no need for a refresh button. That should only be for debugging purposes. If thread is determined by UI activity it can interrupted and the data transfer can be stopped by a simple orientation flip.{: .notice_danger}
Because of this, AsyncTask should only be used for tasks tied to the host activity and only run for a few seconds. In the real world, network access is unlikely to run in perfectly little time. Therefore, its better to use a service.
An application component without a UI that’s less likely to be interrupted. Android has a specialized solution known as a Sync Adapter designed to schedule your background data syncs as efficiently as possible. You can also use Google Cloud Messenger that notifies the Sync Adapter on the server side. So there will only be updates when you know for a fact there are things to be updated.
Override the methods onCreate and OptionsMenu in order to instruct commands on how to inflate views we created in .xml and actions after buttons are clicked.
Security Section
Permission List
Which of the following activities can only be done after declaring a user-permission in the manifest? * Taking a photo * Making a phone call * Accessing contact data * Retrieving current location
The answer is: Retrieving current location
The other activities rely on other applications (phone, contacts, camera) and the user can cancel this action. Current location comes from the UI thread and requires explicit user permission.
What permission must you add to the manifest in order to declare the Internet permission?
The answer is: android.permission.INTERNET