Search code examples
androidjsonparsingretrofit2api-design

Getting null when parsing API using Retrofit2


When entering this directly to my browser (appId removed)

http://api.openweathermap.org/data/2.5/weather?q=37421&appid=xxxx

I get this expected response

{"coord":{"lon":73,"lat":31.32},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"base":"stations","main":{"temp":286.471,"pressure":1007.05,"humidity":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","cod":200}

All I want to get is the lat and lon coordinates.

Here is my Model

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

and my interface

public interface OwmInterface {

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

and my activity

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

I get a response but the toasts show null.


Solution

  • There is an object as root that contains your CoordObject.
    You should create one more class that is:

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

    And now your interface should return a RootObject:

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

    Also check that lat and lon are from type double and not String.