I'm new to Retrofit, I have an activity hosting 3 fragments for a weather app. My FirstFragment is where the current weather data will be displayed using OpenWeatherMap One call API https://openweathermap.org/api/one-call-api. I noticed that retrofit request calls cannot be made on the fragment class, so I tried using my Activity class and everything worked properly except these following lines:
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;
time_zone.setText(response.body().getTimezone());
time_field.setText(response.body().getCurrent.getDt());
current_temp.setText(response.body().getCurrent().getTemp()+" ℃");
current_output.setText(response.body().getCurrent().getWeather().getDescription);
rise_time.setText(response.body().getCurrent().getSunrise()+" AM");
set_time.setText(response.body().getCurrent().getSunset()+" PM");
temp_out.setText(response.body().getCurrent().getTemp()+" ℃");
Press_out.setText(response.body().getCurrent().getPressure()+" hpa");
Humid_out.setText(response.body().getCurrent().getHumidity()+" %");
Ws_out.setText(response.body().getCurrent).getWind_speed()+" Km/h");
Visi_out.setText(response.body().getCurrent().getVisibility()+" m");
UV_out.setText(response.body().getCurrent().getUvi());
showing error Cannot resolve Symbol "response"
. What can be done to fix it, please? My full code: HomeActivity.java
public class HomeActivity extends AppCompatActivity {
public static String BaseUrl = "http://api.openweathermap.org/";
public static String AppId = "";
public static String lat = "9.0574";
public static String lon = "7.4898";
// User Timezone name, current time, current temperature, current condition, sunrise, sunset, temperature, pressure, humidity, wind_speed, visibility, UV Index
TextView time_zone, time_field, current_temp, current_output, rise_time, set_time, temp_out, Press_out, Humid_out, Ws_out, Visi_out, UV_out;
ConstraintLayout constraintLayout;
public static int count=0;
int[] drawable =new int[]{R.drawable.dubai,R.drawable.central_bank_of_nigeria,R.drawable.eiffel_tower,R.drawable.hong_kong,R.drawable.statue_of_liberty};
Timer _t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
time_zone = findViewById(R.id.textView9);
time_field = findViewById(R.id.textView4);
current_temp = findViewById(R.id.textView10);
current_output = findViewById(R.id.textView11);
rise_time = findViewById(R.id.textView25);
set_time = findViewById(R.id.textView26);
temp_out = findViewById(R.id.textView28);
Press_out = findViewById(R.id.textView29);
Humid_out = findViewById(R.id.textView30);
Ws_out = findViewById(R.id.textView33);
Visi_out = findViewById(R.id.textView34);
UV_out = findViewById(R.id.textView35);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
NavController navController = Navigation.findNavController(this, R.id.fragment);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
constraintLayout = findViewById(R.id.layout);
constraintLayout.setBackgroundResource(R.drawable.dubai);
_t = new Timer();
_t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() { // run on ui thread
@Override
public void run() {
if (count < drawable.length) {
constraintLayout.setBackgroundResource(drawable[count]);
count = (count + 1) % drawable.length;
}
}
});
}
}, 5000, 5000);
}
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;
time_zone.setText(response.body().getTimezone());
time_field.setText(response.body().getCurrent.getDt());
current_temp.setText(response.body().getCurrent().getTemp()+" ℃");
current_output.setText(response.body().getCurrent().getWeather().getDescription);
rise_time.setText(response.body().getCurrent().getSunrise()+" AM");
set_time.setText(response.body().getCurrent().getSunset()+" PM");
temp_out.setText(response.body().getCurrent().getTemp()+" ℃");
Press_out.setText(response.body().getCurrent().getPressure()+" hpa");
Humid_out.setText(response.body().getCurrent().getHumidity()+" %");
Ws_out.setText(response.body().getCurrent).getWind_speed()+" Km/h");
Visi_out.setText(response.body().getCurrent().getVisibility()+" m");
UV_out.setText(response.body().getCurrent().getUvi());
}
}
}
}
WeatherService.java
public interface WeatherService {
@GET("data/2.5/weather?")
Call<WeatherResponse> getCurrentWeatherData(@Query("lat") String lat, @Query("lon") String lon, @Query("APPID") String app_id);
}
I also want to make my fragment class make use of the Activity data, how to do that, please?
from Cannot resolve Symbol "response"
No comments:
Post a Comment