際際滷

際際滷Share a Scribd company logo
Zaawansowany Retrofit
O mnie
 Maciej Puchalski
 Entuzjasta Androida
 Rowerzysta
 SoftwareHUT
Agenda
 Kod bez Retrofita
 Om坦wienie podstaw Retrofita
 Pokazanie wybranych zaawansowanych przykad坦w
 Jak rozwin Retrofita?
Kod do endpointa
String url =
"https://api.github.com/users/sennajavie/repos;
URL obj = new URL(url);
HttpURLConnection con =
(HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
final HashMap<String, String> requestParams =
new HashMap<>();
requestParams.put("User-Agent","Mozilla/5.0");
requestParams.put("Content-Type","message");
Kod do endpointa.
BufferedReader in = new BufferedReader(...);
String data;
StringBuffer response = new StringBuffer();
while ((data = in.readLine()) != null) {
response.append(data);
}
Kod do endpointa czy to ju甜 koniec?
BufferedReader in = new BufferedReader(...);
String data;
StringBuffer response = new StringBuffer();
while ((data = in.readLine()) != null) {
response.append(data);
}
Kod do endpointa.
in.close();
Stos kodu
in.close();
Wkracza Retrofit
}
in.close();
Wkracza Retrofit
}@GET("users/{user}/repos")
Call<List<Repository>>
getReposList(@Path("user") String user);
in.close();
jSession #4 - Maciej Puchalski - Zaawansowany retrofit
interface ApiClient {
@GET("users/{user}/repos")
Call<List<Repository>>
getReposList(@Path("user") String user);
}
Kod do endpointa
GsonConverterFactory.create()
Endpoint - Skd model?
 Gson: com.squareup.retrofit2:converter-gson
 Jackson: com.squareup.retrofit2:converter-jackson
 Moshi: com.squareup.retrofit2:converter-moshi
 Protobuf: com.squareup.retrofit2:converter-protobuf
 Wire: com.squareup.retrofit2:converter-wire
 Simple XML: com.squareup.retrofit2:converter-simplexml
