WebResourceResponseCompat


public class WebResourceResponseCompat


Compatibility version of WebResourceResponse. This class can be used as a direct replacement of the framework class.

Summary

Public constructors

WebResourceResponseCompat(
    @NonNull String mimeType,
    @Nullable String encoding,
    @Nullable InputStream data
)

Constructor matching WebResourceResponse

WebResourceResponseCompat(
    @NonNull String mimeType,
    @Nullable String encoding,
    int statusCode,
    @NonNull String reasonPhrase,
    @Nullable Map<StringString> responseHeaders,
    @Nullable InputStream data
)

Constructor matching WebResourceResponse

Public methods

@NonNull List<String>
@RequiresFeature(name = WebViewFeature.COOKIE_INTERCEPT, enforcement = "androidx.webkit" + ".WebViewFeature#isFeatureSupported")
getCookies()

Get the list of cookies set by setCookies or an empty list.

@Nullable InputStream
@Nullable String
@NonNull String
@Nullable String
@NonNull Map<StringString>
int
void
@RequiresFeature(name = WebViewFeature.COOKIE_INTERCEPT, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setCookies(@NonNull List<String> cookies)

Set the list of Set-Cookie header values applicable to this response.

void
void
void
void
void
setStatusCodeAndReasonPhrase(
    int statusCode,
    @NonNull String reasonPhrase
)
@NonNull WebResourceResponse

Convert this object to a WebResourceResponse that can be used as return value in shouldInterceptRequest and shouldInterceptRequest.

static @NonNull WebResourceResponseCompat

Convert a WebResourceResponse to the compat version.

Public constructors

WebResourceResponseCompat

public WebResourceResponseCompat(
    @NonNull String mimeType,
    @Nullable String encoding,
    @Nullable InputStream data
)

Constructor matching WebResourceResponse

WebResourceResponseCompat

public WebResourceResponseCompat(
    @NonNull String mimeType,
    @Nullable String encoding,
    int statusCode,
    @NonNull String reasonPhrase,
    @Nullable Map<StringString> responseHeaders,
    @Nullable InputStream data
)

Constructor matching WebResourceResponse

Public methods

getCookies

@RequiresFeature(name = WebViewFeature.COOKIE_INTERCEPT, enforcement = "androidx.webkit"
            + ".WebViewFeature#isFeatureSupported")
public @NonNull List<StringgetCookies()

Get the list of cookies set by setCookies or an empty list.

getData

public @Nullable InputStream getData()
See also
getData

getEncoding

public @Nullable String getEncoding()
See also
getEncoding

getMimeType

public @NonNull String getMimeType()
See also
getMimeType

getReasonPhrase

public @Nullable String getReasonPhrase()
See also
getReasonPhrase

getResponseHeaders

public @NonNull Map<StringStringgetResponseHeaders()

getStatusCode

public int getStatusCode()
See also
getStatusCode

setCookies

@RequiresFeature(name = WebViewFeature.COOKIE_INTERCEPT, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public void setCookies(@NonNull List<String> cookies)

Set the list of Set-Cookie header values applicable to this response.

Note that these values will only be used by WebView if isIncludeCookiesOnShouldInterceptRequestEnabled is true, and by service workers if isIncludeCookiesOnShouldInterceptRequestEnabled is true. Otherwise the values will be ignored.

It is safe to use this method even if the map of response headers provided in the constructor or through setResponseHeaders already contains a Set-Cookie value. In such cases, all values will be applied. However, it is recommended to only use this method to supply Set-Cookie header values. A Set-Cookie value in the header map will also only be used according to the restrictions mentioned above.

This method should only be called if isFeatureSupported returns true for COOKIE_INTERCEPT.

Parameters
@NonNull List<String> cookies

List of valid Set-Cookie header values

setData

public void setData(@Nullable InputStream data)
See also
setData

setEncoding

public void setEncoding(@Nullable String encoding)
See also
setEncoding

setMimeType

public void setMimeType(@NonNull String mimeType)
See also
setMimeType

setResponseHeaders

public void setResponseHeaders(@NonNull Map<StringString> headers)

setStatusCodeAndReasonPhrase

public void setStatusCodeAndReasonPhrase(
    int statusCode,
    @NonNull String reasonPhrase
)

toWebResourceResponse

public @NonNull WebResourceResponse toWebResourceResponse()

Convert this object to a WebResourceResponse that can be used as return value in shouldInterceptRequest and shouldInterceptRequest.

The cookie headers set through setCookies will be encoded as part of the getResponseHeaders header map to use by the WebView implementation.

toWebResourceResponseCompat

public static @NonNull WebResourceResponseCompat toWebResourceResponseCompat(@NonNull WebResourceResponse response)

Convert a WebResourceResponse to the compat version.