Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
alx-javam
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
alx-javam
Commits
c060ffc0
Commit
c060ffc0
authored
May 16, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Pobieranie pogody - druga wersja
parent
5220e623
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
88 additions
and
2 deletions
+88
-2
PobierzPogodeDlaMiasta.java
...goda/src/main/java/pobieranie/PobierzPogodeDlaMiasta.java
+86
-0
WyswietlPogodeDlaMiasta.form
..._Pogoda/src/main/java/pogoda/WyswietlPogodeDlaMiasta.form
+0
-0
WyswietlPogodeDlaMiasta.java
..._Pogoda/src/main/java/pogoda/WyswietlPogodeDlaMiasta.java
+0
-0
WyswietlPogodeDlaWspolrzednych.form
.../src/main/java/pogoda/WyswietlPogodeDlaWspolrzednych.form
+1
-1
WyswietlPogodeDlaWspolrzednych.java
.../src/main/java/pogoda/WyswietlPogodeDlaWspolrzednych.java
+1
-1
No files found.
20240509_Pogoda/src/main/java/pobieranie/PobierzPogodeDlaMiasta.java
0 → 100644
View file @
c060ffc0
package
pobieranie
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.URL
;
import
java.util.Scanner
;
import
jakarta.json.Json
;
import
jakarta.json.JsonArray
;
import
jakarta.json.JsonObject
;
import
jakarta.json.JsonReader
;
import
jakarta.json.JsonValue
;
public
class
PobierzPogodeDlaMiasta
{
private
Scanner
scanner
=
new
Scanner
(
System
.
in
);
private
double
lat
;
private
double
lon
;
private
String
miasto
;
public
static
void
main
(
String
[]
args
)
{
new
PobierzPogodeDlaMiasta
().
run
();
}
public
void
run
()
{
try
{
System
.
out
.
print
(
"Podaj nazwę miasta: "
);
String
szukane
=
scanner
.
nextLine
();
if
(
znajdzMiasto
(
szukane
))
{
pobierzIWypiszPogode
();
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
private
boolean
znajdzMiasto
(
String
szukane
)
throws
IOException
{
URL
url
=
new
URL
(
"https://geocoding-api.open-meteo.com/v1/search?name="
+
szukane
);
try
(
InputStream
input
=
url
.
openStream
();
JsonReader
reader
=
Json
.
createReader
(
input
))
{
JsonObject
json
=
reader
.
readObject
();
JsonArray
results
=
json
.
getJsonArray
(
"results"
);
if
(
results
.
isEmpty
())
{
System
.
out
.
println
(
"Brak znalezionych lokalizacji"
);
return
false
;
}
System
.
out
.
println
(
"Znalezione lokalizacje:"
);
int
i
=
0
;
for
(
JsonValue
result
:
results
)
{
JsonObject
resulto
=
result
.
asJsonObject
();
//System.out.print(result);
System
.
out
.
printf
(
"%2d: <%+.3f %+.3f> → %s, %s%n"
,
i
++,
resulto
.
getJsonNumber
(
"latitude"
).
doubleValue
(),
resulto
.
getJsonNumber
(
"longitude"
).
doubleValue
(),
resulto
.
getString
(
"name"
),
resulto
.
getString
(
"country"
));
}
System
.
out
.
print
(
"Wybierz numer lokalizacji: "
);
i
=
scanner
.
nextInt
();
if
(
i
<
0
||
i
>=
results
.
size
())
{
return
false
;
}
JsonObject
loc
=
results
.
getJsonObject
(
i
);
lat
=
loc
.
getJsonNumber
(
"latitude"
).
doubleValue
();
lon
=
loc
.
getJsonNumber
(
"longitude"
).
doubleValue
();
miasto
=
loc
.
getString
(
"name"
);
return
true
;
}
}
private
void
pobierzIWypiszPogode
()
throws
IOException
{
URL
url
=
new
URL
(
"https://api.open-meteo.com/v1/forecast?latitude="
+
lat
+
"&longitude="
+
lon
+
"¤t_weather=true&hourly=temperature_2m"
);
try
(
InputStream
input
=
url
.
openStream
();
JsonReader
reader
=
Json
.
createReader
(
input
))
{
JsonObject
json
=
reader
.
readObject
();
JsonObject
weather
=
json
.
getJsonObject
(
"current_weather"
);
System
.
out
.
printf
(
"Bieżąca pogoda dla miasta %s:%n"
,
miasto
);
System
.
out
.
println
(
weather
);
System
.
out
.
printf
(
"Temperatura %.1f °C%n"
,
weather
.
getJsonNumber
(
"temperature"
).
doubleValue
());
System
.
out
.
printf
(
"Wiatr: %.1f km/h%n"
,
weather
.
getJsonNumber
(
"windspeed"
).
doubleValue
());
}
}
}
20240509_Pogoda/src/main/java/pogoda/WyswietlPogodeDlaMiasta.form
0 → 100644
View file @
c060ffc0
This diff is collapsed.
Click to expand it.
20240509_Pogoda/src/main/java/pogoda/WyswietlPogodeDlaMiasta.java
0 → 100644
View file @
c060ffc0
This diff is collapsed.
Click to expand it.
20240509_Pogoda/src/main/java/pogoda/WyswietlPogodeDlaWspolrzednych.form
View file @
c060ffc0
...
...
@@ -68,7 +68,7 @@
<Component
id=
"jLabel3"
alignment=
"3"
min=
"-2"
pref=
"31"
max=
"-2"
attributes=
"0"
/>
<Component
id=
"jLabel_Temperatura"
alignment=
"3"
min=
"-2"
pref=
"31"
max=
"-2"
attributes=
"0"
/>
</Group>
<EmptySpace
pref=
"
259
"
max=
"32767"
attributes=
"0"
/>
<EmptySpace
pref=
"
61
"
max=
"32767"
attributes=
"0"
/>
</Group>
</Group>
</DimensionLayout>
...
...
20240509_Pogoda/src/main/java/pogoda/WyswietlPogodeDlaWspolrzednych.java
View file @
c060ffc0
...
...
@@ -112,7 +112,7 @@ public class WyswietlPogodeDlaWspolrzednych extends javax.swing.JFrame {
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
BASELINE
)
.
addComponent
(
jLabel3
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
31
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)
.
addComponent
(
jLabel_Temperatura
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
31
,
javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
))
.
addContainerGap
(
259
,
Short
.
MAX_VALUE
))
.
addContainerGap
(
61
,
Short
.
MAX_VALUE
))
);
pack
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment