This shows you the differences between two versions of the page.
smd:laboratoare:05 [2021/04/14 19:36] adriana.draghici [Tasks] |
smd:laboratoare:05 [2021/05/18 22:17] (current) adriana.draghici [Tasks] |
||
---|---|---|---|
Line 1: | Line 1: | ||
===== Lab 5. HTTP and web services ===== | ===== Lab 5. HTTP and web services ===== | ||
- | <note important>This page hasn't been updated yet for the 2021 semester and may contain outdated information</note> | ||
==== Objectives ==== | ==== Objectives ==== | ||
Line 43: | Line 42: | ||
</code> | </code> | ||
- | If order for the network request to work we need to have neccessary permissions: | + | If order for the network request to work we need to have necessary permissions: |
- | * For access to Internet | + | * For Internet access |
<code><uses-permission android:name="android.permission.INTERNET" /></code> | <code><uses-permission android:name="android.permission.INTERNET" /></code> | ||
Line 52: | Line 51: | ||
<code><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /></code> | <code><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /></code> | ||
- | Our application should not cause indirect costs to the user so that’s why we need to check what type of network connection do we need for our request. In the code snippet below we are checking that we are connected on the WiFi in order to start our file download. | ||
- | <code> | + | If your app needs to check the network's state it can use the system service ConnectivityManager - [[https://developer.android.com/training/basics/network-ops/reading-network-state|documentation examples]]. |
- | ConnectivityManager connectivityManager = | + | |
- | (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); | + | |
- | NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); | + | |
- | if (networkInfo != null && networkInfo.isConnected() && | + | If your app needs to react to connectivity changes (e.g. send a file when the network becomes available) you can use Android's job scheduling API called [[https://developer.android.com/topic/libraries/architecture/workmanager|WorkManager]], since newer Android versions removed the system broadcast for connectivity changes. Here's an example (scenario #3) of how to use it: [[https://medium.com/ki-labs-engineering/monitoring-wifi-connectivity-status-part-1-c5f4287dd57|example]]. |
- | networkInfo.getType() == ConnectivityManager.TYPE_WIFI && | + | |
- | networkInfo.getType() != ConnectivityManager.TYPE_MOBILE)) { | + | |
- | downloadFileTask.execute(fileUrl); | + | |
- | } | + | |
- | </code> | + | |
- | /* THE NETWORKINFO STUFF IS DEPRECATED AND NEEDS TO BE CHANGED | ||
- | */ | ||
- | <note important>Note for Android 9 and above http calls will receive an IOException because cleartext network traffic is disabled. You can add your application trusted domains in a network security configuration file or to enable cleartext within the application manifest. Check [[https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted|Cleartext Traffic Permitted]]</note> | + | <note important>Note for Android 9 and above HTTP calls will receive an IOException because cleartext network traffic is disabled. You can add your application trusted domains in a network security configuration file or to enable cleartext within the application manifest. Check [[https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted|Cleartext Traffic Permitted]]</note> |
==== HttpsUrlConnection ==== | ==== HttpsUrlConnection ==== | ||
Line 180: | Line 168: | ||
</code> | </code> | ||
<code> | <code> | ||
+ | apply plugin: 'kotlin-kapt' | ||
+ | |||
dependencies { | dependencies { | ||
implementation 'com.squareup.retrofit2:retrofit:2.9.0' | implementation 'com.squareup.retrofit2:retrofit:2.9.0' | ||
- | implementation("com.squareup.moshi:moshi-kotlin:1.12.0") | + | implementation 'com.squareup.retrofit2:converter-moshi:2.9.0' |
+ | implementation 'com.squareup.moshi:moshi-kotlin:1.12.0' | ||
+ | |||
+ | kapt "com.squareup.moshi:moshi-kotlin-codegen:1.11.0" | ||
} | } | ||
</code> | </code> | ||
+ | <note>Kotlin example for retrofit on our lab's repo: [[https://github.com/SMD-UPB/labs/tree/main/lab5-demos/retrofit-demo-kotlin|retrofit-demo-kotlin]]</note> | ||
==== Tasks ==== | ==== Tasks ==== | ||
Line 235: | Line 228: | ||
=== Resources === | === Resources === | ||
- | {{:smd:laboratoare:lab5_skel.zip|}} | + | * {{:smd:laboratoare:lab5_skel.zip|}} |
+ | * [[https://github.com/SMD-UPB/labs/tree/main/demos|Demo app for Retrofit shown during a lab]] | ||