我正在制作一个天气应用程序,但是当我运行代码时,我得到了一个空白屏幕。
下面是我的ApiService类,在其中传递了用于改装的参数。
public interface ApiService {
@GET("/v1/search.json?key=af3b893252df43f8a9e165152182908&q")
Call<Weather> getWeather(
@Query("key") String mKey,
@Query("q") String mCityName);
}
下面是我声明了url的WeatherClient.java类
public class WeatherClient {
private static final String ROOT_URL = "https://api.apixu.com";
/**
* Get Retrofit Instance
*/
private static Retrofit getRetrofitInstance() {
return new Retrofit.Builder()
.baseUrl(ROOT_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
/**
* Get API Service
*
* @return API Service
*/
public static ApiService getApiService() {
return getRetrofitInstance().create(ApiService.class);
}
}
我正在跟踪异常 java.lang.IllegalStateException:应为BEGIN_OBJECT,但在第1行第2列路径$
处为BEGIN_ARRAYbelow my Weather.java Pojo class
public class Weather {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("region")
@Expose
private String region;
@SerializedName("country")
@Expose
private String country;
@SerializedName("lat")
@Expose
private Double lat;
@SerializedName("lon")
@Expose
private Double lon;
@SerializedName("url")
@Expose
private String url;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public Double getLon() {
return lon;
}
public void setLon(Double lon) {
this.lon = lon;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
below my json response
[
{
"id": 803267,
"name": "Paris, Ile-de-France, France",
"region": "Ile-de-France",
"country": "France",
"lat": 48.87,
"lon": 2.33,
"url": "paris-ile-de-france-france"
},
{
"id": 790830,
"name": "Les Batignolles, Ile-de-France, France",
"region": "Ile-de-France",
"country": "France",
"lat": 48.88,
"lon": 2.32,
"url": "les-batignolles-ile-de-france-france"
},
{
"id": 760995,
"name": "Batignolles, Ile-de-France, France",
"region": "Ile-de-France",
"country": "France",
"lat": 48.88,
"lon": 2.32,
"url": "batignolles-ile-de-france-france"
},
{
"id": 813415,
"name": "Saint-Merri, Ile-de-France, France",
"region": "Ile-de-France",
"country": "France",
"lat": 48.85,
"lon": 2.35,
"url": "saint-merri-ile-de-france-france"
},
{
"id": 805471,
"name": "Port De La Villette, Ile-de-France, France",
"region": "Ile-de-France",
"country": "France",
"lat": 48.88,
"lon": 2.37,
"url": "port-de-la-villette-ile-de-france-france"
},
{
"id": 761826,
"name": "Belleville-Villette, Ile-de-France, France",
"region": "Ile-de-France",
"country": "France",
"lat": 48.88,
"lon": 2.37,
"url": "belleville-villette-ile-de-france-france"
},
{
"id": 787420,
"name": "La Villette, Ile-de-France, France",
"region": "Ile-de-France",
"country": "France",
"lat": 48.88,
"lon": 2.37,
"url": "la-villette-ile-de-france-france"
},
{
"id": 822578,
"name": "Villette, Ile-de-France, France",
"region": "Ile-de-France",
"country": "France",
"lat": 48.88,
"lon": 2.37,
"url": "villette-ile-de-france-france"
},
{
"id": 778641,
"name": "Grenelle, Ile-de-France, France",
"region": "Ile-de-France",
"country": "France",
"lat": 48.85,
"lon": 2.3,
"url": "grenelle-ile-de-france-france"
},
{
"id": 813697,
"name": "Saint-Ouen-Sur-Seine, Ile-de-France, France",
"region": "Ile-de-France",
"country": "France",
"lat": 48.9,
"lon": 2.33,
"url": "saint-ouen-sur-seine-ile-de-france-france"
}
]
答案 0 :(得分:0)
您将获得Weather的Json数组,而不是Json对象。更改方法中的返回类型。
public interface ApiService {
// You don't need to explicitly pass ``key`` and ``q``. These query params will be added by retrofit automatically.
@GET("/v1/search.json")
// return type is List<Weather>
Call<List<Weather>> getWeather(
@Query("key") String mKey,
@Query("q") String mCityName);
}