-1

我遇到了这个问题。我在互联网上搜索过,但答案是针对 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")
    }

}

我的列表视图模型

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,
            "takeaway": -1
          },
          "res_id": 16611721,
          "is_grocery_store": false
        },
        "apikey": "61528551ffc800703d600cb2c25e6900",
        "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",
          "country_id": 14,
          "locality_verbose": "Gerroa, Gerroa"
        },
        "switch_to_order_menu": 0,
        "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,
        "currency": "$",
        "highlights": [
          "Cash",
          "Breakfast",
          "Lunch",
          "No Alcohol Available",
          "Dinner",
          "Table booking recommended",
          "Outdoor Seating"
        ],
        "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": 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",
        "featured_image": "",
        "has_online_delivery": 0,
        "is_delivering_now": 0,
        "store_type": "",
        "include_bogo_offers": true,
        "deeplink": "zomato://restaurant/16611721",
        "is_table_reservation_supported": 0,
        "has_table_booking": 0,
        "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": []
            },
            {
              "review": []
            },
            {
              "review": []
            }
          ]
        },
        "establishment": [
          "Casual Dining"
        ],
        "establishment_types": []
      }
    }
  ]
}
4

1 回答 1

0

你的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?
)
于 2020-08-11T17:58:16.567 回答