Android-Network
Android Network Library & Framework Analysis and Tutorial
Network Basic
####Introduction
- What happen after a url-input: link
- Interacting with server-side APIs: Issue in my another repo
- HTTPResponse Code
Retrofit
####Introduction
- Retrofit Documentation link;
- Retrofit Principles: Proxy Design Pattern & Java Reflect & Dynamic Proxy & Callback function, it also integrates with OKHttp;
- Picasso Principles: LRU Cache & Disk Cache;
- RESTful (GET/PUT/POST/DELETE/HEAD);
- The sample is used with Picasso 2.4.0 and Retrofit 1.7.1;
####How to use?
- Gradle:
compile 'com.squareup.retrofit:retrofit:x.x.x'
, - Adding Internet Permission in AndroidManifest.xml,
- Create a ClientClass, API_URL, Class for fetch JSON member, declare and define the interface(with RESTful APIs),
- Set RestAdapter, Instance an the interfaced defined previously and fetch the API based on the parameters,
- In onCreate() method, get the callback.
Reference: OkHttp / Retrofit / Volley
Retrofit v.s. Volley
####Conclusion
- Retrofit supports Java NIO link, we use Retrofit to improve IO performance(Blocking IO creates too much thread to handle jobs!)
- Retrofit uses #GSON# by default to convert HTTP bodies to and from JSON (so we do not need to use Jackson or write our own JSON parser instead)
Volley
OkHttp
####Introduction
- OkHttp Documentation link
####How to use?
- Gradle:
compile 'com.squareup.okhttp:okhttp:x.x.x'
, - Adding Internet Permission in AndroidManifest.xml,
- Setting OkHttpClient:
sample: OkHttpClient client = new OkHttpClient();
, - Setting OkHttp Request:
sample: Request OkRequest = new Request.Builder().url(url).build();
- Setting OkHttp Response:
sample: Response OkResponse = client.newCall(OkRequest).execute();
- Getting the whole JSON string:
sample: String jsonString = OkResponse.body().string();