summaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/example/alcagotchi/DrinkingActivity.kt
diff options
context:
space:
mode:
authorLeonardo Bishop <me@leonardobishop.com>2024-11-17 00:11:08 +0000
committerLeonardo Bishop <me@leonardobishop.com>2024-11-17 00:11:17 +0000
commit25a3947a1dc4233b43b2e4e8e79b127fa7dc57f7 (patch)
treec19258e9d8d2d4edbb604ee18ca0895c049aae10 /app/src/main/java/com/example/alcagotchi/DrinkingActivity.kt
parenta0fd80340f93b04eba585254dc2a718770a30e92 (diff)
genting
Diffstat (limited to 'app/src/main/java/com/example/alcagotchi/DrinkingActivity.kt')
-rw-r--r--app/src/main/java/com/example/alcagotchi/DrinkingActivity.kt95
1 files changed, 0 insertions, 95 deletions
diff --git a/app/src/main/java/com/example/alcagotchi/DrinkingActivity.kt b/app/src/main/java/com/example/alcagotchi/DrinkingActivity.kt
index 4c5e3aa..1d02fea 100644
--- a/app/src/main/java/com/example/alcagotchi/DrinkingActivity.kt
+++ b/app/src/main/java/com/example/alcagotchi/DrinkingActivity.kt
@@ -45,18 +45,12 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.alcagotchi.ui.theme.AlcaGotchiTheme
-import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.HttpRequest
-import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.client.HttpClient
-import com.google.gson.Gson
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
-import okhttp3.OkHttpClient
import java.io.BufferedReader
-import java.io.IOException
import java.io.InputStreamReader
import java.net.HttpURLConnection
-import java.net.URI
import java.net.URL
//import java.net.HttpURLConnection
//import java.net.URL
@@ -67,97 +61,8 @@ import java.net.URL
//var client: OkHttpClient? = OkHttpClient()
fun PostHttpRequest(url_name: String = "http://httpbin.org/post", json_string: String = """{"name":"John Doe", "age": 30}"""){
- val url = URL(url_name)
- val json = json_string
- val connection = url.openConnection() as HttpURLConnection
- connection.apply {
- requestMethod = "POST"
- doOutput = true
- setRequestProperty("Content-Type", "application/json")
- }
-
- val outputStream = connection.outputStream
- outputStream.write(json.toByteArray())
- outputStream.flush()
- outputStream.close()
-
- val response = connection.inputStream.use { it.reader().use { reader -> reader.readText() } }
- println(response)
- connection.disconnect()
}
-fun asyncGetHttpRequest(
- endpoint: String,
- onSuccess: (ApiResponse<AmiiboResponse>) -> Unit,
- onError: (Exception) -> Unit
-) {
- CoroutineScope(Dispatchers.IO).launch {
- val url = URL(endpoint)
- val openedConnection = url.openConnection() as HttpURLConnection
- openedConnection.requestMethod = "GET"
-
- val responseCode = openedConnection.responseCode
- try {
- val reader = BufferedReader(InputStreamReader(openedConnection.inputStream))
- val response = reader.readText()
- val apiResponse = ApiResponse(
- responseCode,
- parseJson<AmiiboResponse>(response)
- )
- print(response)
- reader.close()
- // Call the success callback on the main thread
- launch(Dispatchers.Main) {
- onSuccess(apiResponse)
- }
- } catch (e: Exception) {
- Log.d("Error", e.message.toString())
- // Handle error cases and call the error callback on the main thread
- launch(Dispatchers.Main) {
- onError(Exception("HTTP Request failed with response code $responseCode"))
- }
- } finally {
-
- }
- }
-}
-
-
-//fun sendPost(urlString: String = "http://127.0.0.1:8080/test",
-// bodyContent: String = "test") {
-//
-// val client = HttpClient.newBuilder().build();
-// val request = HttpRequest.newBuilder()
-// .uri(URI.create(urlString))
-// //.header("Content-Type", "application/json")
-// .POST(HttpRequest.BodyPublishers.ofString(bodyContent))
-// .build()
-// val response = client.send(request, HttpResponse.BodyHandlers.ofString())
-// val answer = response.body()
-// println(answer)
-//}
-
-private inline fun <reified T>parseJson(text: String): T =
- Gson().fromJson(text, T::class.java)
-
-data class ApiResponse<T>(
- val responseCode: Int,
- val response: T
-)
-
-data class AmiiboResponse(
- val amiibo: List<AmiiboItem>
-)
-data class AmiiboItem(
- val amiiboSeries: String,
- val character: String,
- val gameSeries: String,
- val head: String,
- val image: String,
- val name: String,
- val tail: String,
- val type: String
-)
class DrinkingActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)