Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.10.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.squareup.okhttp3:okhttp:4.9.3'
implementation 'com.google.code.gson:gson:2.8.9'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<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"
Expand Down
24 changes: 23 additions & 1 deletion app/src/main/java/com/example/labb1_martin/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,33 @@

import android.os.Bundle;

import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.Executor;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
Executor executor = Executors.newSingleThreadExecutor();

executor.execute(()->{
WeatherDataFetcher weatherDataFetcher = new WeatherDataFetcher();
try {
WeatherDataFetcher weatherDataFetcher1 = new WeatherDataFetcher();
System.out.println(weatherDataFetcher1.fetchWeatherData("60.10", "9.58").getTemperature());
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
});*/


}
}
}
//hej
//hej
44 changes: 44 additions & 0 deletions app/src/main/java/com/example/labb1_martin/WeatherData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.example.labb1_martin;

public class WeatherData {
public WeatherData(double temperature, String weatherCondition, String windSpeed, String precipitation, String humidity, String s) {
this.temperature = temperature;
this.weatherCondition = weatherCondition;
this.windSpeed = windSpeed;
this.precipitation = precipitation;
this.humidity = humidity;
}

public double getTemperature() {
return temperature;
}

public String getWeatherCondition() {
return weatherCondition;
}

public String getWindSpeed() {
return windSpeed;
}

public String getPrecipitation() {
return precipitation;
}

public String getHumidity() {
return humidity;
}

public String getWindDirection() {
return windDirection;
}

double temperature;
String weatherCondition;
String windSpeed;
String precipitation;
String humidity;
String windDirection;

}

41 changes: 41 additions & 0 deletions app/src/main/java/com/example/labb1_martin/WeatherDataFetcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.labb1_martin;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class WeatherDataFetcher {
public WeatherData fetchWeatherData(String latitude, String longitude) throws InterruptedException, IOException {
String url = "https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=" + latitude + "&lon=" + longitude;

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.header("User-Agent", "YourApp/1.0")
.build();

Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(response.body().string(), JsonObject.class);
JsonObject current = jsonObject.getAsJsonObject("properties").getAsJsonArray("timeseries").get(0).getAsJsonObject().getAsJsonObject("data").getAsJsonObject("instant").getAsJsonObject("details");
double temperature = current.get("air_temperature").getAsDouble();
JsonObject next1Hour = jsonObject.getAsJsonObject("properties").getAsJsonArray("timeseries").get(0).getAsJsonObject().getAsJsonObject("data").getAsJsonObject("next_1_hours").getAsJsonObject("summary");
String weatherCondition = next1Hour.has("symbol_code") ? next1Hour.get("symbol_code").getAsString() : "unknown";
String windSpeed = current.get("wind_speed").getAsString();
String windDirection = current.get("wind_from_direction").getAsString();
String humidity = current.get("relative_humidity").getAsString();
String precipitation ="";// = next1Hour.getAsJsonObject("details").get("precipitation_amount").getAsString();
return new WeatherData(temperature, weatherCondition, windSpeed, windDirection, humidity, precipitation);
} else throw new IOException("Failed to retrieve JSON data. Status code: " + response.message());



}

}