mchd
mchd

Reputation: 3163

How to implement Retrofit and GSON?

I'm a novice Android developer. I'm trying to build a movie database app using tmdb. For this, I am using the Kotlin language and chose to use Retrofit and GSON for my JSON parsing and HTTP calls. However, I haven't done this before. I went through multiple tutorials but one is different than the other and my A.D.D. doesn't really help when it comes to deriving the concepts.

For now, this is my code. All it does is take a placeholder image and display it in a 3 column recyclerview grid layout 300x (random number because I don't have a list size yet):

MainActivity.kt

class MainActivity() : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val recyclerView = findViewById<RecyclerView>(R.id.recycler_view);
        recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.layoutManager = GridLayoutManager(this, 3);
        recyclerView.adapter = PosterAdapter()
    }
}

PosterAdapter.kt

class PosterAdapter() : RecyclerView.Adapter<PosterHolder>(){

    override fun getItemCount(): Int { return 300}

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PosterHolder{
        val layoutInflater = LayoutInflater.from(parent.context)
        val listItem = layoutInflater.inflate(R.layout.list_item, parent, false)
        return PosterHolder(listItem)
    }

    override fun onBindViewHolder(holder: PosterHolder, position: Int) {
        holder.view.movie_poster?.setImageResource(R.mipmap.beauty_and_the_beast_ver3)
        holder.view.movie_poster?.scaleType = ImageView.ScaleType.FIT_XY
    }
}

class PosterHolder(val view: View) : RecyclerView.ViewHolder(view), View.OnClickListener {
    var imageView: ImageView? = null

    fun PosterHolder(view: View){ this.imageView = view.findViewById<View>(R.id.movie_poster) as ImageView }

    override fun onClick(p0: View?) {}
}

I don't want someone to cook the code for me. I can't learn that way. I would appreciate a simple step-by-step explanation on how to implement both libraries in my app.

Upvotes: 0

Views: 337

Answers (1)

Islam Abdalla
Islam Abdalla

Reputation: 1514

Here is a simple code snippits for how you can setup Retrofit with Kotlin. First, the api interface where you will define your network calls. For example:

interface ServerAPI {

    @FormUrlEncoded
    @POST("/api/v1/login")
    fun login(@Header("Authorization") authorization: String): Call<LoginResponse>
}

Then, create Retrofit instance. Personally, I prefer to make a kotlin object and intialize it with by lazy. Also, notice the GsonConverterFactory

object RetrofitServer {

    val client: ServerAPI by lazy {

        val client = OkHttpClient.Builder()
                .build()
        val retrofit = Retrofit.Builder()
                .baseUrl("http://127.0.0.1/")
                // For Gson
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build()
        retrofit.create(ServerAPI::class.java)
    }
}

Then that's it. To make a call, simply

RetrofitServer.client.login("Auth header").enqueue(object: Callback<BalanceResponse> {
    override fun onFailure(call: Call<BalanceResponse>?, t: Throwable?) {

    }

    override fun onResponse(call: Call<BalanceResponse>?, t: Response<BalanceResponse>?) {

    }
}
)

Upvotes: 1

Related Questions