Convertery
interface ApiClient {
@GET("users/{user}/repos")
Call<List<Repository>>
getReposList(@Path("user") String user);
}
Kod do endpointa
users/{user}/repos
vs
/users/{user}/repos
Endpoint - Co z tym URLem?
https://api.github.com/dummy/ + users/{user}/repos =
https://api.github.com/dummy/users/{user}/repos
Endpoint - Co z tym URLem?
https://api.github.com/dummy/ + /users/{user}/repos =
https://api.github.com/users/{user}/repos
dummy?
Endpoint - Co z tym URLem?
interface ApiClient {
@GET
Call<List<Repository>>
getReposList(@Url String url,
@Path("user") String user);
}
Endpoint URL bonus!
interface ApiClient {
@GET("users/{user}/repos")
Call<List<Repository>>
getReposList(@Path("user") String user);
}
Kod do endpointa
interface ApiClient {
@FormUrlEncoded
@HTTP(method = "DELETE",
path = "users/{user}/repos",
hasBody = true)
Call<ResponseBody>
deleteRepo(@Body Repository repo,
@Field("Name") String value);
}
Endpoint - DELETE z BODY?!
interface ApiClient {
@FormUrlEncoded
@POST("users/newUser")
Call<ResponseBody>
postNewUser(@Field("id") int id,
@Field("name") String name,
@Field("mail") String mail);
}
Kod do endpointa
interface ApiClient {
@FormUrlEncoded
@GET("users/{user}/repos")
Call<List<Repository>>
getReposList(@Path("user") String user,
@Field("id") int id,
@Header("lang") String lang);
}
Zaawansowany kod do endpointa
gson = GsonConverterFactory.create();
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(gson)
.baseUrl("https://api.github.com/")
.build();
return retrofit.create(ApiClient.class);
Utworzenie clienta
@Override
public okhttp3.Response intercept(Chain chain)
{
final Request request =
chain.request().newBuilder()
.addHeader("Authorization", "CODE")
.build();
return chain.proceed(request);
}
Client - Modyfikacja requestu
final OkHttpClient client =
new OkHttpClient.Builder()
.addInterceptor(new AuthHeaderInterceptor())
.build();
//..
retrofitBuilder.client(client)
//..
return retrofit.create(ApiClient.class);
Client - Modyfikacja requestu
final OkHttpClient client =
new OkHttpClient.Builder()
.addInterceptor(new AuthHeaderInterceptor())
.build();
//..
retrofitBuilder.client(client)
//..
return retrofit.create(ApiClient.class);
Client - Modyfikacja requestu
HttpLoggingInterceptor loggingInterceptor =
new HttpLoggingInterceptor();
loggingInterceptor.setLevel(
HttpLoggingInterceptor.Level.BODY);
Client - Debug
OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.addInterceptor(authHeaderInterceptor)
OkHttpClient.Builder()
.addInterceptor(authHeaderInterceptor)
.addInterceptor(loggingInterceptor)
Client - Kolejno debugu!
+
-
reposCall=apiClient.getReposList("sennajavie");
reposCall.enqueue((call, response) -> {
List<Repository> repos= response.body();
});
Przykadowe u甜ycie endpointa
+
Retrofit
Retrofit retrofit =
new Retrofit.Builder()
//...
.addCallAdapterFactory(
RxJavaCallAdapterFactory.create())
//...
.build();
RxJava + Retrofit
interface ApiClient {
@GET("users/{user}/repos")
Observable<List<Repository>>
getReposList(@Path("user") String user);
}
RxJava + Retrofit
reposObservable =
apiClient.getReposList("sennajavie");
reposObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe((repos) -> {
repositories.get(0);
});
RxJava - Przykadowe u甜ycie
reposObservable =
apiClient.getReposList("sennajavie");
reposObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe((repos) -> {
repositories.get(0);
});
RxJava - Przykadowe u甜ycie
reposObservable.
subscribeOn(Schedulers.io())
reposObservable.
observeOn(AndroidSchedulers.mainThread())
RxJava - Zarzdzanie 敬岳一温馨庄
RxJava - Zarzdzanie 敬岳一温馨庄
IO
Thread
Main
Thread
subscribe()
executeRequest() endOfExecute()
subscriber.onNext()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
CompositeSubscription foregroundSub;
public void appStarted() {
foregroundSub = new CompositeSubscription();
}
public void appStopped() {
foregroundSub.unsubscribe();
}
RxJava - Przerwanie dziaania
Subscriber<List<Repository>> reposSub =
(repos) -> {
repos.get(0);
};
foregroundSub.add(reposSub);
//
reposObservable.subscribe(reposSub);
RxJava - Przerwanie dziaania
RxJava - Zarzdzanie 敬岳一温馨庄
IO
Thread
Main
Thread
subscribe()
executeRequest() endOfExecute()
subscriber.onNext()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
unsubscribe()
 Kr坦tki kod
 Mo甜liwoci
 Kontrola endpointa
 Integracja z RxJava
Reasumujc...
puchalski.maciej.official@gmail.com
Dzikuj
店r坦da
 https://unsplash.com/photos/PW0-vZD0wis
 https://unsplash.com/photos/AoSAOV2Vtro
 https://unsplash.com/search/elegant?photo=dgOJDAv96s8
 http://www.amphinicy.com/upload/blog/1000/30_rxjava.png
 http://atl.co.ug/big/wp-content/uploads/2015/05/Retrofit.jpg
Ad

Recommended

