【问题标题】:How to implement Retrofit and GSON?如何实现改造和 GSON?
【发布时间】:2018-06-04 22:59:36
【问题描述】:

我是一名新手 Android 开发人员。我正在尝试使用 tmdb 构建电影数据库应用程序。为此,我使用 Kotlin 语言并选择使用 Retrofit 和 GSON 进行 JSON 解析和 HTTP 调用。但是,我以前没有这样做过。我经历了多个教程,但其中一个与另一个不同,而我的 A.D.D.在推导概念时并没有真正的帮助。

现在,这是我的代码。它所做的只是获取一个占位符图像并将其显示在 3 列 recyclerview 网格布局中 300x(随机数,因为我还没有列表大小):

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?) {}
}

我不希望有人为我编写代码。我不能这样学习。我将不胜感激有关如何在我的应用中实现这两个库的简单分步说明。

【问题讨论】:

  • 我在您的代码中没有看到任何与 Retrofit/GSON 相关的内容...您的网络调用在哪里?
  • 您看不到任何内容,因为“...我以前没有这样做过”

标签: android json kotlin retrofit gson


【解决方案1】:

这里有一个简单的代码片段,介绍了如何使用 Kotlin 设置 Retrofit。 首先,您将在其中定义网络调用的 api 接口。例如:

interface ServerAPI {

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

然后,创建 Retrofit 实例。就个人而言,我更喜欢创建一个kotlin 对象并使用by lazy 对其进行初始化。另外,请注意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)
    }
}

那就这样吧。要拨打电话,只需

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>?) {

    }
}
)

【讨论】:

  • 您介意解释一下interface 块中的代码吗?
  • 这是实现网络调用的标准改造方式。根据文档:“Retrofit 将您的 HTTP API 转换为 Java 接口。”和“Retrofit 类生成 [..] 接口的实现。”。链接:square.github.io/retrofit 基本上,你在一个接口中定义你的调用,并在方法上加上Retrofit注解,例如@POST@GET
  • 您在哪里拨打object RetrofitServer 以及您在哪里拨打电话?
猜你喜欢
  • 2017-12-10
  • 1970-01-01
  • 1970-01-01
  • 2018-03-07
  • 2016-08-23
  • 1970-01-01
  • 1970-01-01
  • 2022-01-26
  • 2015-06-11
相关资源
最近更新 更多