[Python] How to properly implement API calls in Android Fragment using Coroutines and Retrofit?

Discussão em 'Python' iniciado por Stack, Junho 29, 2025.

  1. Stack

    Stack Membro Participativo

    Im trying to make a app in android studio about calculating descriptors from a molecule with SMILES. I use Kotlin for my app and for HTTP-client retrofit. But for calculating descriptors, I use Python. The library I using for calculating descriptors is rdkit. Because I need to somehow show the results to TextViews, I tried to use Chaquopy. But Chaquopy is not compatible with rdkit. So I decided to use a API. In the app you enter a SMILES and it will then send to the API, the descriptors will be calculated and than will be send back to the app. The results will be displayed in TextViews.I have made the API with Flask and deployed in Render. But an error occured for some reason: Unresolved reference 'calculateprop_a'.:64 (and) Unresolved reference 'calculateprop_b'.:67

    //fragment file

    val response1 = Utils.api.calculateprop_a(SmileRequest(smiles))
    val response2 = Utils.api.calculateprop_b(SmileRequest(smiles))


    It seems that Android Studio does not recognize the functions, although I have imported them. They are also present in the interface file.

    //fragment file

    package com.example.myapplication

    import android.os.Bundle
    import androidx.fragment.app.Fragment
    import android.view.LayoutInflater
    import android.view.View
    import android.view.ViewGroup
    import android.widget.Button
    import android.widget.EditText
    import android.widget.ProgressBar
    import android.widget.TextView
    import android.widget.Toast
    import com.chaquo.python.Python
    import com.example.myapplication.models.Response1
    import com.google.firebase.appdistribution.gradle.ApiService
    import com.example.myapplication.models.SmileRequest
    import com.example.myapplication.utils.Utils
    import kotlinx.coroutines.CoroutineScope
    import kotlinx.coroutines.Dispatchers
    import kotlinx.coroutines.launch
    import kotlinx.coroutines.withContext
    import retrofit2.Response


    //interface file (ApiService.kt)

    package com.example.myapplication

    import com.example.myapplication.models.Response1
    import com.example.myapplication.models.Response2
    import com.example.myapplication.models.SmileRequest
    import retrofit2.Response
    import retrofit2.http.Body
    import retrofit2.http.POST

    interface ApiService {
    @POST("/prop1")
    suspend fun calculateprop_a(@Body request: SmileRequest): Response<Response1>
    @POST("/prop2")
    suspend fun calculateprop_b(@Body request: SmileRequest): Response<Response2>
    }


    In dataclass file do I also not find any mistakes.

    //dataclsaa file (SmileRequest.kt)

    package com.example.myapplication.models

    //[molw, logp, rotb, tpsa, numhdon, numhacp]
    data class SmileRequest(val smiles: String)
    data class Response1(
    val status: String?,
    val smiles: String?,
    val molwt: Double?,
    val logp: Double?,
    val rotb: Int?,
    val tpsa: Double?,
    val numhdon: Int?,
    val numhacp: Int?,
    val error: String?
    )

    //[wie_index, bal_index, ran_index, zgr_m1, zgr_m2, tp, surf_area]
    data class Response2(
    val smiles: String?,
    val wie_index: Double?,
    val bal_index: Double?,
    val ran_index: Double?,
    val zgr_m1: Double?,
    val zgr_m2: Double?,
    val tp: Double?,
    val surf_area: Double?,
    val error: String?
    )


    Likewise in object file.

    //object file (Utils.kt)

    package com.example.myapplication.utils

    import com.google.firebase.appdistribution.gradle.ApiService
    import retrofit2.Retrofit
    import retrofit2.converter.gson.GsonConverterFactory

    object Utils {
    private const val BASE_URL = "secret"

    val api: ApiService by lazy {
    Retrofit.Builder()
    .baseUrl(BASE_URL)
    .addConverterFactory(GsonConverterFactory.create())
    .build()
    .create(ApiService::class.java)
    }
    }


    Don´t really know what to do. I´m new in API field. So, I hope for help.

    Continue reading...

Compartilhe esta Página