[英]How to get retrofit response in kotlin
我正在嘗試使用改造來解析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。 請幫助我,如果你知道
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;
}
}
LIST
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.