Skip to content

Commit 16626a9

Browse files
author
alafighting
committed
增加对Retrofit的支持
1 parent 525dc57 commit 16626a9

File tree

15 files changed

+2361
-17
lines changed

15 files changed

+2361
-17
lines changed

app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ dependencies {
2929
compile project(':library')
3030

3131
compile 'com.android.support:appcompat-v7:24.0.0'
32+
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
33+
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
3234
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.im4j.kakacache.rxjava.demo;
2+
3+
import java.util.List;
4+
5+
import retrofit2.http.GET;
6+
import retrofit2.http.Path;
7+
8+
/**
9+
* @version alafighting 2016-07
10+
*/
11+
public interface GitHubService {
12+
13+
@GET("users/{user}/repos")
14+
rx.Observable<List<GithubRepoEntity>> listRepos(@Path("user") String user);
15+
16+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.im4j.kakacache.rxjava.demo;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
/**
6+
* @version alafighting 2016-07
7+
*/
8+
public class GithubRepoEntity {
9+
10+
private String id;
11+
private String name;
12+
@SerializedName("full_name")
13+
private String fullName;
14+
private String description;
15+
16+
public GithubRepoEntity() {
17+
}
18+
public GithubRepoEntity(String id, String name, String fullName, String description) {
19+
this.id = id;
20+
this.name = name;
21+
this.fullName = fullName;
22+
this.description = description;
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return "GithubRepoEntity{" +
28+
"id='" + id + '\'' +
29+
", name='" + name + '\'' +
30+
", fullName='" + fullName + '\'' +
31+
", description='" + description + '\'' +
32+
'}';
33+
}
34+
35+
public String getId() {
36+
return id;
37+
}
38+
39+
public void setId(String id) {
40+
this.id = id;
41+
}
42+
43+
public String getName() {
44+
return name;
45+
}
46+
47+
public void setName(String name) {
48+
this.name = name;
49+
}
50+
51+
public String getFullName() {
52+
return fullName;
53+
}
54+
55+
public void setFullName(String fullName) {
56+
this.fullName = fullName;
57+
}
58+
59+
public String getDescription() {
60+
return description;
61+
}
62+
63+
public void setDescription(String description) {
64+
this.description = description;
65+
}
66+
}

app/src/main/java/com/im4j/kakacache/rxjava/demo/MainActivity.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@
66
import android.util.Log;
77
import android.widget.Button;
88

9-
import com.im4j.kakacache.rxjava.netcache.RxRemoteCache;
9+
import com.im4j.kakacache.rxjava.common.utils.LogUtils;
10+
import com.im4j.kakacache.rxjava.netcache.KakaCache;
11+
import com.im4j.kakacache.rxjava.netcache.KakaRetrofit;
1012
import com.im4j.kakacache.rxjava.netcache.strategy.CacheAndRemoteStrategy;
13+
import com.im4j.kakacache.rxjava.netcache.strategy.FirstCacheStrategy;
1114

1215
import java.util.concurrent.TimeUnit;
1316

17+
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
18+
import retrofit2.converter.gson.GsonConverterFactory;
1419
import rx.Observable;
1520
import rx.Subscriber;
21+
import rx.schedulers.Schedulers;
1622

1723
/**
1824
* Demo主界面
@@ -21,23 +27,40 @@
2127
public class MainActivity extends AppCompatActivity {
2228
static final String KEY_CACHE = "key_cache";
2329

24-
private Button btnTest;
30+
private Button btnTestCache;
31+
private Button btnTestRetrofit;
2532

2633
@Override
2734
protected void onCreate(@Nullable Bundle savedInstanceState) {
2835
super.onCreate(savedInstanceState);
2936
setContentView(R.layout.actiity_main);
3037

31-
btnTest = (Button) findViewById(R.id.btn_test_cache);
32-
btnTest.setOnClickListener(view -> {
33-
remote().compose(RxRemoteCache.transformer(KEY_CACHE, new CacheAndRemoteStrategy())).subscribe(data -> {
38+
btnTestCache = (Button) findViewById(R.id.btn_test_cache);
39+
btnTestCache.setOnClickListener(view -> {
40+
remote().compose(KakaCache.transformer(KEY_CACHE, new CacheAndRemoteStrategy())).subscribe(data -> {
3441
Log.e("main", "next data=" + data);
3542
}, error -> {
3643
Log.e("main", "error", error);
3744
}, () -> {
3845
Log.e("main", "completed");
3946
});
4047
});
48+
49+
btnTestRetrofit = (Button) findViewById(R.id.btn_test_retrofit);
50+
btnTestRetrofit.setOnClickListener(view -> {
51+
KakaRetrofit retrofit = new KakaRetrofit.Builder()
52+
.baseUrl("https://api.github.com/")
53+
.addConverterFactory(GsonConverterFactory.create())
54+
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
55+
.build();
56+
57+
GitHubService service = retrofit.create(GitHubService.class);
58+
service.listRepos("octocat").compose(KakaCache.transformer(KEY_CACHE, new FirstCacheStrategy())).subscribeOn(Schedulers.io()).subscribe(data -> {
59+
LogUtils.e(data);
60+
}, error -> {
61+
LogUtils.e(error);
62+
});
63+
});
4164
}
4265

4366
private rx.Observable<String> remote() {
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<RelativeLayout
2+
<LinearLayout
33
xmlns:android="http://schemas.android.com/apk/res/android"
44
android:layout_width="match_parent"
5-
android:layout_height="match_parent">
5+
android:layout_height="match_parent"
6+
android:orientation="vertical"
7+
android:gravity="center">
68

79
<Button
810
android:id="@+id/btn_test_cache"
911
android:layout_width="wrap_content"
1012
android:layout_height="wrap_content"
11-
android:layout_centerHorizontal="true"
12-
android:text="测试缓存"/>
13+
android:text="测试普通缓存"/>
1314

14-
</RelativeLayout>
15+
<Button
16+
android:id="@+id/btn_test_retrofit"
17+
android:layout_width="wrap_content"
18+
android:layout_height="wrap_content"
19+
android:textAllCaps="false"
20+
android:text="测试Retrofit缓存"/>
21+
22+
</LinearLayout>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.im4j.kakacache.rxjava.common.utils;
2+
3+
import android.util.Log;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.Collection;
8+
import java.util.Iterator;
9+
import java.util.List;
10+
11+
/**
12+
* @version alafighting 2016-07
13+
*/
14+
public final class LogUtils {
15+
private LogUtils() {
16+
}
17+
18+
public static void e(Object obj) {
19+
StackTraceElement element = new Throwable().getStackTrace()[1];
20+
String className = element.getClassName();
21+
className = className.substring(className.lastIndexOf(".") + 1);
22+
String tag = className+'.'+element.getMethodName()+'('+element.getFileName()+':'+element.getLineNumber()+')';
23+
String message = toString(obj);
24+
25+
Log.e("[KakaCache]", tag+"\t"+message);
26+
}
27+
28+
private static String toString(Object obj) {
29+
if (obj == null) {
30+
return "[null]";
31+
}
32+
if (obj instanceof Throwable) {
33+
return Log.getStackTraceString((Throwable) obj);
34+
}
35+
if (obj instanceof Collection) {
36+
Iterator it = ((Collection) obj).iterator();
37+
if (! it.hasNext())
38+
return "[]";
39+
40+
StringBuilder sb = new StringBuilder();
41+
sb.append('[');
42+
for (;;) {
43+
Object e = it.next();
44+
sb.append(e);
45+
if (! it.hasNext())
46+
return sb.append(']').toString();
47+
sb.append(',').append('\n').append(' ');
48+
}
49+
}
50+
return String.valueOf(obj);
51+
}
52+
53+
}

0 commit comments

Comments
 (0)