Weitere ähnliche Inhalte Ähnlich wie Rx java x retrofit (20) Rx java x retrofit14. 基本的な書き方
String name = “shun";
Subscription subscription = Observable.from(new String[]{name}).subscribe(new Action1<String>() {
@Override
public void call(String string) {
String name = "nakahara " + string;
System.out.print(name);
}
});
subscription.unsubscribe();
15. 基本的な処理を便利に!?
Observable.OnSubscribe<String> onSubscribe = new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
final String name = "shun"; // 重い処理などをここで行う。
String fullName = "nakahara " + name;
subscriber.onNext(fullName);
subscriber.onCompleted();
}
};
Subscription subscription = Observable.create(onSubscribe).subscribe(new Action1<String>() {
@Override
public void call(String string) {
System.out.print(string);
}
});
subscription.unsubscribe();
16. 非同期処理
Observable.OnSubscribe<String> onSubscribe = new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
// 通信処理
boolean isSuccess = false;
String response = "response";
if (isSuccess) {
subscriber.onNext(response);
subscriber.onCompleted();
} else {
subscriber.onError(new NetworkErrorException("connect error"));
}
}
};
Observable<String> stringObservable = Observable.create(onSubscribe);
stringObservable.subscribe(new Observer<String>() {
@Override
public void onCompleted() {
// network completed
}
@Override
public void onError(Throwable e) {
// network error message
}
@Override
public void onNext(String string) {
// network success response
}
});
17. 同期処理を書きたい時
Observable.OnSubscribe<String> onSubscribe = new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
// 通信処理
boolean isSuccess = false;
String response = "response";
if (isSuccess) {
subscriber.onNext(response);
subscriber.onCompleted();
} else {
subscriber.onError(new NetworkErrorException("connect error"));
}
}
};
Observable<String> stringObservable = Observable.create(onSubscribe);
String response = stringObservable.toBlocking().single();
if (response == null) {
// error
} else {
// success
}
19. メソッドに切す
public Observable<String> request() {
Observable.OnSubscribe<String> onSubscribe = new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
// 通信処理
boolean isSuccess = false;
String response = "response";
if (isSuccess) {
subscriber.onNext(response);
subscriber.onCompleted();
} else {
subscriber.onError(new NetworkErrorException("connect error"));
}
}
};
return Observable.create(onSubscribe);
}
21. 同期処理
String response = request().toBlocking().single();
if (response == null) {
// network error
} else {
// response call back
}
28. Handler Schedulers
Handler handler = new Handler();
request().subscribeOn(Schedulers.newThread())
.observeOn(HandlerScheduler.from(handler))
.subscribe(new Observer<String>() {
@Override
public void onCompleted() {
// network completed
// handler thread
}
@Override
public void onError(Throwable e) {
// network error
// handler thread
}
@Override
public void onNext(String s) {
// response call back
// handler thread
}
});
31. Retrofit
✤ A Type safe HTTP client for Android and Java
✤ Android の為の REST 通信を手軽に実装できる!?
✤ Retrofit 2.0.0-bata2 を利用します。
33. Gradle の準備
// Retrofit
compile ‘com.squareup.retrofit:retrofit:2.0.0-beta2'
// gson
compile ‘com.google.code.gson:gson:2.4'
// Converter Gson
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
34. Proguard の準備
# Retrofit Proguard
-dontwarn retrofit.**
-keep class retrofit.** { *; }
-keepattributes Signature
-keepattributes Exceptions
36. Model Class の作成
public class SignIn {
@SerializedName("access_token")
public final String accessToken;
@SerializedName("user_id")
public final int userId;
@SerializedName("group_id")
public final int group_id;
public SignIn(String accessToken, int userId, int group_id) {
this.accessToken = accessToken;
this.userId = userId;
this.group_id = group_id;
}
}
37. Service Interface 作成
public interface UsersService {
@FormUrlEncoded
@POST("/v3/users/sign_in")
Call<SignIn> signIn(@Field("email") String email, @Field("password") String password);
}
38. Service Interface 作成
public interface UsersService {
@FormUrlEncoded
@POST("/v3/users/sign_in")
Call<SignIn> signIn(@Field("email") String email, @Field("password") String password);
}
39. 非同期処理
// Gson Converter Factory
GsonConverterFactory factory = GsonConverterFactory.create(new Gson());
// Retrofit
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(factory)
.build();
// Users Service
UsersService usersService = retrofit.create(UsersService.class);
// Call
Call<SignIn> call = usersService.signIn(Email, Password);
call.enqueue(new Callback<SignIn>() {
@Override
public void onResponse(Response<SignIn> response, Retrofit retrofit) {
// network success
}
@Override
public void onFailure(Throwable t) {
// network error
}
});
40. 同期処理
// Gson Converter Factory
GsonConverterFactory factory = GsonConverterFactory.create(new Gson());
// Retrofit
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(factory)
.build();
// Users Service
UsersService usersService = retrofit.create(UsersService.class);
// Call
Call<SignIn> call = usersService.signIn(Email, Password);
try {
Response<SignIn> response = call.execute();
} catch (IOException e) {
e.printStackTrace();
}
45. 非同期処理(RxJava版)
// Gson Converter Factory
GsonConverterFactory factory = GsonConverterFactory.create(new Gson());
// RxJava Call Adapter
RxJavaCallAdapterFactory adapterFactory = RxJavaCallAdapterFactory.create();
// Retrofit
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(factory)
.addCallAdapterFactory(adapterFactory)
.build();
// Users Service
UsersService usersService = retrofit.create(UsersService.class);
// User Service
Observable<SignIn> observable = usersService.signIn(Email, Password);
observable.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<SignIn>() {
@Override
public void onCompleted() {
// network completed
}
@Override
public void onError(Throwable e) {
// network error
}
@Override
public void onNext(SignIn signIn) {
// network success
}
});
46. 同期処理(RxJava版)
// Gson Converter Factory
GsonConverterFactory factory = GsonConverterFactory.create(new Gson());
// RxJava Call Adapter
RxJavaCallAdapterFactory adapterFactory = RxJavaCallAdapterFactory.create();
// Retrofit
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(factory)
.addCallAdapterFactory(adapterFactory)
.build();
// Users Service
UsersService usersService = retrofit.create(UsersService.class);
// Call
Observable<SignIn> observable = usersService.signIn(Email, Password);
SignIn signIn = observable
.subscribeOn(Schedulers.newThread())
.toBlocking()
.single();
if (signIn == null) {
// network error
} else {
// network success
}
48. Retrofit でハマったこと
✤ Twitter Kit では Retrofit 1.8 を使っている
✤ Retrofit 1.8-1.9 と Retrofit 2.0 で設定の変更
✤ Retrofit 2.0 では、RxJava の Call Adapter が必要