I have this test class:
class InspirationalQuoteInstrumentedTest {
private lateinit var server: MockWebServer
#Rule
#JvmField
val mActivityRule: ActivityTestRule<InspirationalQuoteActivity> = ActivityTestRule(InspirationalQuoteActivity::class.java)
#Before
fun setUp() {
server = MockWebServer()
server.start()
Constants.BASE_URL = server.url("/").toString()
}
#After
fun tearDown() {
server.shutdown()
}
#Test
fun ensureTheQuoteOfTheDayIsDisplayed() {
println("Base URL: ${Constants.BASE_URL}")
Log.e(TAG,"Base URL: ${Constants.BASE_URL}")
val response200 = this::class.java.classLoader.getResource("200.json").readText()
val jsonResponse = JSONObject(response200)
val expectedQuote = jsonResponse
.getJSONObject("contents")
.getJSONArray("quotes")
.getJSONObject(0)
.getString("quote")
server.enqueue(MockResponse()
.setResponseCode(200)
.setBody(response200))
val intent = Intent()
mActivityRule.launchActivity(intent)
onView(withId(R.id.inspirationalQuote))
.check(matches(withText(expectedQuote)))
}
companion object {
val TAG = InspirationalQuoteInstrumentedTest::class.java.simpleName
}
}
And I have this activity:
class InspirationalQuoteActivity : AppCompatActivity() {
private lateinit var quoteService: QuoteOfTheDayService
private var quote: String = ""
private var author: String = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_inspirational_quote)
val textView = findViewById<TextView>(R.id.inspirationalQuote) as TextView
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
textView.text = getQuoteOfTheDay()
}
private fun getQuoteOfTheDay(): String {
quoteService = QuoteOfTheDayService()
val qod = quoteService.getQuoteOfTheDay()
val response = qod.execute()
Log.e(TAG, "Response: $response")
response?.let {
quote = response.body()!!.contents.quotes[0].quote
author = response.body()!!.contents.quotes[0].author
}
Log.e(TAG, "Expected Quote: $quote")
return quote
}
companion object {
private val TAG = InspirationalQuoteActivity::class.java.simpleName
}
}
When I run my test getQuoteOfTheDay() gets executed twice. What gives? The issue is that I'm trying to mock out an api call which looks like it's working ask expected, however there is another log that I'm not sure where it's coming from. For reference, here is the out put in logcat
Response: Response{protocol=http/1.1, code=200, message=OK, url=https://quotes.rest/qod}
Expected Quote: Let us think the unthinkable, let us do the undoable, let us prepare to grapple with the ineffable itself, and see if we may not eff it after all.
Response: Response{protocol=http/1.1, code=200, message=OK, url=http://localhost:37290/qod}
Expected Quote: Winning is nice if you don't lose your integrity in the process.
As you can see, I hit https://quotes.rest/qod once, and then I hit my mock server after that.
I missed some arguments in the constructor... Doh.
Changing
ActivityTestRule(InspirationalQuoteActivity::class.java)
to
ActivityTestRule(InspirationalQuoteActivity::class.java, false, false)
did the trick.
You are launching your activity with the intentTestRule
IntentsTestRule<>(InspirationalQuoteActivity.class, false, true);
The third parameter is launchActivity, you must set it as false
Related
I'm creating an app where the user has to insert a serverurl in an EditText field, and that url should be the baseUrl of the retrofit-request.
So, my code works as it should when i use a hardcoded baseurl, but the app crashes when I try to pass the value from the Edittext to the baseUrl.
Thats how I tried to pass the value:
object NetworkLayer {
var newUrl: String = ""
val retrofit: Retrofit
get() = Retrofit.Builder()
.baseUrl(newUrl)
.addConverterFactory(GsonConverterFactory.create())
.build()
val myApi: MyApi by lazy {
retrofit.create(MyApi::class.java)
}
val apiClient = ApiClient(myApi)
}
and in my MainActivity:
var serverUrl = binding.et1.text.toString()
button.setOnClickListener {
NetworkLayer.newUrl = serverUrl
viewModel.getServerInformation(headerValue)
}
I get this error message: Error message: Caused by: java.lang.IllegalArgumentException: Expected URL scheme 'http' or 'https' but no scheme was found for.
So probably retrofit uses the empty "" string for the request. Somehow I should send the information to retrofit that when clicking the button the url from the Edittext (et1) is the baseUrl. When I use a seperate class (f.e. class Constants, with a companion object with a const val baseUrl = "hardcoded url") it works also.
Can I create a function to inform the retrofit client to use the Edittext as baseUrl and declare it in the onClickListener? or could it be a way to create the retrofit client in a class instead of an object? (using url: String as parameter in the class and adding the edittext as argument in the MainActivity?)
Sadly the #Url annotation for Retrofit doesn't work as I have to use also #Header and #Query in the different requests.
Or is there a compeletey different way for doing this?
Hopefully there is someone who can help me.
I managed to solve it, the only thing I had to change was:
val url = binding.etServerUrl.text instead of
val url = binding.etServerUrl.text.toString()
and when calling the function on button click I added the toString() to the url argument. When I try to add the toString() to the val url as I always did before it doesn't work, anyone can tell me why?
Here is an example how I use it (I changed the Retrofit client a bit to my first version in the question). So finally I can go ahead with my app, as I was blocked now for a few weeks with this.. :-)
object RetrofitClient{
var retrofitService: MyApi? = null
fun getInstance(url: String): MyApi{
if (retrofitService == null) {
val retrofit = Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build()
retrofitService = retrofit.create(MyApi::class.java)
}
return retrofitService!!
}
}
I changed the retrofitclient a bit, but it wors
Then in the repository:
class MainRepository (){
suspend fun getToken(cookie: String, url: String): TokenResponse? {
val request = RetrofitClient.getInstance(url).getToken(cookie)
if (request?.isSuccessful!!) {
return request.body()!!
}
return null
}
}
Viewmodel:
class SharedViewModel() : ViewModel() {
private val repository = MainRepository()
private val _getTokenLiveData = MutableLiveData<TokenResponse>()
val getTokenLiveData: LiveData<TokenResponse> = _getTokenLiveData
fun getToken(cookie: String, url: String) {
viewModelScope.launch {
val response = repository.getToken(cookie, url)
_getTokenLiveData.postValue(response)
}
}
}
And finally the MainActivity:
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
val viewModel: SharedViewModel by lazy {
ViewModelProvider(this)[SharedViewModel::class.java]
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater) //initializing the binding class
setContentView(binding.root)
val url = binding.etServerUrl.text
val headerValue = binding.etMac.text.toString()
val button = binding.button
val textView = binding.textView
button.setOnClickListener {
viewModel.getToken(headerValue, url = url.toString())
}
viewModel.getTokenLiveData.observe(this) { response ->
if (response == null) {
Toast.makeText(this#MainActivity, "Fehlerhaft", Toast.LENGTH_SHORT).show()
return#observe
}
textView.text = response.js.token
}
}
}
I want to pass header authorization token in HTTP requests in retrofit. The token is saved in DataStore. My problem is how to retrieve the token from DataStore and pass it to the intercepter header. I've spent hours thinking of a workaround and searching through the internet but I came with nothing. I'm also new to Kotlin. Here is the code snippet:
interface RoomAPIService {
#GET("rooms")
fun getAllRooms(#Header("Authorization") authHeader: String): Call<List<Room>>
var context: Context
companion object {
var retrofitService: RoomAPIService? = null
var token: String = ""
fun getInstance() : RoomAPIService {
GlobalScope.launch(Dispatchers.IO)
{
//How to pass context to DataRepository.getInstance(context)
token = DataStoreRepository.getInstance().getToken().toString()
}
val httpClient = OkHttpClient.Builder()
httpClient.addInterceptor { chain ->
val request = chain.request().newBuilder().addHeader("Authorization","Bearer " + theTokenRetrievedFromDataStore).build()
chain.proceed(request)
}
.
.
}
.
.
}
Here is DataStoreRepository.kt:
class DataStoreRepository(context: Context) {
private val dataStore: DataStore<Preferences> = context.createDataStore(
name = "token_store"
)
companion object {
private val TOKEN = preferencesKey<String>("TOKEN")
private var instance: DataStoreRepository? = null
fun getInstance(context: Context): DataStoreRepository {
return instance ?: synchronized(this) {
instance ?: DataStoreRepository(context).also { instance = it }
}
}
}
suspend fun savetoDataStore(token: String) {
dataStore.edit {
it[TOKEN] = token
}
}
suspend fun getToken(): String? {
val preferences: Preferences = dataStore.data.first()
Log.d("datastore", "token retrieved: ${preferences[TOKEN]} +++++++++++")
return preferences[TOKEN]
}
}
And here is MainActivity.kt:
class MainActivity : AppCompatActivity() {
private lateinit var logoutBtn: Button
private lateinit var bottomNavigation: BottomNavigationView
private lateinit var binding: ActivityMainBinding
lateinit var viewModel: RoomViewModel
private val retrofitService = RoomAPIService.getInstance(this)
val adapter = RoomsAdapter()
private var token: String = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
logoutBtn = binding.logoutBtn
bottomNavigation = binding.bottomNavigation
viewModel = ViewModelProvider(this, RoomViewModeFactory(RoomRepository(retrofitService))).get(RoomViewModel::class.java)
binding.recyclerview.adapter = adapter
var token = intent.getStringExtra("token")
Log.d("tokenCheck","checkToken: $token")
if (token != null) {
viewModel.getAllRooms(token)
}
..
Any help will be greatly appreciated!
You can change your getInstance() signature to contain Context object.
fun getInstance(context:Context) : RoomAPIService {
GlobalScope.launch(Dispatchers.IO)
{
token = DataRepository.getInstance(context).getToken().toString()
}
}
This is not a good practise to use "context" in a non-ui module in your application. Networking modules ( or Data layer you may say) should not know about android libraries and components. My suggestion is to use a value in an "variable" which can be changed in runtime and can be seen in the Network module then use it in the OkHttp interceptor. Finally, you just need to initialize the variable at the beginning of your application.
I want to use coroutines in my project only when I use coroutines I get the error :Unable to invoke no-args constructor. I don't know why it's given this error. I am also new to coroutines.
here is my apiclient class:
class ApiClient {
val retro = Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
Here is my endpoint class:
#GET("v2/venues/search")
suspend fun get(
#Query("near") city: String,
#Query("limit") limit: String = Constants.limit,
#Query("radius") radius: String = Constants.radius,
#Query("client_id") id: String = Constants.clientId,
#Query("client_secret") secret: String = Constants.clientSecret,
#Query("v") date: String
): Call<VenuesMainResponse>
my Repository class:
class VenuesRepository() {
private val _data: MutableLiveData<VenuesMainResponse?> = MutableLiveData(null)
val data: LiveData<VenuesMainResponse?> get() = _data
suspend fun fetch(city: String, date: String) {
val retrofit = ApiClient()
val api = retrofit.retro.create(VenuesEndpoint::class.java)
api.get(
city = city,
date = date
).enqueue(object : Callback<VenuesMainResponse>{
override fun onResponse(call: Call<VenuesMainResponse>, response: Response<VenuesMainResponse>) {
val res = response.body()
if (response.code() == 200 && res != null) {
_data.value = res
} else {
_data.value = null
}
}
override fun onFailure(call: Call<VenuesMainResponse>, t: Throwable) {
_data.value = null
}
})
}
}
my ViewModel class:
class VenueViewModel( ) : ViewModel() {
private val repository = VenuesRepository()
fun getData(city: String, date: String): LiveData<VenuesMainResponse?> {
viewModelScope.launch {
try {
repository.fetch(city, date)
} catch (e: Exception) {
Log.d("Hallo", "Exception: " + e.message)
}
}
return repository.data
}
}
part of activity class:
class MainActivity : AppCompatActivity(){
private lateinit var venuesViewModel: VenueViewModel
private lateinit var adapter: HomeAdapter
private var searchData: List<Venue>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val editText = findViewById<EditText>(R.id.main_search)
venuesViewModel = ViewModelProvider(this)[VenueViewModel::class.java]
venuesViewModel.getData(
city = "",
date = ""
).observe(this, Observer {
it?.let { res ->
initAdapter()
rv_home.visibility = View.VISIBLE
adapter.setData(it.response.venues)
searchData = it.response.venues
println(it.response.venues)
}
})
this is my VenuesMainResponse data class
data class VenuesMainResponse(
val response: VenuesResponse
)
I think the no-args constructor warning should be related to your VenuesMainResponse, is it a data class? You should add the code for it as well and the complete Log details
Also, with Coroutines you should the change return value of the get() from Call<VenuesMainResponse> to VenuesMainResponse. You can then use a try-catch block to get the value instead of using enqueue on the Call.
Check this answer for knowing about it and feel free to ask if this doesn't solve the issue yet :)
UPDATE
Ok so I just noticed that it seems that you are trying to use the foursquare API. I recently helped out someone on StackOverFlow with the foursquare API so I kinda recognize those Query parameters and the Venue response in the code you provided above.
I guided the person on how to fetch the Venues from the Response using the MVVM architecture as well. You can find the complete code for getting the response after the UPDATE block in the answer here.
This answer by me has code with detailed explanation for ViewModel, Repository, MainActivity, and all the Model classes that you will need for fetching Venues from the foursquare API.
Let me know if you are unable to understand it, I'll help you out! :)
RE: UPDATE
So here is the change that will allow you to use this code with Coroutines as well.
Repository.kt
class Repository {
private val _data: MutableLiveData<mainResponse?> = MutableLiveData(null)
val data: LiveData<mainResponse?> get() = _data
suspend fun fetch(longlat: String, date: String) {
val retrofit = Retro()
val api = retrofit.retro.create(api::class.java)
try {
val response = api.get(
longLat = longlat,
date = date
)
_data.value = response
} catch (e: Exception) {
_data.value = null
}
}
}
ViewModel.kt
class ViewModel : ViewModel() {
private val repository = Repository()
val data: LiveData<mainResponse?> = repository.data
fun getData(longLat: String, date: String) {
viewModelScope.launch {
repository.fetch(longLat, date)
}
}
}
api.kt
interface api {
#GET("v2/venues/search")
suspend fun get(
#Query("ll") longLat: String,
#Query("client_id") id: String = Const.clientId,
#Query("client_secret") secret: String = Const.clientSecret,
#Query("v") date: String
): mainResponse
}
MainActivity.kt
private val viewModel by viewModels<ViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel.getData(
longLat = "40.7,-74",
date = "20210718" // date format is: YYYYMMDD
)
viewModel.data
.observe(this, Observer {
it?.let { res ->
res.response.venues.forEach { venue ->
val name = venue.name
Log.d("name ",name)
}
}
})
}
}
I am using Kotlin corountines in my Android Project. I am trying to download some data and display in a textview.
Following is my code
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tv.setOnClickListener {
downloadData()
}
}
private fun downloadData() {
runBlocking {
pb_activity_main.visibility = View.VISIBLE
var data = ""
async {
data = downloadDataBlocking()
}.await()
tv.text = data
pb_activity_main.visibility = View.GONE
}
}
private fun downloadDataBlocking(): String {
val client = OkHttpClient()
val request = Request.Builder().url("https://jsonplaceholder.typicode.com/posts").build()
val response = client.newCall(request).execute()
return response.body()?.string() ?: ""
}
}
But the data is not downloaded. I am not able to figure out why.
I have included the internet permission in Manifest and the url is also working.
Try this:
class MainActivity : AppCompatActivity(), CoroutineScope {
private val job = Job()
override val coroutineContext = Dispatchers.Main + job
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tv.setOnClickListener {
downloadData()
}
}
private fun downloadData() {
launch {
pb_activity_main.visibility = View.VISIBLE
tv.text = withContext(Dispatchers.IO) { downloadDataBlocking() }
pb_activity_main.visibility = View.GONE
}
}
private fun downloadDataBlocking(): String {
val client = OkHttpClient()
val request = Request.Builder().url("https://jsonplaceholder.typicode.com/posts").build()
val response = client.newCall(request).execute()
return response.body()?.string() ?: ""
}
}
First: you should never use runBLocking out of unit-testing or other special domain.
This function should not be used from coroutine. It is designed to bridge regular blocking code to libraries that are written in suspending style, to be used in main functions and in tests.
Second:
Coroutines are always related to some local scope in your application, which is an entity with a limited life-time, like a UI element.
That's why Activity implements CoroutineScope. Honestly, a better place for it is ViewModel or Presenter, but I don't see any in the code...
Third, it is quite pointless to useasync and await right after it's definition. Just use withContext then.
I am teaching myself Kotlin and android dev. So, i'm sure most of my issue is lack of knowledge, but i've been hung up on this part for day or two. I think my issue is partly my JSON query, and mostly my rookieness.
In my for loop below, I'm getting the following error from the IDE "For-Loop range must have an 'iterator()' method". This is in regards to 'cycloneList' in: for(stormInfo in cycloneList)
I've linked my "dummy" JSON data I'm using can be found here: https://api.myjson.com/bins/19uurt to save a bit of space here in the question.
Problem code
`var cycloneList = response?.body()?.currenthurricane?.stormInfo?.get(0)
if (cycloneList != null) {
for (stormInfo in cycloneList) { <<--Problem
val newCyclone = "Name: ${cycloneList.stormName}"
cycloneStrings.add(newCyclone)
}
}`
FULL CODE
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//creates a new CycloneRetriever object from CycloneHelp.kt
var retriever = CycloneRetriever()
val callback = object : Callback<Cyclones> {
override fun onResponse(call: Call<Cyclones>?, response: Response<Cyclones>?) {
println("Got a response!")
var cycloneList = response?.body()?.currenthurricane?.stormInfo?.get(0)
var cycloneStrings = mutableListOf<String>()
if (cycloneList != null) {
for (stormInfo in cycloneList) { //TODO Figure this out!!
val newCyclone = "Name: ${cycloneList.stormName}"
cycloneStrings.add(newCyclone)
}
}
var layoutMovieListView = movieListView
var movieListAdapter = ArrayAdapter(this#MainActivity, android.R.layout.simple_list_item_1,
cycloneStrings)
layoutMovieListView.adapter = movieListAdapter
}
override fun onFailure(call: Call<Cyclones>?, t: Throwable?) {
println("The thing, it failed!")
}
}
retriever.getCyclones(callback)
}
I'm using Retrofit to access/handle JSON data
Retrofit JSON code
interface WeatherWunderGroundAPI {
#GET("bins/19uurt")
fun getCyclones() : Call<Cyclones>
}
class Cyclones(val currenthurricane: CurrentHurricane)
class CurrentHurricane(val stormInfo: List<StormInfo>)
class StormInfo(val stormName: String)
class CycloneRetriever {
val service : WeatherWunderGroundAPI
init {
val = retrofitCyclone
Retrofit.Builder().baseUrl("https://api.myjson.com/")
.addConverterFactory(GsonConverterFactory.create()).build()
service = retrofitCyclone.create(WeatherWunderGroundAPI::class.java)
}
fun getCyclones(callback: Callback<Cyclones>) {
val call = service.getCyclones()
call.enqueue(callback)
}
}
Your stormInfo is a List which provides the mandatory iterator() and this is what you want to iterate over:
var cycloneList = response?.body()?.currenthurricane?.stormInfo ?: emptyList()
for (stormInfo in cycloneList) { }
Hope it helps...