American Young Coder

AYC logo
Loading Events

« All Events

  • This event has passed.

6 PM – Android Game Dev – Gamas

November 12 @ 6:00 pm - 7:00 pm

Today We Did
  1. We continued with Weather App project.
  2. We fetched weather data from https://openweathermap.org/
  3. We get the API Key from openweathermap.org.
Homework
  1. Continue with MainActivity.fetchWeatherInfo(String cityName) method.
  2. You need to make request to openweathermap.org server and get its JSON. This is very similar with how you did with pixabay.com code below. Similar but not 100% the same. You need to modify the code when you get the json from the server.
  3. OkHttpClient httpClient = new OkHttpClient();
    Request request = new Request.Builder().url(url).build();
    httpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            Log.e("MainActivity", "Error fetching city image", e);
        }
    
        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (response.isSuccessful()) {
                String jsonResponse = response.body().string();
                System.out.println(jsonResponse);
                Gson gson = new Gson();
                PixabayResponse pixabayResponse = gson.fromJson(jsonResponse, PixabayResponse.class);
                System.out.println("Total = "+pixabayResponse.getTotal());
                System.out.println("TotalHits = "+pixabayResponse.getTotalHits());
    
                if (!pixabayResponse.getHits().isEmpty()) {
                    int index = random.nextInt(pixabayResponse.getHits().size());
                    String imageUrl = pixabayResponse.getHits().get(index).getWebformatURL();
                    
                    runOnUiThread(() -> {
                        Glide.with(MainActivity.this).load(imageUrl).into(cityImage);
                    });
                }
            } else {
                Log.e("MainActivity", "Error fetching city image: " + response.code());
            }
        }
    });

Details

Date:
November 12
Time:
6:00 pm - 7:00 pm