0

I created a simple Restful Api using Spring boot and Kotlin, and I'm trying to consume it an android app but can't seem to do so. It's working fine on postman,in an Angular project and in the emulators browser(Google Chrome, 'http://10.0.2.2:8080/api/accounts')

The problem is I'm not getting any data to be displayed in the TextView(tvTest).

The endpoint:

It's running on http://localhost:8080/

@RestController
@RequestMapping("/api")
class AccountController {

    @Autowired
    val accountService = AccountService()

    @GetMapping(value=["/accounts"])
    fun accounts(): ResponseEntity<List<Account>> {
        return accountService.accounts()
    }
}

on postman:

Postman GET

Android Manifest:

<uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:usesCleartextTraffic="false"

Api Service:

interface UserApiService {

    @GET("accounts")
    fun getAccounts(): Call<UserList>

}

Retrofit:

object RetrofitInstance {
    private const val BASE_URL = "http://10.0.2.2:8080/api/"

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

ViewModel:

class HomeViewModel():ViewModel() {

    private  var userLiveData = MutableLiveData<User>()

    fun getUser()
    {
        RetrofitInstance.api.getAccounts().enqueue(object : Callback<UserList?> {
            override fun onResponse(call: Call<UserList?>, response: Response<UserList?>) {
                if(response.body() != null)
                {
                    val user = response.body()!!.users[0]
                    userLiveData.value = user
                }else return
            }

            override fun onFailure(call: Call<UserList?>, t: Throwable) {
                print(t.message.toString())
            }
        })
    }

    fun observerUserLiveData(): LiveData<User>
    {
        return userLiveData
    }

Activity:

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private lateinit var homeMvvm: HomeViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        homeMvvm = ViewModelProvider(this)[HomeViewModel::class.java]

        homeMvvm.getUser()
        observeUser()

    }

    private fun observeUser()
    {
        homeMvvm.observerUserLiveData().observe(this)
        {
            t ->
            binding.tvTest.text = t!!.firstName
        }
    }
}

}

cds
  • 1
  • 1
  • What the error are you facing? – Pratik Satani Apr 19 '22 at 06:56
  • what happens ? what doesn't happen ? any errors to post ? have you tried debugging it ? are you sure the emulator has access to It's running on `http://localhost:8080/` ? – a_local_nobody Apr 19 '22 at 06:56
  • Hi, I just edited it. The problem is I'm not getting any data from the api. I want to display it first on a TextView, but no luck. – cds Apr 19 '22 at 06:58
  • Are you testing with an emulator or a real device? – Norris Boateng Apr 20 '22 at 08:32
  • I'm trying for both. I use 10.0.2.2 when using emulator and the PCs IP address when using a real device. – cds Apr 21 '22 at 11:21
  • You have to set `android:usesCleartextTraffic` to `true` in order to use `http` URLs, when you move to `https` base URL then you set the said property to `false` – rahat Aug 23 '22 at 10:29

0 Answers0