简体   繁体   中英

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!!!!!

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.

Your above java code should be like the following.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM