Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
alx_java2b_20250412
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_java2b_20250412
Commits
9757ce16
Commit
9757ce16
authored
May 24, 2025
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Przykłady HttpClient
parent
ad763901
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
373 additions
and
0 deletions
+373
-0
Pobieranie1_URL.java
...-RestKlient/src/main/java/pobieranie/Pobieranie1_URL.java
+34
-0
Pobieranie2_HttpClient_DoPliku.java
.../main/java/pobieranie/Pobieranie2_HttpClient_DoPliku.java
+41
-0
Pobieranie3_HttpClient_String.java
...c/main/java/pobieranie/Pobieranie3_HttpClient_String.java
+36
-0
Pobieranie4_JSON.java
...RestKlient/src/main/java/pobieranie/Pobieranie4_JSON.java
+64
-0
Klient02_URL_JSON.java
...tKlient/src/main/java/sklep/klient/Klient02_URL_JSON.java
+2
-0
Klient03_HttpClient.java
...lient/src/main/java/sklep/klient/Klient03_HttpClient.java
+35
-0
Klient04_HttpClient_String.java
...rc/main/java/sklep/klient/Klient04_HttpClient_String.java
+29
-0
Klient05_HttpClient_Accept.java
...rc/main/java/sklep/klient/Klient05_HttpClient_Accept.java
+30
-0
Klient06_HttpClient_JSON.java
.../src/main/java/sklep/klient/Klient06_HttpClient_JSON.java
+48
-0
Klient07_HttpClient_JSON_Lista.java
...ain/java/sklep/klient/Klient07_HttpClient_JSON_Lista.java
+54
-0
No files found.
PC27-RestKlient/src/main/java/pobieranie/Pobieranie1_URL.java
0 → 100644
View file @
9757ce16
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
();
}
}
}
PC27-RestKlient/src/main/java/pobieranie/Pobieranie2_HttpClient_DoPliku.java
0 → 100644
View file @
9757ce16
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
();
}
}
}
PC27-RestKlient/src/main/java/pobieranie/Pobieranie3_HttpClient_String.java
0 → 100644
View file @
9757ce16
package
pobieranie
;
import
java.io.IOException
;
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
;
public
class
Pobieranie3_HttpClient_String
{
public
static
void
main
(
String
[]
args
)
{
final
String
adres
=
"http://api.nbp.pl/api/exchangerates/tables/A/?format=json"
;
// spróbuj xml
try
{
URI
uri
=
new
URI
(
adres
);
HttpClient
client
=
HttpClient
.
newHttpClient
();
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
).
build
();
HttpResponse
<
String
>
response
=
client
.
send
(
request
,
BodyHandlers
.
ofString
());
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
String
body
=
response
.
body
();
System
.
out
.
println
(
"Długość stringa: "
+
body
.
length
());
System
.
out
.
println
(
body
);
}
catch
(
IOException
|
URISyntaxException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
PC27-RestKlient/src/main/java/pobieranie/Pobieranie4_JSON.java
0 → 100644
View file @
9757ce16
package
pobieranie
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.math.BigDecimal
;
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
jakarta.json.Json
;
import
jakarta.json.JsonArray
;
import
jakarta.json.JsonObject
;
import
jakarta.json.JsonReader
;
import
jakarta.json.JsonValue
;
public
class
Pobieranie4_JSON
{
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
;
}
// parsowanie JSONa z pomocą biblioteki Jakarta JSON (utworzone w Java EE 7 i dalej rozwijane)
// alternatywy: GSON, org.json, Jackson
JsonArray
json
;
try
(
JsonReader
reader
=
Json
.
createReader
(
response
.
body
()))
{
json
=
reader
.
readArray
();
}
// tutaj już zamykamy połączenie
// zerowym (i jedynym) elementem tablicy jest {obiekt JSONowy} odpowiadający tabeli walut
JsonObject
tabela
=
json
.
getJsonObject
(
0
);
// bezpośrednio w tym obiekcie są takie pola, jak numer i data
System
.
out
.
println
(
"Numer tabeli: "
+
tabela
.
getString
(
"no"
));
System
.
out
.
println
(
"Data: "
+
tabela
.
getString
(
"effectiveDate"
));
// w polu rates znajduje się tablica zawierająca dane poszczególnych walut
JsonArray
rates
=
tabela
.
getJsonArray
(
"rates"
);
System
.
out
.
println
(
"Liczba walut: "
+
rates
.
size
());
System
.
out
.
println
();
for
(
JsonValue
rate
:
rates
)
{
// System.out.println(rate);
JsonObject
rateObj
=
rate
.
asJsonObject
();
String
nazwa
=
rateObj
.
getString
(
"currency"
);
String
kod
=
rateObj
.
getString
(
"code"
);
BigDecimal
kurs
=
rateObj
.
getJsonNumber
(
"mid"
).
bigDecimalValue
();
System
.
out
.
println
(
kod
+
" "
+
nazwa
+
": "
+
kurs
);
}
}
catch
(
IOException
|
URISyntaxException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
PC27-RestKlient/src/main/java/sklep/klient/Klient02_URL_JSON.java
View file @
9757ce16
...
@@ -5,6 +5,7 @@ import java.io.InputStream;
...
@@ -5,6 +5,7 @@ import java.io.InputStream;
import
java.net.URL
;
import
java.net.URL
;
import
jakarta.json.*
;
import
jakarta.json.*
;
import
jakarta.json.JsonValue.ValueType
;
public
class
Klient02_URL_JSON
{
public
class
Klient02_URL_JSON
{
/* W tej wersji pobieramy dane JSON podstawowym sposobem URL, ale pobrane dane parsujemy za pomocą narzędzi
/* W tej wersji pobieramy dane JSON podstawowym sposobem URL, ale pobrane dane parsujemy za pomocą narzędzi
...
@@ -19,6 +20,7 @@ public class Klient02_URL_JSON {
...
@@ -19,6 +20,7 @@ public class Klient02_URL_JSON {
JsonArray
array
=
reader
.
readArray
();
JsonArray
array
=
reader
.
readArray
();
// System.out.println(array);
// System.out.println(array);
for
(
JsonValue
jsonValue
:
array
)
{
for
(
JsonValue
jsonValue
:
array
)
{
// if(jsonValue.getValueType() == ValueType.OBJECT)
//System.out.println(jsonValue);
//System.out.println(jsonValue);
JsonObject
jsonObject
=
jsonValue
.
asJsonObject
();
JsonObject
jsonObject
=
jsonValue
.
asJsonObject
();
System
.
out
.
println
(
jsonObject
.
getString
(
"productName"
));
System
.
out
.
println
(
jsonObject
.
getString
(
"productName"
));
...
...
PC27-RestKlient/src/main/java/sklep/klient/Klient03_HttpClient.java
0 → 100644
View file @
9757ce16
package
sklep
.
klient
;
import
java.io.IOException
;
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.Path
;
import
java.nio.file.Paths
;
public
class
Klient03_HttpClient
{
/* W Java 11 pojawiło się rozwiązanie "HttpClient", które umożliwia komunikację HTTP z dużą kontrolą nad szczegółami.
* Wysyłając zapytanie, od razu trzeba podać odpowiedni "BodyHandler",
* który pozwoli nam odczytać treść odpowiedzi we właściwy dla nas sposób.
*
* W tej wersji wynik jest zapisywany do pliku.
*/
public
static
void
main
(
String
[]
args
)
{
try
(
HttpClient
httpClient
=
HttpClient
.
newHttpClient
())
{
URI
uri
=
new
URI
(
Ustawienia
.
ADRES_USLUGI
+
"/products.json"
);
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
).
build
();
HttpResponse
<
Path
>
response
=
httpClient
.
send
(
request
,
BodyHandlers
.
ofFile
(
Paths
.
get
(
"wynik03.json"
)));
System
.
out
.
println
(
"response "
+
response
);
System
.
out
.
println
(
"status: "
+
response
.
statusCode
());
System
.
out
.
println
(
"Content-Type: "
+
response
.
headers
().
firstValue
(
"Content-Type"
).
orElse
(
"BRAK"
));
System
.
out
.
println
(
"OK, zapisany plik: "
+
response
.
body
());
}
catch
(
URISyntaxException
|
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
PC27-RestKlient/src/main/java/sklep/klient/Klient04_HttpClient_String.java
0 → 100644
View file @
9757ce16
package
sklep
.
klient
;
import
java.io.IOException
;
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
;
public
class
Klient04_HttpClient_String
{
public
static
void
main
(
String
[]
args
)
{
try
(
HttpClient
httpClient
=
HttpClient
.
newHttpClient
())
{
URI
uri
=
new
URI
(
Ustawienia
.
ADRES_USLUGI
+
"/products.json"
);
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
).
build
();
// Body z odpowiedzi pobierzemy jako obiekt String z całą treścią
HttpResponse
<
String
>
response
=
httpClient
.
send
(
request
,
BodyHandlers
.
ofString
());
System
.
out
.
println
(
"response "
+
response
);
System
.
out
.
println
(
"status: "
+
response
.
statusCode
());
System
.
out
.
println
(
"Content-Type: "
+
response
.
headers
().
firstValue
(
"Content-Type"
).
orElse
(
"BRAK"
));
String
body
=
response
.
body
();
System
.
out
.
println
(
"Treść odpowiedzi:\n"
+
body
);
}
catch
(
URISyntaxException
|
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
PC27-RestKlient/src/main/java/sklep/klient/Klient05_HttpClient_Accept.java
0 → 100644
View file @
9757ce16
package
sklep
.
klient
;
import
java.io.IOException
;
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
;
public
class
Klient05_HttpClient_Accept
{
public
static
void
main
(
String
[]
args
)
{
try
(
HttpClient
httpClient
=
HttpClient
.
newHttpClient
())
{
URI
uri
=
new
URI
(
Ustawienia
.
ADRES_USLUGI
+
"/products"
);
// W tej wersji do zapytania dodajemy nagłówek Accept
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
)
.
header
(
"Accept"
,
"text/plain"
)
// albo application/xml, application/json, text/html
.
build
();
HttpResponse
<
String
>
response
=
httpClient
.
send
(
request
,
BodyHandlers
.
ofString
());
System
.
out
.
println
(
"response "
+
response
);
System
.
out
.
println
(
"status: "
+
response
.
statusCode
());
System
.
out
.
println
(
"Content-Type: "
+
response
.
headers
().
firstValue
(
"Content-Type"
).
orElse
(
"BRAK"
));
System
.
out
.
println
(
"Treść odpowiedzi:\n"
+
response
.
body
());
}
catch
(
URISyntaxException
|
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
PC27-RestKlient/src/main/java/sklep/klient/Klient06_HttpClient_JSON.java
0 → 100644
View file @
9757ce16
package
sklep
.
klient
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.math.BigDecimal
;
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
jakarta.json.Json
;
import
jakarta.json.JsonObject
;
import
jakarta.json.JsonReader
;
public
class
Klient06_HttpClient_JSON
{
/* W tej wersji używamy technologii Jakarta JSON P.
*
* Widzimy drzewo danych jsonowych i jego elementytakie jak JsonObject, JsonArray, ...
* Mamy dostęp do poszczególnych pól.
*/
public
static
void
main
(
String
[]
args
)
{
try
(
HttpClient
httpClient
=
HttpClient
.
newHttpClient
())
{
URI
uri
=
new
URI
(
Ustawienia
.
ADRES_USLUGI
+
"/products/1"
);
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
)
.
header
(
"Accept"
,
"application/json"
)
.
build
();
HttpResponse
<
InputStream
>
response
=
httpClient
.
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
(
"BRAK"
));
JsonReader
reader
=
Json
.
createReader
(
response
.
body
());
JsonObject
product
=
reader
.
readObject
();
System
.
out
.
println
(
"Pobrany obiekt jsonowy: "
+
product
);
String
nazwa
=
product
.
getString
(
"productName"
);
String
opis
=
product
.
getString
(
"description"
,
"BRAK OPISU"
);
BigDecimal
cena
=
product
.
getJsonNumber
(
"price"
).
bigDecimalValue
();
System
.
out
.
println
(
nazwa
+
" za cenę "
+
cena
+
" , opis: "
+
opis
);
reader
.
close
();
}
catch
(
URISyntaxException
|
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
PC27-RestKlient/src/main/java/sklep/klient/Klient07_HttpClient_JSON_Lista.java
0 → 100644
View file @
9757ce16
package
sklep
.
klient
;
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
jakarta.json.Json
;
import
jakarta.json.JsonArray
;
import
jakarta.json.JsonObject
;
import
jakarta.json.JsonReader
;
import
jakarta.json.JsonValue
;
public
class
Klient07_HttpClient_JSON_Lista
{
public
static
void
main
(
String
[]
args
)
{
try
{
JsonArray
array
=
pobierzJsona
(
Ustawienia
.
ADRES_USLUGI
+
"/products.json"
);
for
(
JsonValue
jsonValue
:
array
)
{
JsonObject
jsonObject
=
jsonValue
.
asJsonObject
();
System
.
out
.
println
(
jsonObject
.
getString
(
"productName"
));
if
(
jsonObject
.
containsKey
(
"description"
))
{
System
.
out
.
println
(
" opis: "
+
jsonObject
.
getString
(
"description"
,
""
));
}
System
.
out
.
println
(
" cena: "
+
jsonObject
.
getJsonNumber
(
"price"
).
bigDecimalValue
());
}
}
catch
(
IOException
|
InterruptedException
|
URISyntaxException
e
)
{
e
.
printStackTrace
();
}
}
private
static
JsonArray
pobierzJsona
(
String
adres
)
throws
IOException
,
InterruptedException
,
URISyntaxException
{
try
(
HttpClient
httpClient
=
HttpClient
.
newHttpClient
())
{
URI
uri
=
new
URI
(
adres
);
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
).
build
();
HttpResponse
<
InputStream
>
response
=
httpClient
.
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
(
"BRAK"
));
return
wczytajJsona
(
response
.
body
());
}
}
private
static
JsonArray
wczytajJsona
(
InputStream
input
)
{
try
(
JsonReader
reader
=
Json
.
createReader
(
input
))
{
return
reader
.
readArray
();
}
}
}
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