Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
AplikacjaALX
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
AplikacjaALX
Commits
b78ef561
Commit
b78ef561
authored
May 10, 2024
by
patryk
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pobieranie danych: URL i HttpClient
parent
17834afc
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
76 additions
and
0 deletions
+76
-0
.gitignore
.gitignore
+1
-0
Pobieranie1_URL.java
src/main/java/pobieranie/Pobieranie1_URL.java
+34
-0
Pobieranie2_HttpClient_DoPliku.java
src/main/java/pobieranie/Pobieranie2_HttpClient_DoPliku.java
+41
-0
No files found.
.gitignore
View file @
b78ef561
...
...
@@ -3,3 +3,4 @@
/.project
/.classpath
/.settings/
/waluty.json
src/main/java/pobieranie/Pobieranie1_URL.java
0 → 100644
View file @
b78ef561
package
pobieranie
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.URL
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.StandardCopyOption
;
public
class
Pobieranie1_URL
{
public
static
void
main
(
String
[]
args
)
{
final
String
adres
=
"http://api.nbp.pl/api/exchangerates/tables/A/?format=json"
;
try
{
URL
url
=
new
URL
(
adres
);
// Od Java 21 bardziej zalecane jest:
// URI uri = new URI(adres);
// URL url = uri.toURL();
try
(
InputStream
input
=
url
.
openStream
())
{
// z input stream dane można czytać porcjami jako ciągi bajtów
// byte[] tablica_bajtow = new byte[1024];
// int ile = input.read(tablica_bajtow);
// System.out.println(new String(tablica_bajtow, 0, ile));
// ale tutaj całość pobrnaych danych zapiszemy do pliku
Files
.
copy
(
input
,
Path
.
of
(
"waluty.json"
),
StandardCopyOption
.
REPLACE_EXISTING
);
System
.
out
.
println
(
"Gotowe"
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
src/main/java/pobieranie/Pobieranie2_HttpClient_DoPliku.java
0 → 100644
View file @
b78ef561
package
pobieranie
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
import
java.net.http.HttpClient
;
import
java.net.http.HttpRequest
;
import
java.net.http.HttpResponse
;
import
java.net.http.HttpResponse.BodyHandlers
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.StandardCopyOption
;
public
class
Pobieranie2_HttpClient_DoPliku
{
public
static
void
main
(
String
[]
args
)
{
final
String
adres
=
"http://api.nbp.pl/api/exchangerates/tables/A/?format=json"
;
try
{
URI
uri
=
new
URI
(
adres
);
HttpClient
client
=
HttpClient
.
newHttpClient
();
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
).
build
();
HttpResponse
<
InputStream
>
response
=
client
.
send
(
request
,
BodyHandlers
.
ofInputStream
());
System
.
out
.
println
(
"response: "
+
response
);
System
.
out
.
println
(
"status: "
+
response
.
statusCode
());
System
.
out
.
println
(
"Content-Type: "
+
response
.
headers
().
firstValue
(
"content-type"
).
orElse
(
"--"
));
if
(
response
.
statusCode
()
!=
200
)
{
return
;
}
// body jest takiego typu, jaki wynika z użytego BodyHandler
try
(
InputStream
input
=
response
.
body
())
{
Files
.
copy
(
input
,
Path
.
of
(
"waluty.json"
),
StandardCopyOption
.
REPLACE_EXISTING
);
System
.
out
.
println
(
"Gotowe"
);
}
}
catch
(
IOException
|
URISyntaxException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
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