Android: RxJava with OkHttp: 如何进行依赖于第一个的第二个调用?
Android: RxJava with OkHttp: How to make second call which is dependent on first?
我没有使用 Retrofit。
我必须进行第一次调用以获取 auth cookie,然后我需要使用相同的 OkHttclient 客户端进行第二次调用(实际上我假设由于 auth cookie 而需要相同的客户端)。因此,当第一个 http 调用完成后,我需要使用相同的客户端变量再次调用另一个 url。我怎样才能干净地实现这一目标?
我认为我可以使用 .concatWith(请参阅下面的代码)但我不确定这是否正确,因为我找不到任何可靠的示例。
Subscription subscription = Observable.create(new Observable.OnSubscribe<Response>() {
OkHttpClient client = new OkHttpClient.Builder()
.cookieJar(new CookieJar() {
private final HashMap<HttpUrl, List<Cookie>> cookieStore = new HashMap<>();
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
cookieStore.put(url, cookies);
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
List<Cookie> cookies = cookieStore.get(url);
return cookies != null ? cookies : new ArrayList<Cookie>();
}
})
.build();
@Override
public void call(Subscriber<? super Response> subscriber) {
try {
HttpUrl.Builder urlBuilder = HttpUrl.parse(AUTHTOKEN_URL).newBuilder();
urlBuilder.addQueryParameter("username", AUTHTOKEN_USERNAME);
urlBuilder.addQueryParameter("password", AUTHTOKEN_PASSWORD);
String url = urlBuilder.build().toString();
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
subscriber.onNext(response);
subscriber.onCompleted();
if (!response.isSuccessful()){
subscriber.onError(new Exception("error"));
}
}
catch (IOException e) {
subscriber.onError(e);
}
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.concatWith(
//what code goes here??
)
.subscribe(new Subscriber<Response>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Response response) {
}
});
return subscription;
}
查看代码RxJava之道
//Create your customized OkHttpClient
final OkHttpClient client = new OkHttpClient.Builder()
.cookieJar(new CookieJar() {
private final HashMap<HttpUrl, List<Cookie>> cookieStore = new HashMap<>();
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
cookieStore.put(url, cookies);
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
List<Cookie> cookies = cookieStore.get(url);
return cookies != null ? cookies : new ArrayList<Cookie>();
}
})
.build();
Observable<AuthResponse> authObservable = Observable.fromCallable(new Func0<AuthResponse>() {
@Override
public AuthResponse call() {
return doOkHttpAuthOperation(client);//You can do with your auth cookie and Login Credentials
}
});
Observable<NormalResponse> normalObservable = authObservable.map(new Func1<AuthResponse, NormalResponse>() {
@Override
public NormalResponse call(AuthResponse authResponse) {
return doOkHttpNormalOperation(client);//Normal API call
}
});
Subscription subscription = normalObservable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<NormalResponse>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
//if Any Error occurred
}
@Override
public void onNext(NormalResponse normalResponse) {
//Authenticated Response
}
});
我没有使用 Retrofit。 我必须进行第一次调用以获取 auth cookie,然后我需要使用相同的 OkHttclient 客户端进行第二次调用(实际上我假设由于 auth cookie 而需要相同的客户端)。因此,当第一个 http 调用完成后,我需要使用相同的客户端变量再次调用另一个 url。我怎样才能干净地实现这一目标?
我认为我可以使用 .concatWith(请参阅下面的代码)但我不确定这是否正确,因为我找不到任何可靠的示例。
Subscription subscription = Observable.create(new Observable.OnSubscribe<Response>() {
OkHttpClient client = new OkHttpClient.Builder()
.cookieJar(new CookieJar() {
private final HashMap<HttpUrl, List<Cookie>> cookieStore = new HashMap<>();
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
cookieStore.put(url, cookies);
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
List<Cookie> cookies = cookieStore.get(url);
return cookies != null ? cookies : new ArrayList<Cookie>();
}
})
.build();
@Override
public void call(Subscriber<? super Response> subscriber) {
try {
HttpUrl.Builder urlBuilder = HttpUrl.parse(AUTHTOKEN_URL).newBuilder();
urlBuilder.addQueryParameter("username", AUTHTOKEN_USERNAME);
urlBuilder.addQueryParameter("password", AUTHTOKEN_PASSWORD);
String url = urlBuilder.build().toString();
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
subscriber.onNext(response);
subscriber.onCompleted();
if (!response.isSuccessful()){
subscriber.onError(new Exception("error"));
}
}
catch (IOException e) {
subscriber.onError(e);
}
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.concatWith(
//what code goes here??
)
.subscribe(new Subscriber<Response>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Response response) {
}
});
return subscription;
}
查看代码RxJava之道
//Create your customized OkHttpClient
final OkHttpClient client = new OkHttpClient.Builder()
.cookieJar(new CookieJar() {
private final HashMap<HttpUrl, List<Cookie>> cookieStore = new HashMap<>();
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
cookieStore.put(url, cookies);
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
List<Cookie> cookies = cookieStore.get(url);
return cookies != null ? cookies : new ArrayList<Cookie>();
}
})
.build();
Observable<AuthResponse> authObservable = Observable.fromCallable(new Func0<AuthResponse>() {
@Override
public AuthResponse call() {
return doOkHttpAuthOperation(client);//You can do with your auth cookie and Login Credentials
}
});
Observable<NormalResponse> normalObservable = authObservable.map(new Func1<AuthResponse, NormalResponse>() {
@Override
public NormalResponse call(AuthResponse authResponse) {
return doOkHttpNormalOperation(client);//Normal API call
}
});
Subscription subscription = normalObservable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<NormalResponse>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
//if Any Error occurred
}
@Override
public void onNext(NormalResponse normalResponse) {
//Authenticated Response
}
});