直接输入到我的浏览器时(appId已删除)
http://api.openweathermap.org/data/2.5/weather?q=37421&appid=xxxx
我得到了这个预期的回应
{“coord”:{“lon”:73,“lat”:31.32},“weather”:[{“id”:800,“main”:“Clear”,“description”:“晴空”,“图标 “:” 01N “}],” 基 “:” 站”, “主”:{ “温度”:286.471, “压力”:1007.05, “湿度”:86, “temp_min”:286.471, “temp_max”: 286.471, “sea_level”:1028.03, “grnd_level”:1007.05}, “风”:{ “速度”:3.01, “DEG”:252.003}, “ Cloud ”:{ “所有”:0}, “DT”:1516458282 ,“sys”:{“message”:0.0035,“country”:“PK”,“sunrise”:1516413964,“sunset”:1516451547},“id”:1179400,“name”:“Chak 247 RB Miani”, “鳕鱼”:200}
我想要的是lat和lon坐标 .
这是我的模特
public class CoordObject {
private String lat;
private String lon;
public CoordObject(
String lat,
String lon({
this.lat =lat;
this.lon =lon;
}
public String getLat(){
return lat;
}
public String getLon(){
return lon;
}
}
和我的界面
public interface OwmInterface {
@GET("/data/2.5/weather")
Call<CoordObject> getCoord(@Query("q") String zipCode,
@Query("appid") String appId);
}
和我的活动
final String BASE_URL = "https://api.openweathermap.org/";
final String API_KEY_OWM = "xxxx";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
OwmInterface request = retrofit.create(OwmInterface.class);
Call<CoordObject> call = request.getCoord(
"37421",
API_KEY_OWM
);
call.enqueue(new Callback<CoordObject>() {
@Override
public void onResponse(Call<CoordObject> call, Response<CoordObject> response){
CoordObject coordObjectResponse = response.body();
Toast.makeText(MainActivity.this, coordObjectResponse.getLat(), Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, coordObjectResponse.getLon(), Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<CoordObject> call, Throwable t) {
Toast.makeText(MainActivity.this, "failed" +t, Toast.LENGTH_SHORT).show();
}
});
我得到了回复,但祝酒词显示为空 .
1 回答
根目录中有一个包含
CoordObject
的对象 .您应该再创建一个类:
现在你的界面应该返回
RootObject
:同时检查
lat
和lon
是否来自类型double
而不是String
.