diff options
Diffstat (limited to 'app/src')
-rw-r--r-- | app/src/main/AndroidManifest.xml | 2 | ||||
-rw-r--r-- | app/src/main/java/com/lonkaars/bingo3/WeatherActivity.java | 48 | ||||
-rw-r--r-- | app/src/main/res/layout/weather.xml | 17 |
3 files changed, 65 insertions, 2 deletions
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 @@ <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> + <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" @@ -23,5 +24,4 @@ <activity android:name=".OrientationActivity"/> <activity android:name=".WeatherActivity"/> </application> - </manifest>
\ 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<String> data = new ArrayList<String>(); + + 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<String> adapter = new ArrayAdapter<String>(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" /> + <ProgressBar + android:id="@+id/loader" + style="?android:attr/progressBarStyle" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginTop="32dp" + android:visibility="invisible" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@+id/button" /> + <ListView + android:id="@+id/forecast_list" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginTop="32dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toBottomOf="@+id/button" /> + app:layout_constraintTop_toBottomOf="@+id/button" > + + </ListView> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file |