Skip to content

Commit 0ccfd1b

Browse files
committed
add volley vs Retrofit
1 parent 086db09 commit 0ccfd1b

6 files changed

Lines changed: 904 additions & 5 deletions

‎Android加强/3D旋转动画.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
3D旋转动画
2+
===
3+
4+
终于切切实实弄明白matrix那几个方法的使用了,比如preTranslate, setTranslate, postTranslate这些。以前对它们都是一知半解,以为这几个方法没什么区别,其实还是有很大不同的,最紧要是这几个方法的调用顺序对坐标变换的影响。抽象的说pre方法是向前"生长", post方法是向后"生长",具体拿个例子来说,比如一个matrix调用了下列一系列的方法:
5+
6+
7+
8+
matrix.preScale(0.5f, 1); matrix.preTranslate(10, 0); matrix.postScale(0.7f, 1); matrix.postTranslate(15, 0); 则坐标变换经过的4个变换过程依次是:translate(10, 0) -> scale(0.5f, 1) -> scale(0.7f, 1) -> translate(15, 0), 所以对matrix方法的调用顺序是很重要的,不同的顺序往往会产生不同的变换效果。pre方法的调用顺序和post方法的互不影响,即以下的方法调用和前者在真实坐标变换顺序里是一致的, matrix.postScale(0.7f, 1); matrix.preScale(0.5f, 1); matrix.preTranslate(10, 0); matrix.postTranslate(15, 0);
9+
10+
而matrix的set方法则会对先前的pre和post操作进行刷除,而后再设置它的值,比如下列的方法调用:
11+
12+
matrix.preScale(0.5f, 1); matrix.postTranslate(10, 0); matrix.setScale(1, 0.6f); matrix.postScale(0.7f, 1); matrix.preTranslate(15, 0); 其坐标变换顺序是translate(15, 0) -> scale(1, 0.6f) -> scale(0.7f, 1).
13+
14+
15+
16+
17+
---
18+
19+
- 邮箱 :charon.chui@gmail.com
20+
- Good Luck!

‎Android加强/Android开发中的MVP模式详解.md‎

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ Android开发中的MVP模式详解
33

