Skip to content

Commit 01375f3

Browse files
committed
update retrofit part
1 parent 99debc9 commit 01375f3

File tree

2 files changed

+300
-263
lines changed

2 files changed

+300
-263
lines changed

Android加强/Retrofit详解(上).md

Lines changed: 90 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -42,55 +42,55 @@ https://api.github.com/users/{user}/repos
4242
```
4343

4444
- `Retrofit`会将你的`api`封装成`Java`接口
45-
```java
46-
public interface GitHubService {
47-
@GET("users/{user}/repos")
48-
Call<List<Repo>> listRepos(@Path("user") String user);
49-
}
50-
```
45+
```java
46+
public interface GitHubService {
47+
@GET("users/{user}/repos")
48+
Call<List<Repo>> listRepos(@Path("user") String user);
49+
}
50+
```
5151

5252
- `Retrofit`类会生成一个`GitHubService`接口的实现类:
5353

54-
```java
55-
Retrofit retrofit = new Retrofit.Builder()
56-
.baseUrl("https://api.github.com/")
57-
.build();
58-
59-
GitHubService service = retrofit.create(GitHubService.class);
60-
```
54+
```java
55+
Retrofit retrofit = new Retrofit.Builder()
56+
.baseUrl("https://api.github.com/")
57+
.build();
58+
59+
GitHubService service = retrofit.create(GitHubService.class);
60+
```
6161

6262
- 从创建的`GithubService`类返回的每个`Call`对象调用后都可以创建一个同步或异步的网络请求:
6363

64-
```java
65-
Call<List<Repo>> repos = service.listRepos("CharonChui");
66-
```
64+
```java
65+
Call<List<Repo>> repos = service.listRepos("CharonChui");
66+
```
6767

6868
- 上面返回的`Call`其实并不是真正的数据结果,它更像一条指令,你需要执行它:
6969

70-
```java
71-
// 同步调用
72-
List<Repo> data = repos.execute();
73-
74-
// 异步调用
75-
repos.enqueue(new Callback<List<Repo>>() {
76-
@Override
77-
public void onResponse(Call<List<Repo>> call, Response<List<Repo>> response) {
78-
List<Repo> data = response.body();
79-
Log.i("@@@", "data size : " + (data == null ? "null" : data.size() + ""));
80-
}
81-
82-
@Override
83-
public void onFailure(Call<List<Repo>> call, Throwable t) {
84-
85-
}
86-
});
87-
```
70+
```java
71+
// 同步调用
72+
List<Repo> data = repos.execute();
73+
74+
// 异步调用
75+
repos.enqueue(new Callback<List<Repo>>() {
76+
@Override
77+
public void onResponse(Call<List<Repo>> call, Response<List<Repo>> response) {
78+
List<Repo> data = response.body();
79+
Log.i("@@@", "data size : " + (data == null ? "null" : data.size() + ""));
80+
}
81+
82+
@Override
83+
public void onFailure(Call<List<Repo>> call, Throwable t) {
84+
85+
}
86+
});
87+
```
8888

89-
那如何取消请求呢?
89+
那如何取消请求呢?
9090

91-
```java
92-
repos.cancel();
93-
```
91+
```java
92+
repos.cancel();
93+
```
9494

9595
上面这一部分代码,你要是拷贝运行后是运行不了的。
9696
当然了,因为木有`Repo`对象。但是添加`Repo`对象也是运行不了的。会报错。
@@ -126,7 +126,7 @@ Caused by: java.lang.IllegalArgumentException: Unable to create converter for ja
126126
at android.app.Activity.performCreate(Activity.java:5304)
127127
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
128128
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
129-
... 12 more
129+
... 12 more
130130
Caused by: java.lang.IllegalArgumentException: Could not locate ResponseBody converter for java.util.List<com.charon.retrofitdemo.Repo>.
131131
Tried:
132132
```
@@ -230,7 +230,7 @@ Url = "https://api.github.com/users/CharonChui/repos"
230230

231231
假设我们有一个分页查询的功能:
232232
```java
233-
@GET("/list")
233+
GET("/list")
234234
Call<ResponseBody> list(@Query("page") int page);
235235
```
236236