Asynchronicity: concurrency. A tale of
Asynchronicity: concurrency. A tale of
Joel Lord
Asynchonicity: concurrency. A tale of
Asynchonicity: concurrency. A tale of
Joel Lord
GraphQL Los Angeles Meetup 際際滷s
GraphQL Los Angeles Meetup 際際滷s
Grant Miller
OWASP Proxy
OWASP Proxy
Security B-Sides
GoCracow #5 Bartlomiej klimczak - GoBDD
GoCracow #5 Bartlomiej klimczak - GoBDD
Bartomiej Kiebasa
Android Libs - Retrofit
Android Libs - Retrofit
Daniel Costa Gimenes
Using Logstash, elasticsearch & kibana
Using Logstash, elasticsearch & kibana
Alejandro E Brito Monedero
Little Big Ruby
Little Big Ruby
LittleBIGRuby
腑菴ャx/net/context (Finding God with x/net/context)
腑菴ャx/net/context (Finding God with x/net/context)
guregu
Data File Handiling File POINTERS IN C++
Data File Handiling File POINTERS IN C++
subham sahu
Process monitoring in UNIX shell scripting
Process monitoring in UNIX shell scripting
Dan Morrill
File handling complete programs in c++
File handling complete programs in c++
zain ul hassan
Fluentd meetup #2
Fluentd meetup #2
Treasure Data, Inc.
Cis 216 shell scripting
Cis 216 shell scripting
Dan Morrill
Retrofit 2 - O que devemos saber
Retrofit 2 - O que devemos saber
Bruno Vieira
httpie
httpie
Scott Leberknight
Using Cerberus and PySpark to validate semi-structured datasets
Using Cerberus and PySpark to validate semi-structured datasets
Bartosz Konieczny
Web Audio API + AngularJS
Web Audio API + AngularJS
Chris Bateman
APPlause - DemoCamp Munich
APPlause - DemoCamp Munich
Peter Friese
Life of an Fluentd event
Life of an Fluentd event
Kiyoto Tamura
Legacy applications - 4Developes konferencja, Piotr Pasich
Legacy applications - 4Developes konferencja, Piotr Pasich
Piotr Pasich
Debugging: Rules & Tools
Debugging: Rules & Tools
Ian Barber
Devinsampa nginx-scripting
Devinsampa nginx-scripting
Tony Fabeen
Redis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your application
rjsmelo
What's new in PHP 5.5
What's new in PHP 5.5
Tom Corrigan
Finding Clojure
Finding Clojure
Matthew McCullough
Infinum Android Talks #16 - Retrofit 2 by Kristijan Jurkovic
Infinum Android Talks #16 - Retrofit 2 by Kristijan Jurkovic
Infinum
various tricks for remote linux exploits by Seok-Ha Lee (wh1ant)
various tricks for remote linux exploits by Seok-Ha Lee (wh1ant)
CODE BLUE
Retrofit Technology Overview by Cumulations Technologies
Retrofit Technology Overview by Cumulations Technologies
Cumulations Technologies
Android: a full-stack to consume a REST API
Android: a full-stack to consume a REST API
Romain Rochegude

More Related Content

What's hot (20)

腑菴ャx/net/context (Finding God with x/net/context)
腑菴ャx/net/context (Finding God with x/net/context)
guregu
Data File Handiling File POINTERS IN C++
Data File Handiling File POINTERS IN C++
subham sahu
Process monitoring in UNIX shell scripting
Process monitoring in UNIX shell scripting
Dan Morrill
File handling complete programs in c++
File handling complete programs in c++
zain ul hassan
Fluentd meetup #2
Fluentd meetup #2
Treasure Data, Inc.
Cis 216 shell scripting
Cis 216 shell scripting
Dan Morrill
Retrofit 2 - O que devemos saber
Retrofit 2 - O que devemos saber
Bruno Vieira
httpie
httpie
Scott Leberknight
Using Cerberus and PySpark to validate semi-structured datasets
Using Cerberus and PySpark to validate semi-structured datasets
Bartosz Konieczny
Web Audio API + AngularJS
Web Audio API + AngularJS
Chris Bateman
APPlause - DemoCamp Munich
APPlause - DemoCamp Munich
Peter Friese
Life of an Fluentd event
Life of an Fluentd event
Kiyoto Tamura
Legacy applications - 4Developes konferencja, Piotr Pasich
Legacy applications - 4Developes konferencja, Piotr Pasich
Piotr Pasich
Debugging: Rules & Tools
Debugging: Rules & Tools
Ian Barber
Devinsampa nginx-scripting
Devinsampa nginx-scripting
Tony Fabeen
Redis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your application
rjsmelo
What's new in PHP 5.5
What's new in PHP 5.5
Tom Corrigan
Finding Clojure
Finding Clojure
Matthew McCullough
Infinum Android Talks #16 - Retrofit 2 by Kristijan Jurkovic
Infinum Android Talks #16 - Retrofit 2 by Kristijan Jurkovic
Infinum
various tricks for remote linux exploits by Seok-Ha Lee (wh1ant)
various tricks for remote linux exploits by Seok-Ha Lee (wh1ant)
CODE BLUE
腑菴ャx/net/context (Finding God with x/net/context)
腑菴ャx/net/context (Finding God with x/net/context)
guregu
Data File Handiling File POINTERS IN C++
Data File Handiling File POINTERS IN C++
subham sahu
Process monitoring in UNIX shell scripting
Process monitoring in UNIX shell scripting
Dan Morrill
File handling complete programs in c++
File handling complete programs in c++
zain ul hassan
Cis 216 shell scripting
Cis 216 shell scripting
Dan Morrill
Retrofit 2 - O que devemos saber
Retrofit 2 - O que devemos saber
Bruno Vieira
Using Cerberus and PySpark to validate semi-structured datasets
Using Cerberus and PySpark to validate semi-structured datasets
Bartosz Konieczny
Web Audio API + AngularJS
Web Audio API + AngularJS
Chris Bateman
APPlause - DemoCamp Munich
APPlause - DemoCamp Munich
Peter Friese
Life of an Fluentd event
Life of an Fluentd event
Kiyoto Tamura
Legacy applications - 4Developes konferencja, Piotr Pasich
Legacy applications - 4Developes konferencja, Piotr Pasich
Piotr Pasich
Debugging: Rules & Tools
Debugging: Rules & Tools
Ian Barber
Devinsampa nginx-scripting
Devinsampa nginx-scripting
Tony Fabeen
Redis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your application
rjsmelo
What's new in PHP 5.5
What's new in PHP 5.5
Tom Corrigan
Infinum Android Talks #16 - Retrofit 2 by Kristijan Jurkovic
Infinum Android Talks #16 - Retrofit 2 by Kristijan Jurkovic
Infinum
various tricks for remote linux exploits by Seok-Ha Lee (wh1ant)
various tricks for remote linux exploits by Seok-Ha Lee (wh1ant)
CODE BLUE