44
[MVC、MVP、MVVM介绍](https://github.com/CharonChui/AndroidNote/blob/master/Java%E5%9F%BA%E7%A1%80/MVC%E4%B8%8EMVP%E5%8F%8AMVVM.md)
55

6+
![image](https://raw.githubusercontent.com/CharonChui/Pictures/master/android_mvp.jpg?raw=true)
67

7-
在`Android`开发中,如果不注重架构的话,`Activity`类就会变得愈发庞大。这是因为在`Android`开发中`View`和其他的线程可以共存于`Activity`内。那最大的问题是什么呢? 其实就是**`Activity`中同事存在业务逻辑和`UI`逻辑。这导致增加了单元测试和维护的成本。
8+
在`Android`开发中,如果不注重架构的话,`Activity`类就会变得愈发庞大。这是因为在`Android`开发中`View`和其他的线程可以共存于`Activity`内。那最大的问题是什么呢? 其实就是`Activity`中同时存在业务逻辑和`UI`逻辑。这导致增加了单元测试和维护的成本。
89

910
![image](https://raw.githubusercontent.com/CharonChui/Pictures/master/activity_is_god.png?raw=true)
1011

@@ -166,5 +167,52 @@ Android开发中的MVP模式详解
166167

167168
---
168169

170+
171+
上面只是抛砖引玉。`MVP`的有点十分明显,就是代码解耦、可以让逻辑清晰,但是同样它也会有缺点,它的缺点就是项目的复杂程度会增加,项目中会多出很多类。
172+
之前很多人都在讨论该如何去正确的设计使用`MVP`来避免它的缺点,众说纷纭,很多人讨论的你死我活。直到`Google`发布了`MVP架构蓝图`,大家才意识到这才是规范。
173+
174+
项目地址:[android-architecture](https://github.com/googlesamples/android-architecture)
175+
`Google`将该项目命名为`Android`的架构蓝图,我想从名字上已可以看穿一切。
176+
177+
在它的官方介绍中是这样说的:
178+
179+
> The Android framework offers a lot of flexibility when it comes to defining how to organize and architect an Android app. This freedom, whilst very valuable, can also result in apps with large classes, inconsistent naming and architectures (or lack of) that can make testing, maintaining and extending difficult.
180+
181+
> Android Architecture Blueprints is meant to demonstrate possible ways to help with these common problems. In this project we offer the same application implemented using different architectural concepts and tools.
182+
183+
> You can use these samples as a reference or as a starting point for creating your own apps. The focus here is on code structure, architecture, testing and maintainability. However, bear in mind that there are many ways to build apps with these architectures and tools, depending on your priorities, so these shouldn't be considered canonical examples. The UI is deliberately kept simple.
184+
185+
186+
187+
已完成的示例:
188+
189+
- todo-mvp/ - Basic Model-View-Presenter architecture.
190+
- todo-mvp-loaders/ - Based on todo-mvp, fetches data using Loaders.
191+
- todo-mvp-databinding/ - Based on todo-mvp, uses the Data Binding Library.
192+
- todo-mvp-clean/ - Based on todo-mvp, uses concepts from Clean Architecture.
193+
- todo-mvp-dagger/ - Based on todo-mvp, uses Dagger2 for Dependency Injection
194+
- todo-mvp-contentproviders/ - Based on todo-mvp-loaders, fetches data using Loaders and uses Content Providers
195+
196+
正在进行中的示例:
197+
198+
- dev-todo-mvp-rxjava/ - Based on todo-mvp, uses RxJava for concurrency and data layer abstraction.
199+
200+
201+
我们接下来就用`todo-mvp`来进行分析,这个应用非常简单,主要有以下几个功能:
202+
203+
- 列表页:展示所有的`todo`项
204+
- 添加页:添加`todo`项
205+
- 详情页:查看`todo`项的详情
206+
- 统计页:查看当前所有已完成`todo`及未完成项的统计数据
207+
208+
209+
210+
211+
212+
213+
214+
---
215+
216+
169217
- 邮箱 :charon.chui@gmail.com
170-
- Good Luck!
218+
- Good Luck!

‎Android加强/InstantRun详解.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ InstantRun详解
102102

103103
在`Instant Run`更改内容前,`Android Studio`会你的应用版本是否支持`Instant Run`并且`App Server`是否运行在对其有用的端口(内部使用了Socket)。 这样来确定你的应用是否在前台运行,并且找到`Studio`所需要的构建`ID`.
104104

105-
总结一下,其实就是内部会对每个`class`文件注入`instant run`相关的代码,然后自定义一个`application`内部指定自定义的`classLoader`(也就是说不使用默认的classLoader了,只要用了Instant Run就需要使用它自定义的classLoader),然后在应用程序里面开启一个服务器,`Studio`将修改的代码发送到该服务器,然后再通过自定义的`classLoader`加载代码的时候会去请求该服务器判断代码是否有更新,如果有更新就会通过委托机制加载新更新的代码然后注入到应用程序中,这样就完成了替换的操作。
105+
总结一下,其实就是内部会对每个`class`文件注入`instant run`相关的代码,然后自定义一个`application`内部指定自定义的`classLoader`(也就是说不使用默认的`classLoader`了,只要用了`Instant Run`就需要使用它自定义的`classLoader`),然后在应用程序里面开启一个服务器,`Studio`将修改的代码发送到该服务器,然后再通过自定义的`classLoader`加载代码的时候会去请求该服务器判断代码是否有更新,如果有更新就会通过委托机制加载新更新的代码然后注入到应用程序中,这样就完成了替换的操作。
106106

107107
###热修复过程
108108

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
volley-retrofit-okhttp之我们该如何选择网路框架
2+
===
3+
4+
说起`Volley`、`Retrofit`、`OkHttp`相信基本没有人不知道。当然这里把`OkHttp`放进来可能有些不恰当。
5+
因为`OkHttp`的官方介绍是`An HTTP+HTTP/2 client for Android and Java applications`。
6+
也就是说`OkHttp`是基于`http`协议封装的一套请求客户端。它是真正的网络请求部分,
7+
与`HttpClient`、`HttpUrlConnection`是一样的,
8+
但是显然它的效率非常高(说到这里顺便提一嘴,从`Android 4.4`开始`HttpUrlConnection`内部默认使用的也是`OkHttp`,
9+
具体请参考之前的文章[HttpUrlConnection详解](https://github.com/CharonChui/AndroidNote/blob/master/Android%E5%8A%A0%E5%BC%BA/HttpURLConnection%E8%AF%A6%E8%A7%A3.md))。
10+
而`Volley`、`Retrofit`是控制请求的队列、切换、解析、缓存等逻辑。所以`Volley`和`Retrofit`都可以结合`OkHttp`来使用。
11+
12+
13+
在`Android`开发中有很多网络请求框架,但是比较过来比较过去,最后最倾向的就是这两个:
14+
15+
- `Volley`:`Google`发布的网络请求框架,专门为移动设备定制,小而美。
16+
- `Retrofit`:良心企业 `Square`由大神`JakeWharton`主导的开源项目,是基于`OkHttp`封装的一套`Resetful`网络请求框架。`Type-safe HTTP client for Android and Java by Square, Inc.`
17+
18+
19+
有关`Volley`的介绍请看之前发布的文章[Volley源码分析](https://github.com/CharonChui/AndroidNote/blob/master/Android%E5%8A%A0%E5%BC%BA/Volley%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90.md)
20+
21+
22+
这里就不分别介绍他俩了,直接说各自的优缺点:
23+
24+
- `Retrofit`使用起来更简单。而`Volley`配置起来会稍微麻烦,因为`Volley`可以使用`HttpClient`、`HttpUrlConnection`、`OkHttp`我们需要根据自己的需求去配置。而`Retrofit`只能结合`OkHttp`使用。
25+
26+
- `Retrofit`依赖于`OkHttp`,从而会导致它的包大小会比`Volley`的大。
27+
28+
- `Volley`有很好的内存缓存管理,它在解析之前会将整个相应部分都加载到内存中,所以它对于小的网络请求非常合适,但是不支持`post`大数据,所以不适合上传文件。而`Retrofit`使用的是硬盘缓存,所以相比起从缓存这块来讲`Retrofit`可能会更慢一些。
29+
30+
- `Retrofit`依赖于`OkHttp`,而`OkHttp`自身会避免同时两次请求同一个请求。所以`Retrofit`同样会和`Volley`一样去避免重复的请求,只不过它是在网络层来处理的。
31+
32+
- `Volley`在网络请求部分默认依赖于`Apache HttpClient`。而`Apache HttpClient`从`API 23`开始已经在`Android`中被移除并废弃了。这就是为什么很多开发者会认为`Volley`已经过时了,因为`Volley`并没有迁移到新的未废弃的代码。
33+
34+
- 默认情况下`Volley`会在`DefaultRetryPolicy`中会将读取和连接的超时时间设置为`2.5s`,并且对每次请求失败或者超时都有一次自动重试。 所以对于一些服务器响应可能会超过`2s`的请求,开发者需要格外的小心下。`Retrofit`的默认超时时间是`10s`,而且它对失败或者超时的操作不会自动重试。
35+
- 很多开发者都会说`Retrofit`会比`Volley`更快。因为有人专门去测试过,其实这里是不严谨的。因为`Volley`可以结合使用`HttpUrlConnection`、`HttpClient`、`OkHttp`等来使用,而`Retrofit`是用`OkHttp`一起,所以如果你让`Volley`结合`OkHttp`之后再来测试你就会发现总体来说其实他们不相上下。
36+
37+
38+
- `Volley`实现了很完善的`Activity`声明周期管理。
39+
40+
虽然`Volley`之前也有一些问题,但是它们也都被各个大神修复。
41+
42+
43+
所以综合起来说使用`Volley+OKHttp`的组合是非常不错的,既可以保证速度又可以满足对缓存、重试等的处理。但是如果你是`RxJava`的使用者那你可能会更偏向于使用`Retrofit`,因为`Retrofit`可以无缝结合`RxJava`使用。目前主流的一套框架就是`Retrofit + OkHttp + RxJava + Dagger2 `,但是对使用者的要求也相对要高些。
44+
45+
46+
---
47+
48+
- 邮箱 :charon.chui@gmail.com
49+
- Good Luck!

‎Android加强/性能优化相关工具.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,8 @@ public void ProcessPeople() {
340340
- `touch .bash_profile`创建
341341
- `open -e .bash_profile`打开
342342
- 添加
343-
```
344-
#Hierarchy Viewer Variable
343+
```java
344+
#Hierarchy Viewer Variable
345345
export ANDROID_HVPROTO=ddm
346346
```
347347
- `source .bash_profile`

0 commit comments

Comments
 (0)