I was able to display some weather data on my app using retrofit from a JSON response, but the current time(dt), sunrise, and sunset displays on my textviews as seconds i.e 1612730263. https://i.stack.imgur.com/1pCq4.png So I need to convert the time to a real-time format(hour and minute) e.g 9:30 PM. Java: Date from unix timestamp and How to convert seconds to time format? doesn't work for me because my response is from an API
My API response:
{
"lat":9.0765,
"lon":7.3986,
"timezone":"Africa/Lagos",
"timezone_offset":3600,
"current":{
"dt":1612779720,
"sunrise":1612763455,
"sunset":1612805901,
"temp":304.15,
"feels_like":302.14,
"pressure":1013,
"humidity":33,
"dew_point":286,
"uvi":8.42,
"clouds":42,
"visibility":7000,
"wind_speed":4.12,
"wind_deg":100,
"weather":[
{
"id":802,
"main":"Clouds",
"description":"scattered clouds",
"icon":"03d"
}
]
}
}
My Retrofit call for the time in HomeActivity:
Retrofit retrofit = new Retrofit.Builder().baseUrl(BaseUrl).addConverterFactory(GsonConverterFactory.create()).build();
WeatherService service = retrofit.create(WeatherService.class);
Call<WeatherResponse> call = service.getCurrentWeatherData(lat, lon, AppId);
call.enqueue(new Callback<WeatherResponse>() {
@Override
public void onResponse(@NonNull Call<WeatherResponse> call, @NonNull Response<WeatherResponse> response) {
if (response.code() == 200) {
WeatherResponse weatherResponse = response.body();
assert weatherResponse != null;
assert response.body() != null;
// current time textview
time_field.setText(String.valueOf(response.body().getCurrent().getDt()));
My Retrofit call for the time & in FirstFragment:
Retrofit retrofit = new Retrofit.Builder().baseUrl(BaseUrl).addConverterFactory(GsonConverterFactory.create()).build();
WeatherService service = retrofit.create(WeatherService.class);
Call<WeatherResponse> call = service.getCurrentWeatherData(lat, lon, AppId);
call.enqueue(new Callback<WeatherResponse>() {
@Override
public void onResponse(@NonNull Call<WeatherResponse> call, @NonNull Response<WeatherResponse> response) {
if (response.code() == 200) {
WeatherResponse weatherResponse = response.body();
assert weatherResponse != null;
assert response.body() != null;
// sunrise & sunset time textviews
rise_time.setText(response.body().getCurrent().getSunrise() + " AM");
set_time.setText(response.body().getCurrent().getSunset() + " PM");
EDIT
WeatherResponse.java:
public class WeatherResponse {
@SerializedName("lat")
@Expose
private Double lat;
@SerializedName("lon")
@Expose
private Double lon;
@SerializedName("timezone")
@Expose
private String timezone;
@SerializedName("timezone_offset")
@Expose
private Integer timezoneOffset;
@SerializedName("current")
@Expose
private Current current;
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 getTimezone() {
return timezone;
}
public void setTimezone(String timezone) {
this.timezone = timezone;
}
public Integer getTimezoneOffset() {
return timezoneOffset;
}
public void setTimezoneOffset(Integer timezoneOffset) {
this.timezoneOffset = timezoneOffset;
}
public Current getCurrent() {
return current;
}
public void setCurrent(Current current) {
this.current = current;
}
}
Please I need the code provided to be linked to my app so that it will be easier to implement it. I have basic knowledge of dt conversion, but not from a weather API response. I think using my textviews to convert the data will be better(If there's any way you can use my textviews to do it)
from Convert current time(dt) from seconds to real time format(hour and minute)
No comments:
Post a Comment