-
Notifications
You must be signed in to change notification settings - Fork 0
Home
wangchen edited this page Mar 20, 2017
·
53 revisions
####本项目的目的是学习研究并最终实现基于Retrofit OkHttp 和RxJava Gson的Android 网络请求库
- 实现AuthToken的过期和二次自动获取机制 100%
- 实现基础的参数的自动加入 100%
- 实现参数的加密和签名 100%
- 支持GET POST 以及文件传输 60%
- 实现一个NodeJs Server 模拟真实的服务器 100%
- 方便的支持Mock 40%
- 支持自定义Cache 100%
###URL Connection和 OkHttp 之间的关系 首先是conn = (HttpURLConnection) myUrl.openConnection();
public URLConnection openConnection() throws IOException {
return streamHandler.openConnection(this);
} /**
* Sets the stream handler factory for this VM.
*
* @throws Error if a URLStreamHandlerFactory has already been installed
* for the current VM.
*/
public static synchronized void setURLStreamHandlerFactory(URLStreamHandlerFactory factory) {
if (streamHandlerFactory != null) {
throw new Error("Factory already set");
}
streamHandlers.clear();
streamHandlerFactory = factory;
} void setupStreamHandler() {
// Check for a cached (previously looked up) handler for
// the requested protocol.
streamHandler = streamHandlers.get(protocol);
if (streamHandler != null) {
return;
}
if (protocol.equals("http")) {
try {
String name = "com.android.okhttp.HttpHandler";
streamHandler = (URLStreamHandler) Class.forName(name).newInstance();
} catch (Exception e) {
throw new AssertionError(e);
}
} else if (protocol.equals("https")) {
try {
String name = "com.android.okhttp.HttpsHandler";
streamHandler = (URLStreamHandler) Class.forName(name).newInstance();
} catch (Exception e) {
throw new AssertionError(e);
}
} else if (protocol.equals("jar")) {
streamHandler = new JarHandler();
}
if (streamHandler != null) {
streamHandlers.put(protocol, streamHandler);
}
}*第三方Jar包怎么统一统计网络请求时间?
###什么是Retrofit 它 的使用方法API 简介
- Retrofit 是Square公司提供的高效的Http库,基于OK HTTP提供网络访问功能
- 将底层的代码都封装起来, 应用关注业务中的数据模型和操作方法
- API 设计
public interface IUserProfileService {
@Headers("Cache-Control: max-age=6400")
@GET("/users/{user}/profile")
Observable<Response<UserProfile>> getUserProfile(@Query("userId") String userId);
@FormUrlEncoded
@POST("/users/{user}/profile")
Observable<ResultModel> updateUserProfile(@Field("userId") String userId, @Body UserProfile profile);
@GET("widget/list")
Call<List<Widget>> widgetList();
}- API 使用
OkHttpClient.Builder clientBuilder = new OkHttpClient().newBuilder();
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
clientBuilder.addInterceptor(httpLoggingInterceptor);
clientBuilder.addInterceptor(new AddParamIterceptor());
CacheInterceptor cacheInterceptor = new CacheInterceptor(mContext);
clientBuilder.addInterceptor(new SignatureIterceptor()).addInterceptor(cacheInterceptor);
sOkHttpClient = clientBuilder.build();
cacheInterceptor.setCache(sOkHttpClient.cache());
sRetrofit = new Retrofit.Builder().client(sOkHttpClient)
.baseUrl(API)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitUtil.getInstance(this)
.get(IUserProfileService.class)
.getUserProfile("498238400")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Response<UserProfile>>() {
});
###内部原理
- 动态代理
- CallAdapter
- Converter 设计模式
###项目代码分析
依赖和参考项目
- https://github.com/alighters/AndroidDemos
- npm modules express body-parser
参考文献 http://expressjs.com/ https://github.com/alighters/AndroidDemos