问题描述
我遇到了这个问题。我已经在互联网上搜索过,但答案是Java,而不是Kotlin。我该如何解决?如果您还有其他需要,请告诉我。我正在尝试获取距离最近的餐馆作为用户位置。目前,我正在使用静态变量,因为我还无法获得Zomato APi。我怎样才能做到这一点?自从我开始学习Kotlin以来已经有4-5天了
我的餐厅数据类
data class Restaurant(
@Serializedname("name")
val restName: String?,@Serializedname("address")
val restAddress: String?
)
我的API接口
interface ZomatoAPI {
@Headers(
"Accept: application/json","user-key: $API_KEY"
)
@GET("/search")
fun getRestaurants(@Query("count") count: Int,@Query("lat") latitude: Double,@Query("lon") longitude: Double,@Query("sort") sort : String,@Query("order") order : String
): Single<List<Restaurant>>
}
我的服务等级
class RestaurantService{
private val baseUrl = "https://developers.zomato.com/api/v2.1/"
private val api: ZomatoAPI
private val latitude = 41.2
private val longitude = 28.2
init {
api = Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().setLenient().create()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
.create(ZomatoAPI::class.java)
}
fun getRestaurants(): Single<List<Restaurant>>{
return api.getRestaurants(5,latitude,longitude,"rating","desc")
}
}
我的Listviewmodel
class Listviewmodel: viewmodel(){
private val restaurantService = RestaurantService()
private val disposable = Compositedisposable()
val restaurants = mutablelivedata<List<Restaurant>>()
val restaurantLoadError = mutablelivedata<Boolean>()
val loading = mutablelivedata<Boolean>()
fun refresh() {
fetchRestaurants()
}
private fun fetchRestaurants() {
loading.value = true
restaurantService.getRestaurants()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(object: disposableSingleObserver<List<Restaurant>>(){
override fun onSuccess(value: List<Restaurant>?) {
restaurants.value = value
restaurantLoadError.value = false
loading.value = false
}
override fun onError(e: Throwable?) {
restaurantLoadError.value = true
loading.value = false
}
})
}
override fun onCleared() {
super.onCleared()
disposable.clear()
}
}
Zomato API的响应正文
{
"results_found": 1499579,"results_start": 0,"results_shown": 2,"restaurants": [
{
"restaurant": {
"R": {
"has_menu_status": {
"delivery": -1,"takeaway": -1
},"res_id": 18047914,"is_grocery_store": false
},"apikey": "61528551ffc800703d600cb2c25e6900","id": "18047914","name": "The Mossy Cafe","url": "https://www.zomato.com/mossy-point-nsw/the-mossy-cafe-mossy-point?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1","location": {
"address": "31 Pacific Street,Mossy Point","locality": "Mossy Point","city": "Mossy Point","city_id": 2181,"latitude": "-35.8369730000","longitude": "150.1796610000","zipcode": "","country_id": 14,"locality_verbose": "Mossy Point,Mossy Point"
},"switch_to_order_menu": 0,"cuisines": "Cafe Food,Coffee and Tea","timings": "7:30am – 3pm (Mon-Sun)","average_cost_for_two": 50,"price_range": 4,"currency": "$","highlights": [
"Cash","Debit Card","No Alcohol Available","Credit Card","Breakfast","Lunch","Kid Friendly","Vegan Options","Pet Friendly","Indoor Seating","gluten Free Options"
],"offers": [],"opentable_support": 0,"is_zomato_book_res": 0,"mezzo_provider": "OTHER","is_book_form_web_view": 0,"book_form_web_view_url": "","book_again_url": "","thumb": "","user_rating": {
"aggregate_rating": "3.9","rating_text": "Good","rating_color": "9ACD32","rating_obj": {
"title": {
"text": "3.9"
},"bg_color": {
"type": "lime","tint": "600"
}
},"Votes": 7
},"all_reviews_count": 0,"photos_url": "https://www.zomato.com/mossy-point-nsw/the-mossy-cafe-mossy-point/photos?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1#tabtop","photo_count": 42,"menu_url": "https://www.zomato.com/mossy-point-nsw/the-mossy-cafe-mossy-point/menu?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1&openSwipeBox=menu&showMinimal=1#tabtop","featured_image": "","has_online_delivery": 0,"is_delivering_Now": 0,"store_type": "","include_bogo_offers": true,"deeplink": "zomato://restaurant/18047914","is_table_reservation_supported": 0,"has_table_booking": 0,"events_url": "https://www.zomato.com/mossy-point-nsw/the-mossy-cafe-mossy-point/events#tabtop?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1","phone_numbers": "02 4471 8599","all_reviews": {
"reviews": []
},"establishment": [
"Café"
],"establishment_types": []
}
},{
"restaurant": {
"R": {
"has_menu_status": {
"delivery": -1,"res_id": 16611721,"id": "16611721","name": "Blue Swimmer at Seahaven","url": "https://www.zomato.com/gerroa-nsw/blue-swimmer-at-seahaven-gerroa?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1","location": {
"address": "19 Riverleigh Ave,Gerroa,Gerroa","locality": "Gerroa","city": "Gerroa","city_id": 2154,"latitude": "-34.7697390000","longitude": "150.8117400000","zipcode": "2534","locality_verbose": "Gerroa,Gerroa"
},"cuisines": "Modern Australian,Australian","timings": "7 AM to 3 PM (Mon,Tue,Wed,Thu,Sun),7 AM to 3 PM,5:30 PM to 11 PM (Fri-Sat)","average_cost_for_two": 20,"price_range": 2,"Dinner","Table booking recommended","Outdoor Seating"
],"Votes": 21
},"all_reviews_count": 11,"photos_url": "https://www.zomato.com/gerroa-nsw/blue-swimmer-at-seahaven-gerroa/photos?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1#tabtop","photo_count": 15,"menu_url": "https://www.zomato.com/gerroa-nsw/blue-swimmer-at-seahaven-gerroa/menu?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1&openSwipeBox=menu&showMinimal=1#tabtop","deeplink": "zomato://restaurant/16611721","events_url": "https://www.zomato.com/gerroa-nsw/blue-swimmer-at-seahaven-gerroa/events#tabtop?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1","phone_numbers": "(02) 4234 3796","all_reviews": {
"reviews": [
{
"review": []
},{
"review": []
},{
"review": []
}
]
},"establishment": [
"Casual Dining"
],"establishment_types": []
}
}
]
}
解决方法
您的getRestaurants
方法应如下所示:
interface ZomatoAPI {
@Headers(
"Accept: application/json","user-key: $API_KEY"
)
@GET("/search")
fun getRestaurants(@Query("count") count: Int,@Query("lat") latitude: Double,@Query("lon") longitude: Double,@Query("sort") sort : String,@Query("order") order : String
): Single<RestaurantResponse>
}
您的模型类应该是这样,
data class RestaurantResponse(
@SerializedName("restaurants")
val restaurants: List<Restaurant>?,@SerializedName("results_found")
val results_found: Int?
@SerializedName("results_start")
val results_start: Int?
@SerializedName("results_shown")
val results_shown: Int?
)