From d80cc5297c1f155e05ba56cfa301ff95a12eb9fd Mon Sep 17 00:00:00 2001 From: lonkaars Date: Tue, 13 Feb 2024 18:04:15 +0100 Subject: eindopdracht klaar --- app/build.gradle.kts | 1 + app/src/main/AndroidManifest.xml | 2 +- .../java/com/lonkaars/bingo3/WeatherActivity.java | 48 ++++++++++++++++++++++ app/src/main/res/layout/weather.xml | 17 +++++++- 4 files changed, 66 insertions(+), 2 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index b330a7d..2a6c7de 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -64,6 +64,7 @@ dependencies { implementation("androidx.compose.ui:ui-graphics") implementation("androidx.compose.ui:ui-tooling-preview") implementation("androidx.compose.material3:material3") + implementation("com.android.volley:volley:1.2.1") testImplementation("junit:junit:4.13.2") androidTestImplementation("androidx.test.ext:junit:1.1.5") androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 0cc7f54..e9de882 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,7 @@ + - \ No newline at end of file diff --git a/app/src/main/java/com/lonkaars/bingo3/WeatherActivity.java b/app/src/main/java/com/lonkaars/bingo3/WeatherActivity.java index 46c965c..3a5aa6e 100644 --- a/app/src/main/java/com/lonkaars/bingo3/WeatherActivity.java +++ b/app/src/main/java/com/lonkaars/bingo3/WeatherActivity.java @@ -1,12 +1,27 @@ package com.lonkaars.bingo3; import android.os.Bundle; +import android.util.Log; import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.ListView; +import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; +import com.android.volley.Request; +import com.android.volley.RequestQueue; +import com.android.volley.Response; +import com.android.volley.VolleyError; +import com.android.volley.toolbox.JsonObjectRequest; +import com.android.volley.toolbox.Volley; import com.google.android.material.appbar.MaterialToolbar; +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.ArrayList; + public class WeatherActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { @@ -18,4 +33,37 @@ public class WeatherActivity extends AppCompatActivity { finish(); }); } + + public void fetch_weather_data(View view) { + findViewById(R.id.loader).setVisibility(View.VISIBLE); + ListView list = findViewById(R.id.forecast_list); + RequestQueue queue = Volley.newRequestQueue(this); + String url = "https://wttr.in/?format=j1"; + + JsonObjectRequest jsonObjectRequest = new JsonObjectRequest + (Request.Method.GET, url, null, response -> { + findViewById(R.id.loader).setVisibility(View.INVISIBLE); + + ArrayList data = new ArrayList(); + + try { + JSONArray forecast = response.getJSONArray("weather"); + for (int i = 0; i < forecast.length(); i++) { + JSONObject rec = forecast.getJSONObject(i); + data.add(String.format("%s: %s°C", rec.getString("date"), rec.getString("avgtempC"))); + } + } catch (Exception e) { + Toast.makeText(WeatherActivity.this, "foutje!", Toast.LENGTH_SHORT).show(); + return; + } + + final ArrayAdapter adapter = new ArrayAdapter(WeatherActivity.this, + android.R.layout.simple_list_item_1, data); + list.setAdapter(adapter); + }, error -> { + findViewById(R.id.loader).setVisibility(View.INVISIBLE); + Toast.makeText(WeatherActivity.this, "foutje!", Toast.LENGTH_SHORT).show(); + }); + queue.add(jsonObjectRequest); + } } \ No newline at end of file diff --git a/app/src/main/res/layout/weather.xml b/app/src/main/res/layout/weather.xml index 99d36df..9f6d589 100644 --- a/app/src/main/res/layout/weather.xml +++ b/app/src/main/res/layout/weather.xml @@ -46,18 +46,33 @@ android:id="@+id/button" android:layout_width="0dp" android:layout_height="wrap_content" + android:onClick="fetch_weather_data" android:text="pak dat zonnetje" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> + + + app:layout_constraintTop_toBottomOf="@+id/button" > + + \ No newline at end of file -- cgit v1.2.3