本文整理匯總了Java中android.webkit.WebResourceResponse類的典型用法代碼示例。如果您正苦於以下問題:Java WebResourceResponse類的具體用法?Java WebResourceResponse怎麽用?Java WebResourceResponse使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
WebResourceResponse類屬於android.webkit包,在下文中一共展示了WebResourceResponse類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: shouldInterceptRequest
import android.webkit.WebResourceResponse; //導入依賴的package包/類
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
/*
ByteArrayInputStream EMPTY = new ByteArrayInputStream("".getBytes());
if (mAdBlock.isAd(request.getUrl().getHost())) {
return new WebResourceResponse("text/plain", "utf-8", EMPTY);
}
if(request.getUrl().getHost().indexOf("127.0.0.1")>=0){
//ToastUtil.showMessage("this site is insecure");
return new WebResourceResponse("text/plain", "utf-8", EMPTY);
}
*/
return super.shouldInterceptRequest(view, request);
}
示例2: shouldInterceptRequest
import android.webkit.WebResourceResponse; //導入依賴的package包/類
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
// Intercept requests for private images and add the WP.com authorization header
if (mIsPrivatePost && !TextUtils.isEmpty(mToken) && UrlUtils.isImageUrl(url)) {
try {
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
conn.setReadTimeout(WPRestClient.REST_TIMEOUT_MS);
conn.setConnectTimeout(WPRestClient.REST_TIMEOUT_MS);
conn.setRequestProperty("Authorization", "Bearer " + mToken);
conn.setRequestProperty("User-Agent", WordPress.getUserAgent());
conn.setRequestProperty("Connection", "Keep-Alive");
conn.connect();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream inputStream = new BufferedInputStream(conn.getInputStream());
return new WebResourceResponse(conn.getContentType(), "UTF-8", inputStream);
}
} catch (IOException e) {
AppLog.e(AppLog.T.READER, e);
}
}
return null;
}
示例3: shouldInterceptRequest
import android.webkit.WebResourceResponse; //導入依賴的package包/類
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
Uri url = request.getUrl();
if ("https://www.fengshihao.com/user.js".indexOf(url.toString()) == 0) {
final WebResourceResponse userjs = new WebResourceResponse("text/javascript", "UTF-8",
new ByteArrayInputStream("console.log('hello user js');".getBytes()));
return userjs;
}
return super.shouldInterceptRequest(view, request);
}
示例4: getResource
import android.webkit.WebResourceResponse; //導入依賴的package包/類
public WebResourceResponse getResource(String path) {
int index = path.lastIndexOf(".");
if (index == -1) {
return null;
}
String ext = path.substring(index);
Log.d("wlx", ext);
String mini = (String) this.minitype.get(ext);
if (mini == null) {
return null;
}
try {
return new WebResourceResponse(mini, "UTF-8", new FileInputStream(this.resource + path));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
示例5: shouldInterceptRequest
import android.webkit.WebResourceResponse; //導入依賴的package包/類
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
WebResourceResponse webResourceResponse = null;
if (mCustomWebViewClient!=null){
webResourceResponse = mCustomWebViewClient.shouldInterceptRequest(view,url);
}
if (webResourceResponse != null){
return webResourceResponse;
}
if (!mIsEnableCache){
return null;
}
return mWebViewCache.getWebResourceResponse(this,url,mCacheStrategy,
mEncoding,mCacheInterceptor);
}
示例6: shouldInterceptRequest
import android.webkit.WebResourceResponse; //導入依賴的package包/類
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
//此處對文件資源,js,css等請求資源進行攔截,替換
Log.d(TAG, "shouldInterceptRequest: request = \n" +
"\nurl = " + request.getUrl().toString() +
"\nmethod = " + request.getMethod() +
"\nheaders = " + request.getRequestHeaders().toString());
// String url = request.getUrl().toString();
// if ((url.startsWith("https://") || url.startsWith("http://")) && (url.endsWith(".png") || url.endsWith(".jpg"))) {
// Log.d(TAG, "攔截資源 :" + url);
// try {
// WebResourceResponse response = new WebResourceResponse(MimeTypeMap.getFileExtensionFromUrl(".jpg"), "utf-8", FileUtils.getInputStreamFromAssets("img/dog.jpg"));
// return response;
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
return super.shouldInterceptRequest(view, request);
}
示例7: shouldInterceptRequest
import android.webkit.WebResourceResponse; //導入依賴的package包/類
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
// Uses cache to determine if the url is ad,
// and returns an empty resource for ads.
boolean ad;
if (!mLoadedUrls.containsKey(url)) {
ad = mAdBlocker.isAd(url);
mLoadedUrls.put(url, ad);
} else {
ad = mLoadedUrls.get(url);
}
// Verbose blocked or passed urls
Log.v(getClass().getSimpleName(), (ad ? "Blocked" : "Pass") + ": " + Uri.parse(url).getHost());
// Return appropriate response
if(ad) {
return mAdBlocker.createEmptyResource();
}
return super.shouldInterceptRequest(view, url);
}
示例8: getWebResourceResponse
import android.webkit.WebResourceResponse; //導入依賴的package包/類
/**
* 獲取本地資源
*/
@Nullable
private WebResourceResponse getWebResourceResponse(String url) {
try {
// 如果是圖片且本地有緩存
if (isImageSuffix(url) || isGifSuffix(url)) {
FileInputStream inputStream = mCache.getStream(url);
if (null != inputStream) {
return new WebResourceResponse(getMimeType(url), "base64", inputStream);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例9: shouldInterceptRequest
import android.webkit.WebResourceResponse; //導入依賴的package包/類
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
if (prefs.getBoolean("adblock", true)) {
boolean ad;
if (!loadedUrls.containsKey(url)) {
ad = AdBlocker.isAd(url);
loadedUrls.put(url, ad);
} else {
ad = loadedUrls.get(url);
}
return ad ? AdBlocker.createEmptyResource() :
super.shouldInterceptRequest(view, url);
}
return super.shouldInterceptRequest(view, url);
}
示例10: shouldInterceptRequest
import android.webkit.WebResourceResponse; //導入依賴的package包/類
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (mAdBlock.isAd(request.getUrl().toString())) {
String stweb = "\n \n \n \n BLOCKED BY AD-BLOCKER" + "\n" +
"\n" +
" \n" +
" \n" +
" \n - TO DISABLE AD-BLOCKER GO TO SETTINGS/GENEARL SETTING";
ByteArrayInputStream EMPTY = new ByteArrayInputStream(stweb.getBytes());
return new WebResourceResponse("text/plain", "utf-8", EMPTY);
}
}
return super.shouldInterceptRequest(view, request);
}
示例11: shouldInterceptRequest
import android.webkit.WebResourceResponse; //導入依賴的package包/類
@SuppressLint("NewApi")
public WebResourceResponse shouldInterceptRequest (WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
try {
URL urlData = new URL(url);
String path = urlData.getPath().substring(1);
if (!path.endsWith(".js") && !path.endsWith(".css")) {
return null;
}
Log.d("BasicWebViewClient", "Find " + path + " from asset.");
InputStream localStream = assetMgr.open(path);
Log.d("BasicWebViewClient", url + " found, try load from asset.");
return new WebResourceResponse((path.endsWith(".js") ? "text/javascript" : "text/css"), "UTF-8", localStream);
} catch (Exception e) {
return null;
}
}
示例12: shouldInterceptRequest
import android.webkit.WebResourceResponse; //導入依賴的package包/類
/**
* Attempt to retrieve the WebResourceResponse associated with the given <code>url</code>.
* This method should be invoked from within
* {@link android.webkit.WebViewClient#shouldInterceptRequest(android.webkit.WebView, String)}.
*
* @param url the url to process.
* @return a response if the request URL had a matching handler, null if no handler was found.
*/
public WebResourceResponse shouldInterceptRequest(String url) {
PathHandler handler = null;
Uri uri = parseAndVerifyUrl(url);
if (uri != null) {
synchronized (uriMatcher) {
handler = (PathHandler) uriMatcher.match(uri);
}
}
if (handler == null)
return null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return new WebResourceResponse(handler.getMimeType(), handler.getEncoding(),
new LegacyLazyInputStream(handler, uri));
} else {
InputStream is = handler.handle(uri);
return new WebResourceResponse(handler.getMimeType(), handler.getEncoding(),
is);
}
}
示例13: testHostAssets
import android.webkit.WebResourceResponse; //導入依賴的package包/類
public void testHostAssets() {
final String testHtmlContents = "<body><div>hah</div></body>";
WebViewLocalServer assetServer = new WebViewLocalServer(new MockProtocolHandler() {
@Override
public InputStream openAsset(String path) throws IOException {
if (path.equals("/www/test.html")) {
return new ByteArrayInputStream(testHtmlContents.getBytes("utf-8"));
}
return null;
}
});
WebViewLocalServer.AssetHostingDetails details =
assetServer.hostAssets("androidplatform.net", "/www", "/assets", true, true);
assertEquals(details.getHttpPrefix(), Uri.parse("http://androidplatform.net/assets"));
assertEquals(details.getHttpsPrefix(), Uri.parse("https://androidplatform.net/assets"));
WebResourceResponse response =
assetServer.shouldInterceptRequest("http://androidplatform.net/assets/test.html");
assertNotNull(response);
assertEquals(testHtmlContents, readAsString(response.getData(), "utf-8"));
}
示例14: loadFromAssets
import android.webkit.WebResourceResponse; //導入依賴的package包/類
private WebResourceResponse loadFromAssets(
String url, String assetPath,
String mimeType, String encoding
) {
AssetManager assetManager = this.activity.getAssets();
InputStream input = null;
try {
Log.d(LOG_TAG, "Loading from assets: " + assetPath);
input = assetManager.open("/images/logo.png");
WebResourceResponse response =
new WebResourceResponse(mimeType, encoding, input);
return response;
} catch (IOException e) {
Log.e("WEB-APP", "Error loading " + assetPath + " from assets: " +
e.getMessage(), e);
}
return null;
}
示例15: shouldInterceptRequest
import android.webkit.WebResourceResponse; //導入依賴的package包/類
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public WebResourceResponse shouldInterceptRequest(WebView aWebView, WebResourceRequest request)
{
String strMimeType = getFileMimeType(request.getUrl().toString());
if (strMimeType != null)
{
String lowerCaseUrl = strMimeType.toLowerCase();
if (lowerCaseUrl.contains("png") || lowerCaseUrl.contains("jpg") || lowerCaseUrl.contains("jpeg"))
{
return handleImageRequest(aWebView, request, strMimeType);
}
}
return super.shouldInterceptRequest(aWebView, request);
}