Reputation: 640
I'm trying to do a simple web service with Retrofit 2. The web url is: https://www.metaweather.com/api/location/1100661/2017/6/26/
List weather= response.body();
When I tried to get data from the response, it is null. Even though the response code is 200.
I used http://www.jsonschema2pojo.org/ to make various versions of the Weather class. The results are the same. Some classes are listed below. Will someone please tell me what's wrong with my code? Thank you
MainActivity:
public class MainActivity extends AppCompatActivity {
private static final String API_BASE_URL = "https://www.metaweather.com/";
@BindView(R.id.tv_weather)
TextView mWeatherTv;
@BindView(R.id.bn_weather)
AppCompatButton mWeatherBn;
@BindView(R.id.tv_details)
TextView mDetailsTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
public void fetchData(View view) {
Toast.makeText(getApplicationContext(), "fetch data", Toast.LENGTH_SHORT).show();
try {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiClient client = retrofit.create(ApiClient.class);
Call<List<Weather>> call = client.getWeatherData();
call.enqueue(new Callback<List<Weather>>() {
@Override
public void onResponse(Call<List<Weather>> call, Response<List<Weather>> response) {
Log.v("testing", "Response is " + response.raw().toString());
if (!response.isSuccessful()){
return;
}
List<Weather> weather= response.body();
String details = "";
Log.v("testing", weather.toString());
if (weather != null && weather.size()>0){
for (int i = 0; i < weather.size(); i++){
String name = weather.get(i).getWeatherStateAbbr();
details += "\n\nweather: " + name;
}
mDetailsTv.setText(details);
Log.v("testing", "weather is " + details);
}
}
@Override
public void onFailure(Call<List<Weather >> call, Throwable t) {
Log.v("testing", "Failed " + String.valueOf(t));
}
});
} catch (Exception e){
Log.e("exception", String.valueOf(e));
}
}
}
ApiClient:
import java.util.List;
import me.anky.brisbaneweather.Weather;
import retrofit2.Call;
import retrofit2.http.GET;
public interface ApiClient {
// https://www.metaweather.com/api/location/1100661/2017/6/26/
@GET("api/location/1100661/2017/6/26/")
Call<List<Weather>> getWeatherData();
}
Weather
public class Weather {
private long id;
private String weatherStateName;
private String weatherStateAbbr;
private String windDirectionCompass;
private String created;
private String applicableDate;
private double minTemp;
private double maxTemp;
private double theTemp;
private double windSpeed;
private double windDirection;
private double airPressure;
private long humidity;
private Object visibility;
private long predictability;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getWeatherStateName() {
return weatherStateName;
}
public void setWeatherStateName(String weatherStateName) {
this.weatherStateName = weatherStateName;
}
public String getWeatherStateAbbr() {
return weatherStateAbbr;
}
public void setWeatherStateAbbr(String weatherStateAbbr) {
this.weatherStateAbbr = weatherStateAbbr;
}
public String getWindDirectionCompass() {
return windDirectionCompass;
}
public void setWindDirectionCompass(String windDirectionCompass) {
this.windDirectionCompass = windDirectionCompass;
}
public String getCreated() {
return created;
}
public void setCreated(String created) {
this.created = created;
}
public String getApplicableDate() {
return applicableDate;
}
public void setApplicableDate(String applicableDate) {
this.applicableDate = applicableDate;
}
public double getMinTemp() {
return minTemp;
}
public void setMinTemp(double minTemp) {
this.minTemp = minTemp;
}
public double getMaxTemp() {
return maxTemp;
}
public void setMaxTemp(double maxTemp) {
this.maxTemp = maxTemp;
}
public double getTheTemp() {
return theTemp;
}
public void setTheTemp(double theTemp) {
this.theTemp = theTemp;
}
public double getWindSpeed() {
return windSpeed;
}
public void setWindSpeed(double windSpeed) {
this.windSpeed = windSpeed;
}
public double getWindDirection() {
return windDirection;
}
public void setWindDirection(double windDirection) {
this.windDirection = windDirection;
}
public double getAirPressure() {
return airPressure;
}
public void setAirPressure(double airPressure) {
this.airPressure = airPressure;
}
public long getHumidity() {
return humidity;
}
public void setHumidity(long humidity) {
this.humidity = humidity;
}
public Object getVisibility() {
return visibility;
}
public void setVisibility(Object visibility) {
this.visibility = visibility;
}
public long getPredictability() {
return predictability;
}
public void setPredictability(long predictability) {
this.predictability = predictability;
}
}
And some of my dependencies are below:
compile 'com.squareup.okhttp3:okhttp:3.6.0'
testCompile 'junit:junit:4.12'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.jakewharton:butterknife:8.6.0'
Upvotes: 0
Views: 1112
Reputation: 3920
You used GsonConverterFactory
but not implemented in the right way. Here is the link: Consuming APIs with Retrofit
Tips: Retrofit builder with GsonConverterFactory
only accept POJO class with Gson @annotation
. Change your Weather class to WeatherPojo
class like this:
public class WeatherPojo {
@SerializedName("id")
@Expose
public long id;
@SerializedName("weatherStateName")
@Expose
public String weatherStateName;
@SerializedName("weatherStateAbbr")
@Expose
public String weatherStateAbbr;
@SerializedName("windDirectionCompass")
@Expose
public String windDirectionCompass;
@SerializedName("created")
@Expose
public String created;
@SerializedName("applicableDate")
@Expose
public String applicableDate;
@SerializedName("minTemp")
@Expose
public double minTemp;
@SerializedName("maxTemp")
@Expose
public double maxTemp;
@SerializedName("theTemp")
@Expose
public double theTemp;
@SerializedName("windSpeed")
@Expose
public double windSpeed;
@SerializedName("windDirection")
@Expose
public double windDirection;
@SerializedName("airPressure")
@Expose
public double airPressure;
@SerializedName("humidity")
@Expose
public long humidity;
@SerializedName("visibility")
@Expose
public Object visibility;
@SerializedName("predictability")
@Expose
public long predictability;
}
This call POJO (Plane old java object). You can use this class as your model or create new class and convert POJO to your ModelObject.
I use public field instead of getter and setter because this reason. Public field vs getter and setter
Upvotes: 1
Reputation: 104
Generate toString method in Weather.class. It should be like this:
@Override
public String toString() {
return "Weather{" +
"id='" + id + '\'' +
", weatherStateName =" + weatherStateName +
", FIELD_NAME =" + FIELD +
'}';
}
Upvotes: 0