Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Kotlin-java.lang.IllegalStateExpection:应为BEGIN_数组,但为字符串_Android_Kotlin_Retrofit2_Zomato Api - Fatal编程技术网

Android Kotlin-java.lang.IllegalStateExpection:应为BEGIN_数组,但为字符串

Android Kotlin-java.lang.IllegalStateExpection:应为BEGIN_数组,但为字符串,android,kotlin,retrofit2,zomato-api,Android,Kotlin,Retrofit2,Zomato Api,我面临着这个问题。我在互联网上搜索过,但答案是Java,而不是Kotlin。我如何解决这个问题?如果你还需要什么,请告诉我。我正试图找到最近的餐馆,以确定用户位置。目前我正在使用静态变量,因为我还无法获得Zomato APi。我怎样才能做到这一点?我开始学习kotlin已经4-5天了 我的餐厅数据类 data class Restaurant( @SerializedName("name") val restName: String?, @Serial

我面临着这个问题。我在互联网上搜索过,但答案是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>>

}

您的
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>
接口ZomatoAPI{
@标题(
“接受:应用程序/json”,
“用户密钥:$API_密钥”
)
@获取(“/search”)
趣味餐厅(@Query(“count”)count:Int
,@Query(“纬度”)纬度:双
,@Query(“lon”)经度:双精度
,@Query(“sort”)排序:字符串
,@Query(“订单”)订单:字符串
):单身
}

你的模型课应该是这样的

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?
)
数据类餐厅响应(
@序列化名称(“餐厅”)
瓦尔餐厅:名单?,
@SerializedName(“找到的结果”)
找到的val结果:Int?
@SerializedName(“结果和开始”)
val结果\u开始:Int?
@SerializedName(“显示结果”)
显示的val结果:Int?
)

您需要共享堆栈跟踪、看到此错误的行以及能够重现错误的最小代码issue@sidgate当我启动它时,通常没有错误,但也没有加载。在我的ListViewModel中有onSuccess和onError函数,我在这两个函数上都设置了一个断点,它会给出错误消息。“在第1行第1列路径$”错误表示这是解析响应时产生的问题。您可以添加响应的样本格式或自己检查。可能您的响应JSON有缺陷,错误是这样说的。您需要为响应创建整个类(带有子类)。我建议您将json(来自zomato api)传递到此页面并生成必要的类@MihodiLushan我将立即添加它以获得回复,但我的适配器和listview是否应使用RestaurantResponse或Listenery其他内容应与之前相同。请记住,您之前期望的是列表,现在列表位于模型类中。您应该在从响应对象获取后传递列表。我尝试了jsonschema2pojo.org并进行了工作,我需要获取所有详细信息,但只是想学习如何使用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": []
      }
    }
  ]
}
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?
)