Search in sources :

Example 1 with Request

use of com.yanzhenjie.nohttp.rest.Request 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)1 IBasicRequest (com.yanzhenjie.nohttp.IBasicRequest)1 RedirectHandler (com.yanzhenjie.nohttp.RedirectHandler)1 Request (com.yanzhenjie.nohttp.rest.Request)1