在我的响应中获取 302 重定向的 HttpException
Getting HttpException on 302 redirect in my response
Api.initOAuthDiscoverConfig("url")
.flatMap(new Function<String, ObservableSource<String>>() {
@Override
public ObservableSource<String> apply(String jsonString) throws Exception {
Log.i(TAG, " initOAuthDiscoverConfig " + jsonString);
JSONObject jsonObject = new JSONObject(jsonString);
String nonce = UUID.randomUUID().toString();
String state = nonce.split("-")[0];
return Api.initOAuthLogin(jsonObject.getString("authorization_endpoint") + "?" +
"scope=" +
"response_type=code&\n" +
"client_id=" +
"redirect_uri=" +
"state=" + getAlphaNumericString(12) + "&" +
"nonce=" + getAlphaNumericString(10) + "&" +
"prompt=login");
}
}).flatMap(new Function<String, ObservableSource<String>>() {
@Override
public ObservableSource<String> apply(String authLoginString) throws Exception {
Log.i(TAG, " initOAuthLogin " + authLoginString);
RequestBody requestBody = RequestBody.create("loginForm=loginForm&" +
"javax.faces.ViewState=stateless&" +
"loginForm:username=" +
"loginForm:password=" +
"loginForm:loginButton=Sign In", MediaType.parse("Content-Type:application/x-www-form-urlencoded*"));
return Api.OAuthLoginForm("url", requestBody);
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableObserver<String>() {
@Override
public void onNext(String loginResponseString) {
Log.i(TAG, " OAuthLoginForm " + loginResponseString);
hideProgress();
Toast.makeText(this, "onNext called", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Throwable e) {
hideProgress();
showAlertDialog(getString(R.string.something_went_wrong), false);
Toast.makeText(this, "onError called", Toast.LENGTH_SHORT).show();
HttpException error = (HttpException)e;
String errorBody = error.response().errorBody().toString();
Log.d(TAG, "onError: "+ errorBody);
}
@Override
public void onComplete() {
}
});
它每次都会进入 onError,而不是 subscribeWith 中的 onNext。不知道哪里出了问题。感谢任何帮助。
根据我的理解,我认为 DisposableObserver 中存在一些问题。 (也许我错了)。
P.S:- 没有多少 Rxjava 经验
根据实现onErrorResumeNext
功能的评论中的讨论,解决了问题。
Api.initOAuthDiscoverConfig("url")
.flatMap(new Function<String, ObservableSource<String>>() {
@Override
public ObservableSource<String> apply(String jsonString) throws Exception {
Log.i(TAG, " initOAuthDiscoverConfig " + jsonString);
JSONObject jsonObject = new JSONObject(jsonString);
String nonce = UUID.randomUUID().toString();
String state = nonce.split("-")[0];
return Api.initOAuthLogin(jsonObject.getString("authorization_endpoint") + "?" +
"scope=" +
"response_type=code&\n" +
"client_id=" +
"redirect_uri=" +
"state=" + getAlphaNumericString(12) + "&" +
"nonce=" + getAlphaNumericString(10) + "&" +
"prompt=login");
}
}).flatMap(new Function<String, ObservableSource<String>>() {
@Override
public ObservableSource<String> apply(String authLoginString) throws Exception {
Log.i(TAG, " initOAuthLogin " + authLoginString);
RequestBody requestBody = RequestBody.create("loginForm=loginForm&" +
"javax.faces.ViewState=stateless&" +
"loginForm:username=" +
"loginForm:password=" +
"loginForm:loginButton=Sign In", MediaType.parse("Content-Type:application/x-www-form-urlencoded*"));
return Api.OAuthLoginForm("url", requestBody);
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableObserver<String>() {
@Override
public void onNext(String loginResponseString) {
Log.i(TAG, " OAuthLoginForm " + loginResponseString);
hideProgress();
Toast.makeText(this, "onNext called", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Throwable e) {
hideProgress();
showAlertDialog(getString(R.string.something_went_wrong), false);
Toast.makeText(this, "onError called", Toast.LENGTH_SHORT).show();
HttpException error = (HttpException)e;
String errorBody = error.response().errorBody().toString();
Log.d(TAG, "onError: "+ errorBody);
}
@Override
public void onComplete() {
}
});
它每次都会进入 onError,而不是 subscribeWith 中的 onNext。不知道哪里出了问题。感谢任何帮助。
根据我的理解,我认为 DisposableObserver 中存在一些问题。 (也许我错了)。
P.S:- 没有多少 Rxjava 经验
根据实现onErrorResumeNext
功能的评论中的讨论,解决了问题。