FileDocCategorySizeDatePackage
WebResourceResponse.javaAPI DocAndroid 5.1 API6888Thu Mar 12 22:22:10 GMT 2015android.webkit

WebResourceResponse

public class WebResourceResponse extends Object
Encapsulates a resource response. Applications can return an instance of this class from {@link WebViewClient#shouldInterceptRequest} to provide a custom response when the WebView requests a particular resource.

Fields Summary
private String
mMimeType
private String
mEncoding
private int
mStatusCode
private String
mReasonPhrase
private Map
mResponseHeaders
private InputStream
mInputStream
Constructors Summary
public WebResourceResponse(String mimeType, String encoding, InputStream data)
Constructs a resource response with the given MIME type, encoding, and input stream. Callers must implement {@link InputStream#read(byte[]) InputStream.read(byte[])} for the input stream.

param
mimeType the resource response's MIME type, for example text/html
param
encoding the resource response's encoding
param
data the input stream that provides the resource response's data

        mMimeType = mimeType;
        mEncoding = encoding;
        mInputStream = data;
    
public WebResourceResponse(String mimeType, String encoding, int statusCode, String reasonPhrase, Map responseHeaders, InputStream data)
Constructs a resource response with the given parameters. Callers must implement {@link InputStream#read(byte[]) InputStream.read(byte[])} for the input stream.

param
mimeType the resource response's MIME type, for example text/html
param
encoding the resource response's encoding
param
statusCode the status code needs to be in the ranges [100, 299], [400, 599]. Causing a redirect by specifying a 3xx code is not supported.
param
reasonPhrase the phrase describing the status code, for example "OK". Must be non-null and not empty.
param
responseHeaders the resource response's headers represented as a mapping of header name -> header value.
param
data the input stream that provides the resource response's data

        this(mimeType, encoding, data);
        setStatusCodeAndReasonPhrase(statusCode, reasonPhrase);
        setResponseHeaders(responseHeaders);
    
Methods Summary
public java.io.InputStreamgetData()
Gets the input stream that provides the resource response's data.

return
the input stream that provides the resource response's data

        return mInputStream;
    
public java.lang.StringgetEncoding()
Gets the resource response's encoding.

return
the resource response's encoding

        return mEncoding;
    
public java.lang.StringgetMimeType()
Gets the resource response's MIME type.

return
the resource response's MIME type

        return mMimeType;
    
public java.lang.StringgetReasonPhrase()
Gets the description of the resource response's status code.

return
the description of the resource response's status code.

        return mReasonPhrase;
    
public java.util.MapgetResponseHeaders()
Gets the headers for the resource response.

return
the headers for the resource response.

        return mResponseHeaders;
    
public intgetStatusCode()
Gets the resource response's status code.

return
the resource response's status code.

        return mStatusCode;
    
public voidsetData(java.io.InputStream data)
Sets the input stream that provides the resource response's data. Callers must implement {@link InputStream#read(byte[]) InputStream.read(byte[])}.

param
data the input stream that provides the resource response's data

        mInputStream = data;
    
public voidsetEncoding(java.lang.String encoding)
Sets the resource response's encoding, for example UTF-8. This is used to decode the data from the input stream.

param
encoding the resource response's encoding

        mEncoding = encoding;
    
public voidsetMimeType(java.lang.String mimeType)
Sets the resource response's MIME type, for example text/html.

param
mimeType the resource response's MIME type

        mMimeType = mimeType;
    
public voidsetResponseHeaders(java.util.Map headers)
Sets the headers for the resource response.

param
headers mapping of header name -> header value.

        mResponseHeaders = headers;
    
public voidsetStatusCodeAndReasonPhrase(int statusCode, java.lang.String reasonPhrase)
Sets the resource response's status code and reason phrase.

param
statusCode the status code needs to be in the ranges [100, 299], [400, 599]. Causing a redirect by specifying a 3xx code is not supported.
param
reasonPhrase the phrase describing the status code, for example "OK". Must be non-null and not empty.

        if (statusCode < 100)
            throw new IllegalArgumentException("statusCode can't be less than 100.");
        if (statusCode > 599)
            throw new IllegalArgumentException("statusCode can't be greater than 599.");
        if (statusCode > 299 && statusCode < 400)
            throw new IllegalArgumentException("statusCode can't be in the [300, 399] range.");
        if (reasonPhrase == null)
            throw new IllegalArgumentException("reasonPhrase can't be null.");
        if (reasonPhrase.trim().isEmpty())
            throw new IllegalArgumentException("reasonPhrase can't be empty.");
        for (int i = 0; i < reasonPhrase.length(); i++) {
            int c = reasonPhrase.charAt(i);
            if (c > 0x7F) {
                throw new IllegalArgumentException(
                        "reasonPhrase can't contain non-ASCII characters.");
            }
        }
        mStatusCode = statusCode;
        mReasonPhrase = reasonPhrase;