Similar to jSession #4 - Maciej Puchalski - Zaawansowany retrofit (14)

Retrofit Technology Overview by Cumulations Technologies
Retrofit Technology Overview by Cumulations Technologies
Cumulations Technologies
Android: a full-stack to consume a REST API
Android: a full-stack to consume a REST API
Romain Rochegude
Java Libraries You Cant Afford to Miss
Java Libraries You Cant Afford to Miss
Andres Almiray
Infinum Android Talks #01 - Retrofit
Infinum Android Talks #01 - Retrofit
Infinum
Introduction to Retrofit
Introduction to Retrofit
Kazuhiro Serizawa
Lab 5-Android
Lab 5-Android
Lilia Sfaxi
Retrofit Android by Chris Ollenburg
Retrofit Android by Chris Ollenburg
Trey Robinson
Modern Android app library stack
Modern Android app library stack
Tom叩邸 Kypta
Extending Retrofit for fun and profit
Extending Retrofit for fun and profit
Matthew Clarke
Retrofit Library In Android
Retrofit Library In Android
InnovationM
Retrofit library for android
Retrofit library for android
InnovationM
Performance #4 network
Performance #4 network
Vitali Pekelis
Android Performance #4: Network
Android Performance #4: Network
Yonatan Levin
Advanced #2 networking
Advanced #2 networking
Vitali Pekelis
Retrofit Technology Overview by Cumulations Technologies
Retrofit Technology Overview by Cumulations Technologies
Cumulations Technologies
Android: a full-stack to consume a REST API
Android: a full-stack to consume a REST API
Romain Rochegude
Java Libraries You Cant Afford to Miss
Java Libraries You Cant Afford to Miss
Andres Almiray
Infinum Android Talks #01 - Retrofit
Infinum Android Talks #01 - Retrofit
Infinum
Lab 5-Android
Lab 5-Android
Lilia Sfaxi
Retrofit Android by Chris Ollenburg
Retrofit Android by Chris Ollenburg
Trey Robinson
Modern Android app library stack
Modern Android app library stack
Tom叩邸 Kypta
Extending Retrofit for fun and profit
Extending Retrofit for fun and profit
Matthew Clarke
Retrofit Library In Android
Retrofit Library In Android
InnovationM
Retrofit library for android
Retrofit library for android
InnovationM
Performance #4 network
Performance #4 network
Vitali Pekelis
Android Performance #4: Network
Android Performance #4: Network
Yonatan Levin
Advanced #2 networking
Advanced #2 networking
Vitali Pekelis
Ad

Recently uploaded (20)

Key Requirements to Successfully Implement Generative AI in Edge DevicesOpt...
Key Requirements to Successfully Implement Generative AI in Edge DevicesOpt...
Edge AI and Vision Alliance
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Safe Software
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
The Future of Data, AI, and AR: Innovation Inspired by You.pdf
The Future of Data, AI, and AR: Innovation Inspired by You.pdf
Safe Software
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
Muhammad Rizwan Akram
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
Key Requirements to Successfully Implement Generative AI in Edge DevicesOpt...
Key Requirements to Successfully Implement Generative AI in Edge DevicesOpt...
Edge AI and Vision Alliance
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Safe Software
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
The Future of Data, AI, and AR: Innovation Inspired by You.pdf
The Future of Data, AI, and AR: Innovation Inspired by You.pdf
Safe Software
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
Muhammad Rizwan Akram
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
Ad

jSession #4 - Maciej Puchalski - Zaawansowany retrofit