put here the title of the presentationprofs.scienze.univr.it/~perbellini/labsoa/7-8... · di java...
TRANSCRIPT
![Page 1: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/1.jpg)
Programmazione
Android
Giovanni Perbellini
Università di Verona – EDALab S.r.l.
![Page 2: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/2.jpg)
Agenda
• Introduzione Android
– Overview
– Architettura SW
– Ambiente di sviluppo (Android Studio)
• Esempi
– Helloworld
– MVC programming
2
![Page 3: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/3.jpg)
3
Cos’è Android?
• Stack software che include:
– Sistema operativo
– Middleware
– Applicazioni
• Android SDK
– Fornisce gli strumenti e le API per sviluppare
applicazioni su terminali mobili Android
utilizzando il linguaggio di programmazione
Java
![Page 4: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/4.jpg)
Android overview
• Sviluppato da Google Inc.
• Linguaggio di programmazione: C/C++ e Java
• Basato su Kernel Linux (monolithic kernel)
• Ultima versione stabile: 6.0 (Marshmallow)
• Ultima versione delle API: 23 (API level)
• O.S. per embedded systems: smartphone, netbooks,
tablet PC, smart watch, TV, etc.
4
![Page 5: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/5.jpg)
Architettura Android
5
ART Dalvik
Virtual machine
![Page 6: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/6.jpg)
Application
• Calendar
• Agenda
• Maps
• Browser
• SMS program
• Etc.
6
![Page 7: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/7.jpg)
Application framework (I)
• Insieme di componenti, API e servizi utili
per lo sviluppo di nuove applicazioni.
– Stesse API usate per lo sviluppo delle core
applications
• Si sfrutta il concetto del riuso dei
componenti
– ogni applicazione può usare le
potenzialità/caratteristiche di altre applicazioni
7
![Page 8: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/8.jpg)
Application framework (II)
• Include i seguenti servizi:
– View
• Lists, grids, buttons, text boxes, web browser, etc.
– Content providers
• Permette alle applicazioni di accedere ai dati di altre applicazioni (e.g.,
Agenda) oppure di condividere i propri dati ad altre applicazioni.
– Resourse manager
• Accesso a risorse del tipo: file esterni (XML, PNG, JPEG) inclusi
nell’applicazione a build-time.
– Notification manager
• Per fornire alle applicazioni uno strumento di notifica
– Activity manager
• Gestisce il ciclo di vita di ogni applicazione
8
![Page 9: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/9.jpg)
Libraries
• Insieme di librerie C/C++ usate dai vari
componenti di Android
• Esportate allo sviluppatore attraverso Android
application framework
• Alcuni esempi:
– System C library: implementazione della libc per
embedded system
– SQLite: DB embedded
– Media libraries: gestione di componenti audio video
– etc.
9
![Page 10: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/10.jpg)
Android runtime
• Include le librerie che forniscono le funzionalità principali
di Java
– Collections, I/O, etc.
• Dalvik Virtual Machine
– Esegue file nel formato .dex (Dalvik Executable)
– Sfrutta la compilazione JIN (Just-in-Time) eseguita durante
l’esecuzione
• ART (Android Runtime)
– A partire da Android 4.3/4.4
– Esegue file nel formato .dex (Dalvik Executable)
– Introduce la compilazione AOT (Ahead-of-Time) eseguita
durante l’installazione
10
![Page 11: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/11.jpg)
11
![Page 12: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/12.jpg)
Linux kernel
• Sfrutta Linux kernel 2.6 (kernel 3.10.x per
Android 5.0) per i servizi principali come:
– Security, memory management, process
management, network stack, driver model.
• Include l’ HW Abstraction Layer (HAL)
12
![Page 13: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/13.jpg)
Kernel Android
• Monolithic kernel
13
![Page 14: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/14.jpg)
Ambiente di sviluppo
1. Android Standard Development Kit (SDK)
– Insieme di tool per sviluppare applicazioni Android
2. Android Studio
– Android Virtual Device (AVD)
3. Android Native Development Kit (NDK)
14
![Page 15: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/15.jpg)
Android SDK
• Contiene API, compilatore, debugger, emulatore (Android
Virtual Device), etc.
• SDK Manager
– Aggiornamento pacchetti
• Platforms API
• Tools
• USB device
15
![Page 16: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/16.jpg)
Android SDK – emulator (i)
• Fornisce un dispositivo virtuale sul
quale caricare le App Android
– Pulsanti di controllo e navigazione con
i quali si possono generare eventi
• L’emulatore traduce dinamicamente il
codice macchina del dispositivo sul
OS guest della macchina di sviluppo
• Utilizza le configurazioni Android
Virtual Device (AVD)
– Definisce le caratteristiche HW
dell’emulatore (Display, RAM, etc.)
16
![Page 17: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/17.jpg)
Android SDK – emulator (ii)
• Caratteristiche HW supportate dall’emulatore – ARMv5, ARMv7, o CPU x86
– 16-bit LCD display
– Una o più keyboards (Qwerty-based keyboard e Dpad/Phone
buttons)
– Chip sound con funzionalità output e input
– Flash memory (emulata attraverso immagini di file su disco)
– Modem GSM, inclusa SIM Card simulata
– Camera, usando una webcam connessa al Pc di sviluppo
– Sensors come accelerometer
17
![Page 18: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/18.jpg)
Android SDK – emulator (iiI)
• Path SDK (elenco
dei dispositivi installati)
18
![Page 19: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/19.jpg)
Android Studio
19
Basato su IntelliJ IDEA
Sistema Gradle per la compilazione
![Page 20: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/20.jpg)
Android Virtual Device
• Inserire un nuovo Virtual Device
20
![Page 21: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/21.jpg)
Progetti “command line”
• Possibilità di creare un progetto Android senza Android Studio IDE
– Strumenti necessari
• IDE
• Android SDK: include il tool “android” per creare tutto il codice sorgente,
directory, file per la compilazione
• Esempio di creazione del progetto HelloAndroid android create project
--package com.android.helloandroid
--name <your_project_name>
--activity <my activity>
--target <target id>
--path <path-to-your-project>/FirstApp
21
![Page 22: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/22.jpg)
22
![Page 23: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/23.jpg)
Activity (I)
• E’ un singolo task di una applicazione
Android
– Per esempio è la rappresentazione di una
window dove l’utente può inserire la sua UI
• onCreate(Bundle): e' richiamato
quando l'activity viene avviata per la prima
volta (per esempio quando l’utente
selezione l’app dallo schermo)
23
![Page 24: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/24.jpg)
Activity – ciclo di vita (II)
24
![Page 25: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/25.jpg)
Esempio: First App (I)
• Creare il nuovo progetto:
– File -> New -> New Project...
25
![Page 26: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/26.jpg)
Esempio: First App (II)
• Selezione SDK
26
![Page 27: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/27.jpg)
Esempio: First App (III)
• Impostare il nome dell’Activity, Layout, etc
27
![Page 28: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/28.jpg)
Esempio: First App (IV)
• Accedere al file contenente Activity Java
28
![Page 29: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/29.jpg)
Esempio: First App (V)
• Editare il file MainActivity.java aggiungendo:
// We want to view some very simple text, so we need a TextView
TextView tv = new TextView(this);
// Put some text to the newly created TextView
tv.setText("Laboratorio di SOA");
// Tell our App to display the textView
this.setContentView(tv);
• Commentare la riga:
setContentView(R.layout.activityMain);
29
![Page 30: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/30.jpg)
Esempio: run First App (VI)
• Esecuzione dell’applicazione
(emulatore o device fisico)
– Premere “Run App”
– Selezionare il dispositivo
30
![Page 31: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/31.jpg)
Esempio: run First App (VII)
• Dispositivo fisico
– Plug-in USB
• Installa il device driver del tuo dispositivo
– Abilita USB-debugging sul tuo dispositivo
• Emulatore – Selezione AVD
31
![Page 32: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/32.jpg)
Model View Controller (MVC) (I)
• Definisce tre oggetti per implementare un’applicazione
– Model, View, Controller
• Definisce l’interazione (comunicazione) tra gli oggetti
• Benefici
– Riusabilità del codice, interfacce meglio definite, facilità di
estensione dell’applicazione
32
![Page 33: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/33.jpg)
Model View Controller (MVC) (II)
• Model
– Rappresenta i dati dell’applicazione e definisce la logica di manipolazione dei
dati (definisce i metodi di accesso)
– Nessuna esplicita connessione con View objects che presenta i dati (agevola la
riusabilità del codice)
– Comunicazione: e.g., la modifica dei dati (ricevuti esternamente) notifica il
Controller object che aggiorna il View object
• View
– Presentazione dei dati del Model object dell’applicazione (interazione con
l’utente)
– Comunicazione: e.g., inserimenti di dati in un Textfield passano attraverso il
Controller object che aggiorna il Model object
• Controller
– Intermediario tra il Model object e il View object
33
![Page 34: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/34.jpg)
MVC in Android
34
View Controller Model
XML Format
Activity
Data
![Page 35: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/35.jpg)
Esempio: programmazione XML (I)
• Aggiunta di elementi grafici attraverso la modalità visuale (XML)
35
– Modifica del file res -> layout -> activity_main.xml
– Modifica del file res -> values -> string.xml
![Page 36: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/36.jpg)
Esempio: programmazione XML (II)
• Modifica file activity_main.xml
– Inserisci un pulsante sul layout attraverso il
drag/drop dall’insieme dei widget disponibili
– Editare activity_main.xml
• impostare android:text=“Click me"
• Salvare i cambiamenti
36
![Page 37: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/37.jpg)
Esempio: programmazione XML (III)
37
![Page 38: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/38.jpg)
Esempio: programmazione XML (III)
38
![Page 39: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/39.jpg)
Esempio: programmazione XML (IV)
• Modifica del file .java
setContentView(R.layout.main);
Button bnt = (Button)findViewById(R.id.Button01);
bnt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(MainActivity.this,“/=\\ Message /=\\
World",Toast.LENGTH_SHORT).show();
}
});
39
![Page 40: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/40.jpg)
Esempio: programmazione XML (V)
40
![Page 41: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/41.jpg)
Esempio: HTTP connection (I)
• Interazione con le API HTTP Android per il
recupero di informazioni legate alle previsioni
metereologiche a partire da Longitudine e
Latitudine:
– Esempio di connessione HTTP GET:
http://graphical.weather.gov/xml/sample_products/bro
wser_interface/ndfdBrowserClientByDay.php?&lat=39
.91&lon=-121.29&format=24+hourly&numDays=1
41
![Page 42: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/42.jpg)
Esempio: HTTP connection (II)
public class MainActivity extends Activity {
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bnt = (Button)findViewById(R.id.button1);
txt = (TextView)findViewById(R.id.lblTextViewBig);
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
bnt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
connect();
}
});
}}
42
![Page 43: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/43.jpg)
Esempio: HTTP connection (III) private void connect() {
try {
InputStream is = null;
int len = 1500;
//HttpClient client = new DefaultHttpClient();
URL url = new
URL("http://graphical.weather.gov/xml/sample_products/browser_interface/ndfdBrowserClientByDay.php?&lat=39.91&lon=-121.29&format=24+hourly&numDays=1");
Toast.makeText(MainActivity.this,"HTTP GET Connection",Toast.LENGTH_SHORT).show();
Log.i("QUERY", url.toString());
//HttpGet get = new HttpGet(getURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
…
…
43
![Page 44: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/44.jpg)
Esempio: HTTP connection (IV) …
// Starts the query
conn.connect();
is = conn.getInputStream();
// Convert the InputStream into a string
String contentAsString = readIt(is, len);
txt.setText(contentAsString);
} catch (Exception e) {
e.printStackTrace();
Log.e("ERROR HELLO",e.toString());
}
}
44
![Page 45: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/45.jpg)
Esempio: HTTP connection (V)
• Aggiunta al manifest <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
45
![Page 46: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/46.jpg)
Esempio: HTTP connection (VI)
• Main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:text="Start" />
…
…
46
![Page 47: Put here the title of the presentationprofs.scienze.univr.it/~perbellini/LabSOA/7-8... · di Java –Collections, I/O, etc. • Dalvik Virtual Machine –Esegue file nel formato .dex](https://reader036.vdocuments.net/reader036/viewer/2022071000/5fbc78e18e56da6f875880bf/html5/thumbnails/47.jpg)
Esempio: HTTP connection (VII)
…
<ScrollView
android:id="@+id/scrollViewOne"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/lblTextViewBig"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="..." />
</ScrollView>
</LinearLayout>
47