Lesson 11 Connect To The Internet
Lesson 11 Connect To The Internet
Connect to the
internet
Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this lesson
Lesson 11: Connect to the internet
● Android permissions
● Connect to, and use, network resources
● Connect to a web service
● Display images
● Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 2
Android permissions
Android Development with Kotlin This work is licensed under the Apache 2 license. 3
Permissions
Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Permissions granted to your app
Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Permission protection levels
Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Add permissions to the manifest
In AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sampleapp">
<uses-permission android:name="android.permission.USE_BIOMETRIC" />
<application>
<activity
android:name=".MainActivity" ... >
...
</activity>
</application>
</manifest>
Android Development with Kotlin This work is licensed under the Apache 2 license. 7
Internet access permissions
In AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Android Development with Kotlin This work is licensed under the Apache 2 license. 8
Request dangerous permissions
Android Development with Kotlin This work is licensed under the Apache 2 license. 9
Prompt for dangerous permission
Android Development with Kotlin This work is licensed under the Apache 2 license. 10
App permissions best practices
Android Development with Kotlin This work is licensed under the Apache 2 license. 11
Connect to, and use,
network resources
Android Development with Kotlin This work is licensed under the Apache 2 license. 12
Retrofit
● Networking library that turns your HTTP API into a Kotlin and
Java interface
● Enables processing of requests and responses into objects for
use by your apps
○ Provides base support for parsing common response types,
such as XML and JSON
○ Can be extended to support other response types
Android Development with Kotlin This work is licensed under the Apache 2 license. 13
Why use Retrofit?
Android Development with Kotlin This work is licensed under the Apache 2 license. 14
Add Gradle dependencies
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "com.squareup.retrofit2:converter-moshi:2.9.0"
implementation "com.squareup.moshi:moshi:$moshi_version"
implementation "com.squareup.moshi:moshi-kotlin:$moshi_version"
kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshi_version"
Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Connect to a web service
Android Development with Kotlin This work is licensed under the Apache 2 license. 16
HTTP methods
● GET
● POST
● PUT
● DELETE
Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Example web service API
Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Define a Retrofit service
interface SimpleService {
@GET("posts")
suspend fun listAll(): List<Post>
@GET("posts/{userId}")
suspend fun listByUser(@Path("userId") userId:String): List<Post>
@POST("posts/new")
suspend fun create(@Body post : Post): Post
}
Android Development with Kotlin This work is licensed under the Apache 2 license. 19
Create a Retrofit object for network access
Android Development with Kotlin This work is licensed under the Apache 2 license. 20
End-to-end diagram
Retrofit Service
HTTP Request
App UI Server
ViewModel Converter HTTP Response Web API
(JSON)
Moshi
Android Development with Kotlin This work is licensed under the Apache 2 license. 21
Converter.Factory
Android Development with Kotlin This work is licensed under the Apache 2 license. 22
Moshi
List of JSON
Moshi
Post objects response
Android Development with Kotlin This work is licensed under the Apache 2 license. 23
Moshi JSON encoding
@JsonClass(generateAdapter = true)
data class Post (
val title: String,
val description: String,
val url: String,
val updated: String,
val thumbnail: String,
val closedCaptions: String?)
Android Development with Kotlin This work is licensed under the Apache 2 license. 24
JSON code
{
"title":"Android Jetpack: EmojiCompat",
"description":"Android Jetpack: EmojiCompat",
"url":"https://www.youtube.com/watch?v=sYGKUtM2ga8",
"updated":"2018-06-07T17:09:43+00:00",
"thumbnail":"https://i4.ytimg.com/vi/sYGKUtM2ga8/hqdefault.jpg"
}
Android Development with Kotlin This work is licensed under the Apache 2 license. 25
Set up Retrofit and Moshi
private val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.baseUrl(BASE_URL)
.build()
object API {
val retrofitService : SimpleService by lazy {
retrofit.create(SimpleService::class.java)
}
}
Android Development with Kotlin This work is licensed under the Apache 2 license. 26
Use Retrofit with coroutines
viewModelScope.launch {
Log.d("posts", API.retrofitService.searchPosts("query"))
}
Android Development with Kotlin This work is licensed under the Apache 2 license. 27
Display images
Android Development with Kotlin This work is licensed under the Apache 2 license. 28
Glide
Android Development with Kotlin This work is licensed under the Apache 2 license. 29
Add Gradle dependency
implementation "com.github.bumptech.glide:glide:$glide_version"
Android Development with Kotlin This work is licensed under the Apache 2 license. 30
Load an image
Glide.with(fragment)
.load(url)
.into(imageView);
Android Development with Kotlin This work is licensed under the Apache 2 license. 31
Customize a request with RequestOptions
Android Development with Kotlin This work is licensed under the Apache 2 license. 32
RequestOptions example
@BindingAdapter("imageUrl")
fun bindImage(imgView: ImageView, imgUrl: String?) {
imgUrl?.let {
val imgUri = imgUrl.toUri().buildUpon().scheme("https").build()
Glide.with(imgView)
.load(imgUri)
.apply(RequestOptions()
.placeholder(R.drawable.loading_animation)
.error(R.drawable.ic_broken_image))
.into(imgView)
}
}
Android Development with Kotlin This work is licensed under the Apache 2 license. 33
Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 34
Summary
In Lesson 11, you learned how to:
● Declare permissions your app needs in AndroidManifest.xml
● Use t
he three protection levels for permissions: normal, signature, and dan
gerous (prompt the user at runtime for dangerous permissions)
● Use the Retrofit library to make web service API calls from your app
● Use the Moshi library to parse JSON response into class objects
● Load and display images from the internet using the Glide library
Android Development with Kotlin This work is licensed under the Apache 2 license. 35
Learn More
Android Development with Kotlin This work is licensed under the Apache 2 license. 36
Pathway
Android Development with Kotlin This work is licensed under the Apache 2 license. 37