简体   繁体   English

如何将 java 接口代码转换为 kotlin 代码?

[英]how to convert java interface code to kotlin code?

I want to convert some java code to kotlin.我想将一些 java 代码转换为 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。

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:如果您使用的是 Android Studio,您可以:

  • Configure Kotlin in project在项目中配置 Kotlin
  • Right-click on .java file and select 'Convert Java File to Kotlin File'右击.java文件和 select '将 Java 文件转换为 Z539A3A5859D1F07FFBAZE74D1F07FFBAZE7 文件

If you copy paste the Java snippet into.kt file, it will also ask you if you want to convert it automatically.如果您将 Java 片段复制粘贴到.kt 文件中,它还会询问您是否要自动转换它。

Your above java code should be like the following.您上面的 java 代码应如下所示。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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