【问题标题】:Getting null when parsing API using Retrofit2使用 Retrofit2 解析 API 时获取 null
【发布时间】:2018-06-29 15:53:52
【问题描述】:

当直接在我的浏览器中输入时(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":"晴空" ,"icon":"01n"}],"base":"stations","main":{"temp":286.471,"pressure":1007.05,"湿度":86,"temp_min":286.471,"temp_max ":286.471,"sea_level":1028.03,"grnd_level":1007.05},"wind":{"speed":3.01,"deg":252.003},"clouds":{"all":0},"dt" :1516458282,"sys":{"message":0.0035,"country":"PK","sunrise":1516413964,"sunset":1516451547},"id":1179400,"name":"Chak 247 RB Miani ","鳕鱼":200}

我只想得到纬度和经度坐标。

这是我的模型

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();
            }
    });

我收到了回复,但祝酒词显示为空。

【问题讨论】:

    标签: android json parsing retrofit2 api-design


    【解决方案1】:

    有一个对象作为根包含您的CoordObject
    您应该再创建一个类:

    public class RootObject {
    
        private CoordObject coord;
    
        public RootObject(CoordObject coord){
            this.coord = coord
        }
    
        public CoordObject getCoordinates(){
            return coord;
        }
    }
    

    现在你的界面应该返回一个RootObject

    @GET("/data/2.5/weather")
    Call<RootObject> getCoord(@Query("q") String zipCode,
                               @Query("appid") String appId);
    

    还要检查 latlon 是否来自类型 double 而不是 String

    【讨论】:

    • 非常感谢!我是否还需要在活动中将我的 CoordObject 更改为 RootObject?
    • 完美运行。感谢您清晰而详细的回答。
    猜你喜欢
    • 2021-10-26
    • 2020-07-08
    • 2023-03-12
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多