Skip to content
Extraits de code Groupes Projets
Valider 5ae0e39c rédigé par Monnot's avatar Monnot
Parcourir les fichiers

Ended TD2, half of TD3 and start of TD4

parent 24f32f1d
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<GradleProjectSettings> <GradleProjectSettings>
<option name="testRunner" value="PLATFORM" /> <option name="testRunner" value="PLATFORM" />
<option name="disableWrapperSourceDistributionNotification" value="true" />
<option name="distributionType" value="DEFAULT_WRAPPED" /> <option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="1.8" /> <option name="gradleJvm" value="1.8" />
......
plugins { plugins {
id 'com.android.application' id 'com.android.application'
id 'kotlin-android' id 'kotlin-android'
id 'org.jetbrains.kotlin.plugin.serialization' version '1.3.72' // vérifier que votre version de kotlin est la même
} }
android { android {
...@@ -28,7 +29,7 @@ android { ...@@ -28,7 +29,7 @@ android {
targetCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8
} }
kotlinOptions { kotlinOptions {
jvmTarget = '1.8' jvmTarget = JavaVersion.VERSION_1_8.toString()
} }
} }
...@@ -47,4 +48,26 @@ dependencies { ...@@ -47,4 +48,26 @@ dependencies {
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
// AndroidX - KTX
implementation 'androidx.preference:preference-ktx:1.1.1'
implementation 'androidx.activity:activity-ktx:1.2.0-beta01'
implementation 'androidx.fragment:fragment-ktx:1.3.0-beta01'
implementation 'androidx.core:core-ktx:1.3.2'
// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'
// KotlinX Serialization
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.1"
implementation 'com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0'
// Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9"
// Lifecycle
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
} }
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.clemhaowen.dm_td2"> package="com.clemhaowen.dm_td2">
<uses-permission android:name="android.permission.INTERNET" />
<application <application
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
......
package com.clemhaowen.dm_td2.network
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import retrofit2.Retrofit
object Api {
// constantes qui serviront à faire les requêtes
private const val BASE_URL = "https://android-tasks-api.herokuapp.com/api/"
private const val TOKEN = "eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoyOTAsImV4cCI6MTYzODg4NTIyMX0.NSGoVvI5U18v4xMHB-_SDFArkT-Exz8S-aXp1ptqNb4"
// on construit une instance de parseur de JSON:
private val jsonSerializer = Json {
ignoreUnknownKeys = true
coerceInputValues = true
}
// instance de convertisseur qui parse le JSON renvoyé par le serveur:
private val converterFactory =
jsonSerializer.asConverterFactory("application/json".toMediaType())
// client HTTP
private val okHttpClient by lazy {
OkHttpClient.Builder()
.addInterceptor { chain ->
// intercepteur qui ajoute le `header` d'authentification avec votre token:
val newRequest = chain.request().newBuilder()
.addHeader("Authorization", "Bearer $TOKEN")
.build()
chain.proceed(newRequest)
}
.build()
}
// permettra d'implémenter les services que nous allons créer:
private val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(converterFactory)
.build()
val userService: UserService by lazy {
retrofit.create(UserService::class.java)
}
}
\ No newline at end of file
package com.clemhaowen.dm_td2.network
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class UserInfo (
@SerialName("email")
val email: String,
@SerialName("firstname")
val firstName: String,
@SerialName("lastname")
val lastName: String
)
\ No newline at end of file
package com.clemhaowen.dm_td2.network
import retrofit2.Response
import retrofit2.http.GET
interface UserService {
@GET("users/info")
suspend fun getInfo(): Response<UserInfo>
}
\ No newline at end of file
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter