Unterlage zur Android Vorlesung "Programmierung von Apps" an der Hochschule Karlsruhe.
Die Studierenden kennen die Möglichkeiten fürAndroid Persistence und Networking und verstehen, wie damit auf Daten und Netzwerke zugegriffen werden kann.
System-Management-Trio: Zentrale Verwaltung mit facter, puppet und augeasSpeedPartner GmbH
Weitere ähnliche Inhalte
Android Persistence and Networking
1. Programmierung
von Apps
Android Persistence
and Networking
Danny Fürniß, 25.11.2012, 1
2. Die Studierenden
kennen die Möglichkeiten für
Android Persistence und Networking
und verstehen, wie damit auf Daten
und Netzwerke zugegriffen
werden kann.
Danny Fürniß, 25.11.2012, 2
5. openFileInput()
openFileOutput()
MODE_PRIVATE – Kein Zugriff durch andere Apps
MODE_WORLD_READABLE – Lesender Zugriff durch andere Apps
MODE_WORLD_WRITABLE – Schreibender Zugriff durch andere Apps
MODE_WORLD_READABLE | MODE_WORLD_WRITABLE – Lese- und Schreib-Zugriff
Danny Fürniß, 25.11.2012, 5
13. SQLite
• Keine Konfiguration notwendig
• Kein Datenbank-Server
• Single-File Datenbank (Filesystem-Security)
• Open Source
• In Android integriert
• Keine strikte statische Typisierung
• sqlite.org
Danny Fürniß, 25.11.2012, 13
14. SQLite DataTypes
• TEXT
• INTEGER
• REAL
No checks!
• Date and Time (functions)
• TEXT as ISO8601 strings
("YYYY-MM-DD HH:MM:SS.SSS").
Danny Fürniß, 25.11.2012, 14
• INTEGER as Unix Time, the number of
seconds since 1970-01-01 00:00:00 UTC.
Quelle: http://www.sqlite.org/datatype3.html
15. extend SQLiteOpenHelper
• Erzeugt Datenbank
• onCreate()
• Aktualisiert Datenbank
• onUpgrade()
• Ermöglicht Zugriff auf die Datenbank
• getReadableDatabase()
• getWritableDatabase()
Danny Fürniß, 25.11.2012, 15
Demo
16. onCreate()
public void onCreate(SQLiteDatabase db) {
db.execSQL(NewsItemContract.Table.CREATE_TABLE_NEWS);
db.execSQL("CREATE TABLE xyz (_id PRIMARY KEY AUTOIN…");
}
Danny Fürniß, 25.11.2012, 16
17. onUpgrade()
public void onUpgrade(SQLiteDatabase db,
int oldVersion, int newVersion) {
db.execSQL("drop table if exists " +
NewsItemContract.Table.NEWS);
onCreate(db);
}
Danny Fürniß, 25.11.2012, 17
20. query()
SQL Interface
SELECT edvNr, name projection/columns
FROM lehrveranstaltung table
WHERE name selection
= "Prof. Dr.‐Ing. Vogelsang" selectionArgs
groupBy = null
Danny Fürniß, 25.11.2012, 20
having = null
ORDER BY edvNr orderBy
32. Representational State
Transfer (REST)
Architekturstil HTTP-Verben
basierend auf WWW POST
GET
URI, Ressourcen, Repräsentationen
http://localhost:8180/api/news PUT
http://localhost:8180/api/news/3
http://localhost:8180/api/persons DELETE
Danny Fürniß, 25.11.2012, 32
etc.
json, xml, html, etc. http://www.slideshare.net/dnene/rest‐
representational‐state‐transfer‐explained
33. JSON
JSONObject
JSONArray
JSONObject
{ "newsItem" : [ { "id" : 3,
"imageFileName" : "news3.jpg",
"pictureUri" : "http://localhost:8180/images/news3.jpg",
"published" : "2011‐12‐20T13:07:54.480+01:00",
"teaser" : "für Innovationen in der Hochschullehre",
"title" : "Prof. Dr. Andreas Heberle erhält für gute Lehre Förderpreis von 50 000 Euro"
} ] }
Danny Fürniß, 25.11.2012, 33
Demo
34. XML APIs
SAX (Simple API for XML)
DOM (Document Object Model)
StAX (Streaming API for XML)
Danny Fürniß, 25.11.2012, 34
Quelle: http://www.torsten-horn.de/techdocs/java-xml.htm
35. XmlPullParser
XmlPullParserFactory.newPullParser().
Danny Fürniß, 25.11.2012, 35
Quelle: http://www.xmlpull.org; http://android-developers.blogspot.com/2011/12/watch-out-for-xmlpullparsernexttext.html
36. XML
START_TAG, name = person
START_TAG, name = akademischerTitel
Text
<person>
<akademischerTitel>Prof. Dr.</akademischerTitel>
<displayName>Albrecht, Ditzinger</displayName>
<imageURI>http://localhost:8180/images/I_2e8f4d534f.jpg</imageURI>
<personURI>http://localhost:8180/api/persons/3</personURI>
</person>
Danny Fürniß, 25.11.2012, 36
END_TAG, name = person
Demo
39. URIs
Multi Item
content://com.dfuerniss.pva.ss2012.database.contentprovider/lehrveranstaltungen
Single Item
content://com.dfuerniss.pva.ss2012.database.contentprovider/lehrveranstaltungen/#
Platzhalter für die Id der
Lehrveranstaltung
Danny Fürniß, 25.11.2012, 39
Demo
48. AsyncTaskLoader
@Override
public Cursor loadInBackground() {
Log.i(TAG, "load in background");
SQLiteDatabase db = ((Application) getApplication())
.getDbHelper().getReadableDatabase();
return db.query(
NewsItemContract.Table.NEWS,
// Projection muss _id enthalten!
NewsItemContract.Query.PROJECTION_ALL,
selection,
selectionArgs,
groupBy,
having,
Danny Fürniß, 25.11.2012, 48
orderBy);
}
@Override
public void onStartLoading() { …forceLoad()… }
49. CursorLoader
CursorLoader cl =
new CursorLoader(context,
URI,
projection,
selection,
selectionArgs,
sortOrder
);
Danny Fürniß, 25.11.2012, 49
50. android:installLocation
internalOnly (default)
auto
preferExternal
Danny Fürniß, 25.11.2012, 50
51. Portions of this presentation
are modifications based on
work created and shared by
Google and used according
to terms described in
the Creative Commons 3.0
Attribution License.
Danny Fürniß, 25.11.2012, 51