下面列出了android.webkit.WebView#postUrl ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void intoWebView(WebView webView) {
try {
AnalyzeUrl analyzeUrl = new AnalyzeUrl(tag, url);
switch (analyzeUrl.getRequestMethod()) {
case POST:
webView.postUrl(analyzeUrl.getUrl(), analyzeUrl.getPostData());
break;
case GET:
webView.loadUrl(analyzeUrl.getQueryUrl(), analyzeUrl.getHeaderMap());
break;
case DEFAULT:
webView.loadUrl(analyzeUrl.getUrl(), analyzeUrl.getHeaderMap());
}
} catch (Exception e) {
webView.loadUrl(url);
}
}
public static void loadWebapp(WebView webView, AppSettings appSettings, boolean doLogin) {
Context context = webView.getContext();
Uri url;
try {
url = Uri.parse(appSettings.getProfilePathFull());
} catch (Exception e) {
webView.loadData(context.getString(R.string.no_valid_path), "text/html", "UTF-16");
return;
}
String url_s = url.toString();
if (appSettings.isProfileEmpty()) {
webView.loadData(context.getString(R.string.no_valid_path), "text/html", "UTF-16");
} else {
webView.loadUrl(url_s);
if (doLogin) {
url_s += "?a=checklogin";
String postData = "name=" + appSettings.getProfileLoginUsername() + "&password=" + appSettings.getProfileLoginPassword();
webView.postUrl(url_s, EncodingUtils.getBytes(postData, "base64"));
}
}
}
@SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface"})
private static WebView createAjaxWebView(AjaxParams params, Handler handler) {
WebView webView = new WebView(params.context.getApplicationContext());
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setBlockNetworkImage(true);
settings .setMediaPlaybackRequiresUserGesture(false);
settings.setUserAgentString(params.getUserAgent());
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
if (params.isSniff()) {
webView.setWebViewClient(new SnifferWebClient(params, handler));
} else {
webView.setWebViewClient(new HtmlWebViewClient(handler));
webView.addJavascriptInterface(new JavaInjectMethod(handler), "OUTHTML");
}
switch (params.getRequestMethod()) {
case POST:
webView.postUrl(params.url, params.postData);
break;
case GET:
case DEFAULT:
webView.loadUrl(params.url, params.headerMap);
break;
}
return webView;
}
@ReactProp(name = "source")
public void setSource(WebView view, @NonNull ReadableMap source) {
if (source != null) {
if (source.hasKey("html")) {
String html = source.getString("html");
if (source.hasKey("baseUrl")) {
view.loadDataWithBaseURL(
source.getString("baseUrl"), html, HTML_MIME_TYPE, HTML_ENCODING, null);
} else {
view.loadData(html, HTML_MIME_TYPE, HTML_ENCODING);
}
return;
}
if (source.hasKey("uri")) {
String url = source.getString("uri");
String previousUrl = view.getUrl();
if (source.hasKey("method")) {
String method = source.getString("method");
if (method.equals(HTTP_METHOD_POST)) {
byte[] postData = null;
if (source.hasKey("body")) {
String body = source.getString("body");
try {
postData = body.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
postData = body.getBytes();
}
}
if (postData == null) {
postData = new byte[0];
}
view.postUrl(url, postData);
return;
}
}
HashMap<String, String> headerMap = new HashMap<>();
if (source.hasKey("headers")) {
ReadableMap headers = source.getMap("headers");
ReadableMapKeySetIterator iter = headers.keySetIterator();
while (iter.hasNextKey()) {
String key = iter.nextKey();
if ("user-agent".equals(key.toLowerCase(Locale.ENGLISH))) {
if (view.getSettings() != null) {
view.getSettings().setUserAgentString(headers.getString(key));
}
} else {
headerMap.put(key, headers.getString(key));
}
}
}
view.loadUrl(url, headerMap);
return;
}
}
view.loadUrl(BLANK_URL);
}
@ReactProp(name = "source")
public void setSource(WebView view, @Nullable ReadableMap source) {
if (source != null) {
if (source.hasKey("html")) {
String html = source.getString("html");
if (source.hasKey("baseUrl")) {
view.loadDataWithBaseURL(
source.getString("baseUrl"), html, HTML_MIME_TYPE, HTML_ENCODING, null);
} else {
view.loadData(html, HTML_MIME_TYPE, HTML_ENCODING);
}
return;
}
if (source.hasKey("uri")) {
String url = source.getString("uri");
String previousUrl = view.getUrl();
if (previousUrl != null && previousUrl.equals(url)) {
return;
}
if (source.hasKey("method")) {
String method = source.getString("method");
if (method.equals(HTTP_METHOD_POST)) {
byte[] postData = null;
if (source.hasKey("body")) {
String body = source.getString("body");
try {
postData = body.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
postData = body.getBytes();
}
}
if (postData == null) {
postData = new byte[0];
}
view.postUrl(url, postData);
return;
}
}
HashMap<String, String> headerMap = new HashMap<>();
if (source.hasKey("headers")) {
ReadableMap headers = source.getMap("headers");
ReadableMapKeySetIterator iter = headers.keySetIterator();
while (iter.hasNextKey()) {
String key = iter.nextKey();
if ("user-agent".equals(key.toLowerCase(Locale.ENGLISH))) {
if (view.getSettings() != null) {
view.getSettings().setUserAgentString(headers.getString(key));
}
} else {
headerMap.put(key, headers.getString(key));
}
}
}
view.loadUrl(url, headerMap);
return;
}
}
view.loadUrl(BLANK_URL);
}
@ReactProp(name = "source")
public void setSource(WebView view, @Nullable ReadableMap source) {
if (source != null) {
if (source.hasKey("html")) {
String html = source.getString("html");
if (source.hasKey("baseUrl")) {
view.loadDataWithBaseURL(
source.getString("baseUrl"), html, HTML_MIME_TYPE, HTML_ENCODING, null);
} else {
view.loadData(html, HTML_MIME_TYPE, HTML_ENCODING);
}
return;
}
if (source.hasKey("uri")) {
String url = source.getString("uri");
if (source.hasKey("method")) {
String method = source.getString("method");
if (method.equals(HTTP_METHOD_POST)) {
byte[] postData = null;
if (source.hasKey("body")) {
String body = source.getString("body");
try {
postData = body.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
postData = body.getBytes();
}
}
if (postData == null) {
postData = new byte[0];
}
view.postUrl(url, postData);
return;
}
}
HashMap<String, String> headerMap = new HashMap<>();
if (source.hasKey("headers")) {
ReadableMap headers = source.getMap("headers");
ReadableMapKeySetIterator iter = headers.keySetIterator();
while (iter.hasNextKey()) {
String key = iter.nextKey();
headerMap.put(key, headers.getString(key));
}
}
view.loadUrl(url, headerMap);
return;
}
}
view.loadUrl(BLANK_URL);
}