@@ -263,6 +263,7 @@ Call<User> updateUser(@Part("photo") RequestBody photo, @Part("description") Req
263263
```
264264

265265
`@Headers`
266+
---
266267

267268
可以通过`@Headers`来设置静态的请求头
268269

@@ -291,63 +292,64 @@ Call<User> getUser(@Header("Authorization") String authorization)
291292

292293
- 添加依赖
293294

294-
```java
295-
compile 'com.squareup.retrofit2:retrofit:2.1.0'
296-
compile 'com.squareup.retrofit2:converter-gson:2.1.0'// 支持gson
297-
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'// 支持rxjava
298-
299-
// rxjava part
300-
compile 'io.reactivex:rxandroid:1.2.1'
301-
compile 'io.reactivex:rxjava:1.2.3'
302-
```
295+
```java
296+
compile 'com.squareup.retrofit2:retrofit:2.1.0'
297+
compile 'com.squareup.retrofit2:converter-gson:2.1.0'// 支持gson
298+
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'// 支持rxjava
299+
300+
// rxjava part
301+
compile 'io.reactivex:rxandroid:1.2.1'
302+
compile 'io.reactivex:rxjava:1.2.3'
303+
```
303304

304305
- 修改`Retrofit`的配置,让其支持`RxJava`
305306

306-
```java
307-
Retrofit retrofit = new Retrofit.Builder()
308-
.baseUrl("https://api.github.com/")
309-
.addConverterFactory(GsonConverterFactory.create())
310-
.addCallAdapterFactory(RxJavaCallAdapterFactory.create()) // 支持RxJava
311-
.build();
312-
```
307+
```java
308+
Retrofit retrofit = new Retrofit.Builder()
309+
.baseUrl("https://api.github.com/")
310+
.addConverterFactory(GsonConverterFactory.create())
311+
.addCallAdapterFactory(RxJavaCallAdapterFactory.create()) // 支持RxJava
312+
.build();
313+
```
313314

314315
- 修改`GitHubService`,将返回值改为`Observable`,而不是`Call`
315316

316-
```java
317-
public interface GitHubService {
318-
@GET("users/{user}/repos")
319-
Observable<List<Repo>> listRepos(@Path("user") String user);
320-
}
321-
```
317+
```java
318+
public interface GitHubService {
319+
@GET("users/{user}/repos")
320+
Observable<List<Repo>> listRepos(@Path("user") String user);
321+
}
322+
```
322323

323324
- 执行部分
324-
```java
325-
GitHubService service = retrofit.create(GitHubService.class);
326-
327-
service.listRepos("CharonChui")
328-
.subscribeOn(Schedulers.newThread())
329-
.observeOn(AndroidSchedulers.mainThread())
330-
.subscribe(new Subscriber<List<Repo>>() {
331-
@Override
332-
public void onCompleted() {
333-
Log.i("@@@", "onCompleted");
334-
}
335-
336-
@Override
337-
public void onError(Throwable e) {
338-
Log.i("@@@", "onError : " + e.toString());
339-
}
340-
341-
@Override
342-
public void onNext(List<Repo> repos) {
343-
Log.i("@@@", "onNext : " + repos.size());
344-
Toast.makeText(MainActivity.this, "size : " + repos.size(), Toast.LENGTH_SHORT).show();
345-
}
346-
});
347-
```
348-
349325

350-
`RxJava`+`Retrofit`形式的时候,`Retrofit`把请求封装进`Observable`在请求结束后调用 `onNext()`或在请求失败后调用`onError()`
326+
```java
327+
GitHubService service = retrofit.create(GitHubService.class);
328+
329+
service.listRepos("CharonChui")
330+
.subscribeOn(Schedulers.newThread())
331+
.observeOn(AndroidSchedulers.mainThread())
332+
.subscribe(new Subscriber<List<Repo>>() {
333+
@Override
334+
public void onCompleted() {
335+
Log.i("@@@", "onCompleted");
336+
}
337+
338+
@Override
339+
public void onError(Throwable e) {
340+
Log.i("@@@", "onError : " + e.toString());
341+
}
342+
343+
@Override
344+
public void onNext(List<Repo> repos) {
345+
Log.i("@@@", "onNext : " + repos.size());
346+
Toast.makeText(MainActivity.this, "size : " + repos.size(), Toast.LENGTH_SHORT).show();
347+
}
348+
});
349+
```
350+
351+
352+
`RxJava`+`Retrofit`形式的时候,`Retrofit`把请求封装进`Observable`在请求结束后调用 `onNext()`或在请求失败后调用`onError()`
351353

352354

353355
`Proguard`配置

0 commit comments

Comments
 (0)