skip to Main Content

I wanted to get data from this Website: http://ries.world:32170/rest/getDateTime .There are 4 informations Local, utc, message and code. I tried to get them and then show them in the emulator but somehow it doesn’t show the informations and I really don’t know why. Also the program shows no errors at all.

That’s the MainActivity.kt

package com.example.test48

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class MainActivity : AppCompatActivity() {

    private lateinit var local: TextView
    private lateinit var utc: TextView
    private lateinit var message: TextView
    private lateinit var code: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        local = findViewById(R.id.local)
        utc = findViewById(R.id.utc)
        message = findViewById(R.id.message)
        code = findViewById(R.id.code)

        val retrofit = Retrofit.Builder()
            .baseUrl("http://ries.world:32170/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()

        val apiService = retrofit.create(ApiService::class.java)

        val call: Call<ApiResponse> = apiService.getData()

        call.enqueue(object : Callback<ApiResponse> {
            override fun onResponse(call: Call<ApiResponse>, response: Response<ApiResponse>) {
                if (response.isSuccessful) {
                    val apiResponse: ApiResponse? = response.body()

                    local.text = getString(R.string.local_placeholder, apiResponse?.local.orEmpty())
                    utc.text = getString(R.string.utc_placeholder, apiResponse?.local.orEmpty())
                    message.text = getString(R.string.message_placeholder, apiResponse?.local.orEmpty())
                    code.text = getString(R.string.code_placeholder, apiResponse?.local.orEmpty())

                }
            }

            override fun onFailure(call: Call<ApiResponse>, t: Throwable) {
                t.printStackTrace()
            }
        })
    }
}

That’s the ApiService

package com.example.test48

import retrofit2.Call
import retrofit2.http.GET

interface ApiService {
    @GET("rest/getDateTime")
    fun getData(): Call<ApiResponse>
}

That’s the ApiResponse

package com.example.test48

data class ApiResponse(
    val local: String?,
    val utc: String?,
    val message: String?,
    val code: String?
)

That’s the activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/local"
        android:layout_width="407dp"
        android:layout_height="75dp"
        android:layout_marginStart="131dp"
        android:layout_marginTop="77dp"
        android:layout_marginEnd="131dp"
        android:text="@string/local_label"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/utc"
        android:layout_width="412dp"
        android:layout_height="67dp"
        android:layout_marginStart="131dp"
        android:layout_marginTop="200dp"
        android:layout_marginEnd="131dp"
        android:text="@string/utc_label"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/message"
        android:layout_width="408dp"
        android:layout_height="66dp"
        android:layout_marginStart="131dp"
        android:layout_marginTop="330dp"
        android:layout_marginEnd="131dp"
        android:text="@string/message_label"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/code"
        android:layout_width="405dp"
        android:layout_height="56dp"
        android:layout_marginStart="131dp"
        android:layout_marginTop="460dp"
        android:layout_marginEnd="131dp"
        android:text="@string/code_label"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

That’s the build.graddle.kts

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
}

android {
    namespace = "com.example.test48"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.example.test48"
        minSdk = 28
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

dependencies {

    implementation("androidx.core:core-ktx:1.12.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("com.squareup.retrofit2:retrofit:2.9.0")
    implementation("com.squareup.retrofit2:converter-gson:2.9.0")
}

That’s in the AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />

And last, here is the Strings.xml

<resources>
    <string name="app_name">Test48</string>
    <string name="local_placeholder">Local: %1$s</string>
    <string name="utc_placeholder">Utc: %1$s</string>
    <string name="message_placeholder">Message: %1$s</string>
    <string name="code_placeholder">Code: %1$s</string>
    <string name="local_label">Local: </string>
    <string name="utc_label">Utc: </string>
    <string name="message_label">Message: </string>
    <string name="code_label">Code: </string>
</resources>

I tried to get the data from the mentioned website and then show it on the Android Studios Emulator. However it didn’t go as planned as it should go, I wasn’t able to see the data and I have absolutely no idea why.

2

Answers


  1. Try to set android:usesCleartextTraffic in Manifest true and/or NetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted(), it should returns false

    Login or Signup to reply.
  2. You are using apiResponse?.local.orEmpty()) for all the fields, Which means you are using local field for all Textview. you should use the respective fields for each TextView.

    override fun onResponse(call: Call<ApiResponse>, response: Response<ApiResponse>) {
                    if (response.isSuccessful) {
                        val apiResponse: ApiResponse? = response.body()
    
                        local.text = getString(R.string.local_placeholder, apiResponse?.local.orEmpty())
                        utc.text = getString(R.string.utc_placeholder, apiResponse?.utc.orEmpty())
                        message.text = getString(R.string.message_placeholder, apiResponse?.message.orEmpty())
                        code.text = getString(R.string.code_placeholder, apiResponse?.code.orEmpty())
    
                    }
                }
    

    and update ApiService to include the full URL and Make sure your ‘APIResponse’ matches the structure of the JSON response, check logcat for any errors

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search