如何在Kotlin中获得改造响应

时间:2018-10-29 17:07:36

标签: android json kotlin retrofit retrofit2

我正在尝试使用改造来解析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,它仍然出错。 如果您知道

,请帮助我

1 个答案:

答案 0 :(得分:0)

关于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