I am working on an android app which request Mars photos ans use it to display it on screen.
To make an request.And trying to use A public Api object that exposes the lazy-initialized Retrofit service.
below is source code with error
import retrofit2.Retrofit
import retrofit2.converter.scalars.ScalarsConverterFactory
import retrofit2.http.GET
class MarsApiService {
public val retrofit = Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
.baseUrl(Companion.BASE_URL)
.build()
interface MarsApiService{
#GET("photos")
fun getPhotos(): String
}
object MarsApi {
val retrofitService: MarsApiService by lazy { retrofit.create(MarsApiService::class.java) }
}
companion object {
private const val BASE_URL = "https://android-kotlin-fun-mars-server.appspot.com"
}
}
17th line the code inside object MarsApi pops up errors Unresolved reference : retrofit. The call to create() function on a Retrofit object is expensive and the app needs only one instance of Retrofit API service. So, i exposed the service to the rest of the app using object declaration.
What I have tried:
The code is working if i bring code inside object MarsApi out but doing so may result in multiple instance of retrofit.
code1
interface MarsApiService {
#GET("photos")
suspend fun getPhotos(): String
companion object {
private const val BASE_URL = "https://android-kotlin-fun-mars-server.appspot.com"
val marsApiService: MarsApiService by lazy {
Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
.baseUrl(BASE_URL)
.build().create(MarsApiService::class.java)
}
}
}
code2
private const val BASE_URL = "https://android-kotlin-fun-mars-server.appspot.com/"
private val retrofit = Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
.baseUrl(BASE_URL)
.build()
interface MarsApiService {
#GET("photos")
suspend fun getPhotos(): String
}
object MarsApi {
val marsApiService: MarsApiService by lazy { retrofit.create(MarsApiService::class.java) }
}
run
fun main() = runBlocking {
val rs = marsApiService.getPhotos()
println(rs)
}
In your code, retrofit by lazy reference not resolved cause of retrofit is property of class, move the retrofit globally like using (Companion) object or like code2 (highest hierarchy?)
Hope this link
make you clear the difference between object and companion object and when to use them. Happy learning
It was the same problem that I had.
I solved it just excluding the class MarsApiService
I didnĀ“t use companion object too.
The right code is below
import retrofit2.Retrofit
import retrofit2.converter.scalars.ScalarsConverterFactory
import retrofit2.http.GET
private const val BASE_URL = "https://android-kotlin-fun-mars-server.appspot.com"
public val retrofit = Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
.baseUrl(Companion.BASE_URL)
.build()
interface MarsApiService{
#GET("photos")
fun getPhotos(): String
}
object MarsApi {
val retrofitService: MarsApiService by lazy {
retrofit.create(MarsApiService::class.java)
}
}
Related
Google is deprecating Android AsyncTask API in Android 11
Google is deprecating Android AsyncTask API in Android 11 and suggesting to use java.util.concurrent instead
My question is that what should be proper replacement of the code snippet shown below using java.util.concurrent
package gaspriceinegypt.gaspriceegypt.gaspriceinegypt
import android.os.AsyncTask
import com.google.android.gms.maps.GoogleMap
import gaspriceinegypt.gaspriceegypt.gaspriceinegypt.DownloadUrl
import org.json.JSONObject
import org.json.JSONArray
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
import com.google.android.gms.maps.CameraUpdateFactory
import org.json.JSONException
import java.io.IOException
class FetchData : AsyncTask<Any?, String?, String?>() {
var googleNeatByPlacesData: String? = null
var googleMap: GoogleMap? = null
var url: String? = null
override fun onPostExecute(s: String?) {
try {
val jsonObject = JSONObject(s)
val jsonArray = jsonObject.getJSONArray("results")
for (i in 0 until jsonArray.length()) {
val jsonObject1 = jsonArray.getJSONObject(i)
val getLocation = jsonObject1.getJSONObject("geometry").getJSONObject("location")
val lat = getLocation.getString("lat")
val lng = getLocation.getString("lng")
val getName = jsonArray.getJSONObject(i)
val name = getName.getString("name")
val latLng = LatLng(lat.toDouble(), lng.toDouble())
val markerOptions = MarkerOptions()
markerOptions.title(name)
markerOptions.position(latLng)
googleMap!!.addMarker(markerOptions)
googleMap!!.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15f))
}
} catch (e: JSONException) {
e.printStackTrace()
}
}
override fun doInBackground(vararg objects: Any?): String? {
try {
googleMap = objects[0] as GoogleMap
url = objects[1] as String
val downloadUrl = DownloadUrl()
googleNeatByPlacesData = downloadUrl.retireveUrl(url)
} catch (e: IOException) {
e.printStackTrace()
}
return googleNeatByPlacesData
}
}
As you know Google is deprecating Android AsyncTask API in Android 11.
Alternatives in my mind are:
kotlin coroutines
use self Executor interface implemetion which is a Java standard lib interface
WorkManager which is a android lib
I'm trying to instanciate a Room database in my main activity in Android Studio, following codelabs and tutorials, but my app always crash. Here's a part of the code:
My database (AppDatabase.kt):
#Database(entities = [Device::class], version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
abstract fun deviceDao(): DeviceDao
companion object {
#Volatile
private var INSTANCE: AppDatabase? = null
fun getDatabase(context: Context): AppDatabase {
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
"item_database"
)
.fallbackToDestructiveMigration()
.build() // <---- The crash occurs here
INSTANCE = instance
return instance
}
}
}
}
And here's the activity from which I'm trying to instantiate it:
class NavigationActivity() : AppCompatActivity() {
private lateinit var binding: ActivityNavigationBinding
private val db by lazy { AppDatabase.getDatabase(this) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityNavigationBinding.inflate(layoutInflater)
setContentView(binding.root)
Log.d("instantiation", "$db") // <----- Called from here
val navView: BottomNavigationView = binding.navView
val navController = findNavController(R.id.nav_host_fragment_activity_navigation)
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_devices, R.id.navigation_logs, R.id.navigation_settings
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
}
Finally, here's the error message, which doesn't helps me much:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Package.getName()' on a null object reference
at androidx.room.Room.getGeneratedImplementation(Room.java:82)
at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:1486)
at AppDatabase$Companion.getDatabase(AppDatabase.kt:24)
I tried a lot of things, including ViewModel, Repository and more, but got the crash systematically, at the same point.
Here's also the part of my build.gradle file where I import Room, maybe I'm wrong in some version or whatever...
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-android'
id 'kotlin-kapt'
}
[...]
def roomVersion = "2.4.2"
implementation("androidx.room:room-runtime:$roomVersion")
kapt("androidx.room:room-compiler:$roomVersion")
implementation "androidx.room:room-ktx:$roomVersion"
Make sure package declaration on top of the class is declared, for example:
package com.macrosystems.clean.ui.core.view
import android.content.Context
import android.content.Intent
import android.graphics.Color
etc...
I have a app where I use youtube api and make a get request using retrofit, now I want to get a list of videos for a specific keyword, but for that I have to use a different get req everytime so how can I change the get request programatically
Code for calling API
private fun getVideosList() {
val videos = RetrofitInstance.youtubeapi.getYoutubeVideos()
videos.enqueue(object : Callback<YoutubeAPIData?> {
override fun onResponse(call: Call<YoutubeAPIData?>, response: Response<YoutubeAPIData?>) {
val videosList = response.body()?.items
if (videosList != null) {
for(video in videosList) {
Log.d("title", video.snippet.title)
}
}
}
override fun onFailure(call: Call<YoutubeAPIData?>, t: Throwable) {
Toast.makeText(applicationContext, "Unable to fetch results!", Toast.LENGTH_SHORT).show()
Log.d("APIError",t.toString())
}
})
}
Retrofit Instance
object RetrofitInstance {
const val BASE_URL = "https://www.googleapis.com/youtube/v3/"
private val retrofit by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
val youtubeapi: YoutubeListApi by lazy {
retrofit.create(YoutubeListApi::class.java)
}}
Code for API interface
interface YoutubeListApi {
#GET("search?part=snippet&q=eminem&key=*my_key*")
fun getYoutubeVideos(): Call<YoutubeAPIData>}
Now what I want is to change the #GET("search?part=snippet&q=eminem&key=my_key") in the api interface so that if the keyword is eminem it should be search?part=snippet&q=eminem&key=my_key
and if keyword is dog it should be search?part=snippet&q=dogkey=my_key
Why not use #Query from retrofit?
You could redefine your interface to:
interface YoutubeListApi {
#GET("search")
fun getYoutubeVideos(
#Query("part") part: String,
#Query("q") query: String,
#Query("key") key: String,
): Call<YoutubeAPIData>
}
and then you can call it like getYoutubeVideos("snippets", "eminem", "your key") or getYoutubeVideos("snippets", "dog", "your key").
I think you can even hardcode some values in the URL if you want, but honestly I think you can just use kotlin default values:
interface YoutubeListApi {
#GET("search")
fun getYoutubeVideos(
#Query("q") query: String,
#Query("part") part: String = "snippet",
#Query("key") key: String = "your key",
): Call<YoutubeAPIData>
}
and just pass the query getYoutubeVideos("eminem"). I haven't double checked this, but I think it could work.
I am new with MVVM in kotlin, I want to fetch some data using retrofit and show this in textview but I can't fetch this. In this app first time, I am using the android jetpack component. I tried a lot of times but I can't solve this error. My code in below
MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val repository = Repository()
val viewModelFactory = MainViewModelFactory(repository)
viewModel = ViewModelProvider(this, viewModelFactory).get(MainViewModel::class.java)
viewModel.getEmployeeData()
viewModel.repoResponse.observe(this, Observer { response ->
if (response.isSuccessful) {
Log.d("Response", response.body()?.employee_id.toString())
Log.d("Response", response.body()?.employee_name.toString())
Log.d("Response", response.body()?.employee_age.toString())
Log.d("Response", response.body()?.employee_salary.toString())
name.text = response.body()?.employee_name!!
} else {
Log.d("Response", response.errorBody().toString())
name.text = response.code().toString()
}
})
}
MainViewModelFactory.kt
class MainViewModelFactory(private val repository: Repository):ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return MainViewModel(repository) as T
}
MainViewModel.kt
class MainViewModel(private val repository: Repository):ViewModel() {
val repoResponse:MutableLiveData<Response<Employee>> = MutableLiveData()
fun getEmployeeData(){
viewModelScope.launch {
val response = repository.getEmployeeData()
repoResponse.value = response
}
}
ApiClient.kt
object ApiClient {
private val retrofit by lazy{
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
val api:Api by lazy {
retrofit.create(Api::class.java)
}
Api.kt
public interface Api {
#GET("api/v1/employee/1")
suspend fun getEmployeeData():Response<Employee>
}
Repository.kt
class Repository {
suspend fun getEmployeeData():Response<Employee>{
return ApiClient.api.getEmployeeData()
}
Employee.kt
data class Employee(
#SerializedName("id")
var employee_id: Int,
#SerializedName("employee_name")
var employee_name: String,
#SerializedName("employee_age")
var employee_age: Int,
#SerializedName("employee_salary")
var employee_salary: Int
)
Constants.kt
class Constants {
companion object{
const val BASE_URL = "http://dummy.restapiexample.com"
}
Error image
How can i solve this? please help me. Thank you
Retrofit only supports suspend keyword since 2.6.4, so using 2.3.0 won't work.
You should update to a newer version of Retrofit. The current latest version at the time of writing is 2.9.0.
I am trying to figure out if I can work with Kotlin and Spark,
and use the former's data classes instead of Scala's case classes.
I have the following data class:
data class Transaction(var context: String = "", var epoch: Long = -1L, var items: HashSet<String> = HashSet()) :
Serializable {
companion object {
#JvmStatic
private val serialVersionUID = 1L
}
}
And the relevant part of the main routine looks like this:
val transactionEncoder = Encoders.bean(Transaction::class.java)
val transactions = inputDataset
.groupByKey(KeyExtractor(), KeyExtractor.getKeyEncoder())
.mapGroups(TransactionCreator(), transactionEncoder)
.collectAsList()
transactions.forEach { println("collected Transaction=$it") }
With TransactionCreator defined as:
class TransactionCreator : MapGroupsFunction<Tuple2<String, Timestamp>, Row, Transaction> {
companion object {
#JvmStatic
private val serialVersionUID = 1L
}
override fun call(key: Tuple2<String, Timestamp>, values: MutableIterator<Row>): Transaction {
val seq = generateSequence { if (values.hasNext()) values.next().getString(2) else null }
val items = seq.toCollection(HashSet())
return Transaction(key._1, key._2.time, items).also { println("inside call Transaction=$it") }
}
}
However, I think I'm running into some sort of serialization problem,
because the set ends up empty after collection.
I see the following output:
inside call Transaction=Transaction(context=context1, epoch=1000, items=[c])
inside call Transaction=Transaction(context=context1, epoch=0, items=[a, b])
collected Transaction=Transaction(context=context1, epoch=0, items=[])
collected Transaction=Transaction(context=context1, epoch=1000, items=[])
I've tried a custom KryoRegistrator to see if it was a problem with Kotlin's HashSet:
class MyRegistrator : KryoRegistrator {
override fun registerClasses(kryo: Kryo) {
kryo.register(HashSet::class.java, JavaSerializer()) // kotlin's HashSet
}
}
But it doesn't seem to help.
Any other ideas?
Full code here.
It does seem to be a serialization issue.
The documentation of Encoders.bean states (Spark v2.4.0):
collection types: only array and java.util.List currently, map support is in progress
Porting the Transaction data class to Java and changing items to a java.util.List seems to help.