Search in sources :

Example 6 with Headers

use of com.yanzhenjie.nohttp.Headers 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)6 IBasicRequest (com.yanzhenjie.nohttp.IBasicRequest)2 RedirectHandler (com.yanzhenjie.nohttp.RedirectHandler)2 Connection (com.yanzhenjie.nohttp.Connection)1 HttpConnection (com.yanzhenjie.nohttp.HttpConnection)1 NetworkError (com.yanzhenjie.nohttp.error.NetworkError)1 ServerError (com.yanzhenjie.nohttp.error.ServerError)1 StorageReadWriteError (com.yanzhenjie.nohttp.error.StorageReadWriteError)1 StorageSpaceNotEnoughError (com.yanzhenjie.nohttp.error.StorageSpaceNotEnoughError)1 TimeoutError (com.yanzhenjie.nohttp.error.TimeoutError)1 URLError (com.yanzhenjie.nohttp.error.URLError)1 UnKnownHostError (com.yanzhenjie.nohttp.error.UnKnownHostError)1 Request (com.yanzhenjie.nohttp.rest.Request)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 SocketTimeoutException (java.net.SocketTimeoutException)1