/Android-Network

Android Network Library & Framework Analysis and Tutorial

Primary LanguageJava

Android-Network

Android Network Library & Framework Analysis and Tutorial

Network Basic

####Introduction

  1. What happen after a url-input: link
  2. Interacting with server-side APIs: Issue in my another repo
  3. HTTPResponse Code

Retrofit

####Introduction

  1. Retrofit Documentation link;
  2. Retrofit Principles: Proxy Design Pattern & Java Reflect & Dynamic Proxy & Callback function, it also integrates with OKHttp;
  3. Picasso Principles: LRU Cache & Disk Cache;
  4. RESTful (GET/PUT/POST/DELETE/HEAD);
  5. The sample is used with Picasso 2.4.0 and Retrofit 1.7.1;

####How to use?

  1. Gradle: compile 'com.squareup.retrofit:retrofit:x.x.x' ,
  2. Adding Internet Permission in AndroidManifest.xml,
  3. Create a ClientClass, API_URL, Class for fetch JSON member, declare and define the interface(with RESTful APIs),
  4. Set RestAdapter, Instance an the interfaced defined previously and fetch the API based on the parameters,
  5. In onCreate() method, get the callback.

Reference: OkHttp / Retrofit / Volley
Retrofit v.s. Volley

####Conclusion

  1. Retrofit supports Java NIO link, we use Retrofit to improve IO performance(Blocking IO creates too much thread to handle jobs!)
  2. 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

  1. OkHttp Documentation link

####How to use?

  1. Gradle: compile 'com.squareup.okhttp:okhttp:x.x.x' ,
  2. Adding Internet Permission in AndroidManifest.xml,
  3. Setting OkHttpClient: sample: OkHttpClient client = new OkHttpClient();,
  4. Setting OkHttp Request: sample: Request OkRequest = new Request.Builder().url(url).build();
  5. Setting OkHttp Response: sample: Response OkResponse = client.newCall(OkRequest).execute();
  6. Getting the whole JSON string: sample: String jsonString = OkResponse.body().string();