Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
Connection to API (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
S3JER authored Apr 4, 2024
1 parent 27c7bc8 commit 4098fc5
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 52 deletions.
72 changes: 26 additions & 46 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id("org.jlleitschuh.gradle.ktlint") version "12.1.0"
}

android {
Expand Down Expand Up @@ -42,52 +43,31 @@ android {

dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.11.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.7.0")
implementation("androidx.activity:activity-compose:1.8.2")
implementation(platform("androidx.compose:compose-bom:2024.03.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
implementation("org.apache.commons:commons-lang3:3.14.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
androidTestImplementation(platform("androidx.compose:compose-bom:2024.03.00"))
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")

def composeBom = platform('androidx.compose:compose-bom:2024.03.00')
implementation composeBom
androidTestImplementation composeBom

// Choose one of the following:
// Material Design 3
implementation 'androidx.compose.material3:material3'
// or Material Design 2
implementation 'androidx.compose.material:material'
// or skip Material Design and build directly on top of foundational components
implementation 'androidx.compose.foundation:foundation'
// or only import the main APIs for the underlying toolkit systems,
// such as input and measurement/layout
implementation 'androidx.compose.ui:ui'

// Android Studio Preview support
implementation 'androidx.compose.ui:ui-tooling-preview'
debugImplementation 'androidx.compose.ui:ui-tooling'

// UI Tests
androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
debugImplementation 'androidx.compose.ui:ui-test-manifest'

// Optional - Included automatically by material, only add when you need
// the icons but not the material library (e.g. when using Material3 or a
// custom design system based on Foundation)
implementation 'androidx.compose.material:material-icons-core'
// Optional - Add full set of material icons
implementation 'androidx.compose.material:material-icons-extended'
// Optional - Add window size utils
implementation 'androidx.compose.material3:material3-window-size-class'
// For the API call
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

}

// Optional - Integration with activities
implementation 'androidx.activity:activity-compose:1.8.2'
// Optional - Integration with ViewModels
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1'
// Optional - Integration with LiveData
implementation 'androidx.compose.runtime:runtime-livedata'
// Optional - Integration with RxJava
implementation 'androidx.compose.runtime:runtime-rxjava2'
ktlint {
dependencies {
ktlintRuleset("com.twitter.compose.rules:ktlint:0.0.26")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dk.scheduling.schedulingfrontend
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*
Expand All @@ -15,7 +14,7 @@ import org.junit.Assert.*
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
@org.junit.jupiter.api.Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
Expand All @@ -10,6 +11,7 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.SchedulingFrontend"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name=".MainActivity"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package dk.scheduling.schedulingfrontend.API

import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.security.SecureRandom
import java.security.cert.X509Certificate
import javax.net.ssl.SSLContext
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager

object RetrofitClient {
/**
* The base URL of the API
* 10.0.2.2 is a special alias to your host loopback interface (127.0.0.1 on your development machine)
* 2222 is the port where the server is running
*/
private const val BASE_URL = "http://10.0.2.2:2222/"
private val gson = GsonBuilder().setLenient().create()
private val okHttpClient: OkHttpClient by lazy {
val trustAllCertificates = arrayOf<TrustManager>(object : X509TrustManager {
override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?) {}
override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?) {}
override fun getAcceptedIssuers(): Array<X509Certificate> = arrayOf()
})

val sslContext = SSLContext.getInstance("SSL")
sslContext.init(null, trustAllCertificates, SecureRandom())

val sslSocketFactory = sslContext.socketFactory

OkHttpClient.Builder()
.sslSocketFactory(sslSocketFactory, trustAllCertificates[0] as X509TrustManager)
.hostnameVerifier { _, _ -> true } // Allow all hostnames
.build()
}
val retrofit: Retrofit by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
}
}



object ApiClient {
val apiService: ApiService by lazy {
RetrofitClient.retrofit.create(ApiService::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dk.scheduling.schedulingfrontend.API
import retrofit2.Call
import retrofit2.http.GET

interface ApiService {
@GET("test")
fun test() : Call<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,38 @@ package dk.scheduling.schedulingfrontend

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

import android.util.Log
import android.util.Log.ERROR
import android.widget.Button
import dk.scheduling.schedulingfrontend.API.ApiClient
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
val call = ApiClient.apiService.test()

call.enqueue(object : Callback<String>{
override fun onResponse(call: Call<String>, response: Response<String>) {
if (response.isSuccessful) {
//val post = response.body()
println("we got a response")
// Handle the retrieved post data
} else {
println("we did not get a successful response")
// Handle error
}
}

override fun onFailure(call: Call<String>, t: Throwable) {
// Handle failure
Log.e("error message", "could not get a response")
}
})
}
}
}
12 changes: 12 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,16 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.51"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.26999998" />

</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dk.scheduling.schedulingfrontend

import org.junit.Test
import org.junit.jupiter.api.Test

import org.junit.Assert.*

Expand All @@ -10,7 +10,7 @@ import org.junit.Assert.*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
@org.junit.jupiter.api.Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
Expand Down
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ kotlin.code.style=official
android.nonTransitiveRClass=true
# TODO: Fix this supression
android.suppressUnsupportedCompileSdk=34
android.defaults.buildfeatures.buildconfig=true
android.nonFinalResIds=false

0 comments on commit 4098fc5

Please sign in to comment.