Search in sources :

Example 1 with RedirectHandler

use of com.yanzhenjie.nohttp.RedirectHandler in project NoHttp by yanzhenjie.

the class RedirectActivity method requestDisAllowRedirect.

/**
     * 不允许重定向的请求
     */
private void requestDisAllowRedirect() {
    Request<String> request = NoHttp.createStringRequest(Constants.URL_NOHTTP_REDIRECT_BAIDU);
    request.setRedirectHandler(new RedirectHandler() {

        @Override
        public IBasicRequest onRedirect(Headers responseHeaders) {
            // 不允许重定向时此方法不会被调用。
            return null;
        }

        @Override
        public boolean isDisallowedRedirect(Headers responseHeaders) {
            // 返回true代表不允许重定向。
            return true;
        }
    });
    request(0, request, this, false, true);
}
Also used : RedirectHandler(com.yanzhenjie.nohttp.RedirectHandler) Headers(com.yanzhenjie.nohttp.Headers) IBasicRequest(com.yanzhenjie.nohttp.IBasicRequest)

Example 2 with RedirectHandler

use of com.yanzhenjie.nohttp.RedirectHandler in project NoHttp by yanzhenjie.

the class RedirectActivity method requestAllowRedirect.

/**
     * 允许重定向的请求
     */
public void requestAllowRedirect() {
    final Request<String> request = NoHttp.createStringRequest(Constants.URL_NOHTTP_REDIRECT_BAIDU);
    request.setRedirectHandler(new RedirectHandler() {

        @Override
        public IBasicRequest onRedirect(Headers responseHeaders) {
            // 允许重定向时这个方法会被调用
            // 1. 返回null,NoHttp会自动拷贝父请求的请求方法和代理自动请求,不会拷贝其他属性。
            // 2. 返回非null,会把这个新请求的数据交给父请求去解析。
            Request<String> redirectRequest = NoHttp.createStringRequest(responseHeaders.getLocation());
            // 为了防止嵌套重定向,这里可以每个子级请求都监听
            redirectRequest.setRedirectHandler(this);
            return redirectRequest;
        }

        // 是否不允许重定向。
        @Override
        public boolean isDisallowedRedirect(Headers responseHeaders) {
            // 返回false表示允许重定向
            return false;
        }
    });
    request(0, request, this, false, true);
}
Also used : RedirectHandler(com.yanzhenjie.nohttp.RedirectHandler) Headers(com.yanzhenjie.nohttp.Headers) IBasicRequest(com.yanzhenjie.nohttp.IBasicRequest) Request(com.yanzhenjie.nohttp.rest.Request) IBasicRequest(com.yanzhenjie.nohttp.IBasicRequest)

Aggregations

Headers (com.yanzhenjie.nohttp.Headers)2 IBasicRequest (com.yanzhenjie.nohttp.IBasicRequest)2 RedirectHandler (com.yanzhenjie.nohttp.RedirectHandler)2 Request (com.yanzhenjie.nohttp.rest.Request)1