DEV Community

Cover image for Kotlin and retrofit network calls
Odhiambo Paul
Odhiambo Paul

Posted on • Updated on

Retrofit Kotlin Kotlin and retrofit network calls

Liquid syntax error: 'raw' tag was never closed

Top comments (10)

Collapse
 
davidmartinezfl profile image
David Martinez

In this post it was missing adding the internet permission in the AndroidManifest.xml

"android.permission.INTERNET"

It helped me a lot, thanks!

Collapse
 
turicahoria profile image
TuricaHoria

I have a problem , I cannot get onto onResponse , I only get onFailure. This is my code:

object RetrofitClientInstance {

private val BASE_URL = "https://wger.de/api/v2/"

private val httpClient = OkHttpClient.Builder()

private val retrofit = Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .client(httpClient.build())
            .build()

fun<T> buildService (service : Class <T>) : T {
    return  retrofit.create(service)
}
Enter fullscreen mode Exit fullscreen mode

}

interface APIServices {

@GET("exercise")
fun getAllExercises(): Call<MutableList<Exercise>>
Enter fullscreen mode Exit fullscreen mode

}

class ExerciseFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment_exercises, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

    val request = RetrofitClientInstance.buildService(APIServices::class.java)

    val call = request.getAllExercises()
    call.enqueue(object : Callback<MutableList<Exercise>>
        {
            override fun onFailure(call: Call<MutableList<Exercise>>, t: Throwable) {
                Toast.makeText(context,"Couldn't fetch the exercises" , Toast.LENGTH_SHORT).show()
            }

            override fun onResponse(call: Call<MutableList<Exercise>>, response: Response<MutableList<Exercise>>) {

                if (response.isSuccessful)
                {
                    rv_exercices.apply {
                        setHasFixedSize(true)
                        layoutManager = LinearLayoutManager(context)
                        adapter = ExercisesAdapter(response.body()!!.toMutableList())
                    }
                }
            }
        }
    )
}
Enter fullscreen mode Exit fullscreen mode

}

Collapse
 
paulodhiambo profile image
Odhiambo Paul

in your code response.body()!! Should return a MutableList
Just pass response.body()!! to your recyclerview adapter instead of response.body()!!.toMutableList()

Collapse
 
petestmart profile image
Pete St. Martin • Edited

Is anyone else having an issue getting the images (the movie posters) to load?

I would post my code, but I'm not sure where this is even coming from. I'm combing over it and trying different things in different places.

Collapse
 
petestmart profile image
Pete St. Martin • Edited

Figured it out!

In the RecyclerView adapter

Glide.with(itemView.context).load("http://image.tmdb.org/t/p/w500${movie.poster_path}").into(photo)

Should be:

Glide.with(itemView.context).load("https://image.tmdb.org/t/p/w500${movie.poster_path}").into(photo)

I don't know if things have changed, but the http should be an https or nothing will send.

Collapse
 
thihaaungeng profile image
Thiha Aung

Do you have any other tutorials about items clicking and displaying detail pages?

Collapse
 
paulodhiambo profile image
Odhiambo Paul • Edited

I have one that I will be publishing within the next few days.

Collapse
 
microsoftjulius profile image
microsoftjulius

I dont know why when i run the code it returns the error E/RecyclerView: No adapter attached; skipping layout

Collapse
 
paulimjr profile image
Paulo Cesar

This is because you forget to register your adapter in your RecycleView

Basically you can do something like that. example below:

val listOfItems = lisfOf("1", "2", "3")
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = YourAdapter(listOfItems)

This is quick example that you following. I hope helped you. see u.

Collapse
 
paulodhiambo profile image
Odhiambo Paul

Please share your code through gist so that I can help you out