hyeonjae lee
hyeonjae lee

Reputation: 1

how to convert java interface code to kotlin code?

I want to convert some java code to kotlin. this is my code wanna convert.

/*Create handle for the RetrofitInstance interface*/
    GetDataService service = RetrofitClientInstance.getRetrofitInstance().create(GetDataService.class);

And this is class i made.

class RetrofitClientInstance {

    var retrofit: Retrofit?=null
    var BASE_URL = "10.80.7.30:3000"

     fun getRetrofitInstnace() :Retrofit {
        if (retrofit == null) {
            retrofit = retrofit2.Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
        }
        return retrofit!!
    }

}

interface MovieService {

    @GET("/movie/:id/script")
    fun getCurrentMovieData(
        @Path("id") id : Int
    ) : Call<Movie>

    @GET("/movie/script")
    fun getMoviesData() : Call<List<Movie>>
}

Please help me anytime!!!!!

Upvotes: 0

Views: 404

Answers (2)

Jakir Hossain
Jakir Hossain

Reputation: 3930

Your above java code should be like the following.

var service = RetrofitClientInstance.getRetrofitInstance().create(GetDataService::class.java)

Upvotes: 1

Simon
Simon

Reputation: 1737

If you're using Android Studio, you can just:

  • Configure Kotlin in project
  • Right-click on .java file and select 'Convert Java File to Kotlin File'

If you copy paste the Java snippet into .kt file, it will also ask you if you want to convert it automatically.

Upvotes: 2

Related Questions