【问题标题】:How to get retrofit response in kotlin如何在 kotlin 中获得改造响应
【发布时间】:2018-10-29 17:07:36
【问题描述】:

我正在尝试使用改造来解析 json,但这是我第一次使用改造

这是json的样子

    {
    "results": [
        {
            "idEvent": "576561",
            "idSoccerXML": "389946",
            "strEvent": "Liverpool vs Cardiff",
            "strFilename": "English Premier League 2018-10-27 Liverpool vs Cardiff",
            "strSport": "Soccer",
            "idLeague": "4328",
            "strLeague": "English Premier League",
            "strSeason": "1819",
            "strDescriptionEN": null,
            "strHomeTeam": "Liverpool",
            "strAwayTeam": "Cardiff",
            "intHomeScore": "4",
            "intRound": "10",
            "intAwayScore": "1",
        },
     {.
      .  
      .
}]

我正在使用改造来获取数据。 这是我的界面

interface INetworkAPI {

@GET("api/v1/json/1/eventslast.php?id=133602")
fun getAllPosts(): Call<Team>

}

这是我试图回调但我不太明白的地方

rv__list_posts?.layoutManager = LinearLayoutManager(this.requireContext())

    val retrofit = Retrofit.Builder().addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .baseUrl("https://www.thesportsdb.com/").build()

    val postsApi = retrofit.create(INetworkAPI::class.java)

    var response = postsApi.getAllPosts()

    response.observeOn(AndroidSchedulers.mainThread()).subscribeOn(IoScheduler()).subscribe {
        rv__list_posts.adapter = MainAdapter(it, this.requireContext())
    }

我不知道如何以好的方式回调它,它仍然会出错,因为它仍在使用 observeOn。 知道的请帮帮我

【问题讨论】:

标签: android json kotlin retrofit retrofit2


【解决方案1】:

Java 示例:

JSON

{
        "employeeList": [
        {
            "name": "Gretchen Rodriquez",
            "email": "gretchenrodriquez@trasola.com",
            "phone": "6204732096"
        },
        {
            "name": "Sharon Harris",
            "email": "sharonharris@trasola.com",
            "phone": "2184582386"
        },
        {
            "name": "Serrano Haynes",
            "email": "serranohaynes@trasola.com",
            "phone": "1434162816"
        },
        {
            "name": "Wagner Thornton",
            "email": "wagnerthornton@trasola.com",
            "phone": "5494622467"
        },
        {
            "name": "Mooney Dawson",
            "email": "mooneydawson@trasola.com",
            "phone": "3894433413"
        }
        ]
}

型号

public class Employee {
        @SerializedName("name")
        private String name;
        @SerializedName("email")
        private String email;
        @SerializedName("phone")
        private String phone;

        public Employee(String name, String email, String phone) {
            this.name = name;
            this.email = email;
            this.phone = phone;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getPhone() {
            return phone;
        }

        public void setPhone(String phone) {
            this.phone = phone;
        }
}

列表

public class EmployeeList {
        @SerializedName("employeeList")
        private ArrayList<Employee> employeeList;

        public ArrayList<Employee> getEmployeeArrayList() {
            return employeeList;
        }

        public void setEmployeeArrayList(ArrayList<Employee> employeeArrayList) {
            this.employeeList = employeeArrayList;
        }
}

更多:https://medium.com/@jacinth9/android-retrofit-2-0-tutorial-89de3c714c63

【讨论】:

    猜你喜欢
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2019-07-13
    相关资源
    最近更新 更多