我有一个autocompletetextview用于在webview中打开网址。我想自动将“.com”作为后缀添加到没有.com,.tv,.co.in等任何扩展名的网址。但问题是如何检查输入的网址是否已存在扩展名。我搜索了它并发现了这个post,但它只检查了特定的已知扩展。我想检查是否存在任何扩展名。如果存在扩展名,则打开网址,如果没有,则将“.com”作为后缀添加到网址。
public class MainActivity extends AppCompatActivity implements AdvancedWebView.Listener {
private AdvancedWebView mWebView;
private AutoCompleteTextView urlEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
urlEditText = (AutoCompleteTextView) findViewById(R.id.urlField);
mWebView = (AdvancedWebView) findViewById(R.id.webView);
mWebView.setListener(this, this);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setGeolocationEnabled(false);
mWebView.setMixedContentAllowed(true);
mWebView.setCookiesEnabled(true);
mWebView.setThirdPartyCookiesEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView v, String url){
v.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
//Toast.makeText(MainActivity.this, "Finished loading", Toast.LENGTH_SHORT).show();
}
});
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
Toast.makeText(MainActivity.this, title, Toast.LENGTH_SHORT).show();
}
});
mWebView.addHttpHeader("X-Requested-With", "");
Button openUrl = (Button) findViewById(R.id.goButton);
openUrl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
String url = urlEditText.getText().toString();
if(!url.contains("http://www."))
url = "http://www." + url;
// Here should be the code to check for extensions
try{
mWebView.loadUrl(url);
urlEditText.setText(mWebView.getUrl());
imm.hideSoftInputFromWindow(urlEditText.getWindowToken(), 0);
}catch(Exception e)
{
e.printStackTrace();
}
}
});
}
@SuppressLint("NewApi")
@Override
protected void onResume() {
super.onResume();
mWebView.onResume();
// ...
}
@SuppressLint("NewApi")
@Override
protected void onPause() {
mWebView.onPause();
// ...
super.onPause();
}
@Override
protected void onDestroy() {
mWebView.onDestroy();
// ...
super.onDestroy();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
mWebView.onActivityResult(requestCode, resultCode, intent);
// ...
}
@Override
public void onBackPressed() {
if (!mWebView.onBackPressed()) { return; }
// ...
super.onBackPressed();
}
@Override
public void onPageStarted(String url, Bitmap favicon) {
mWebView.setVisibility(View.INVISIBLE);
}
@Override
public void onPageFinished(String url) {
mWebView.setVisibility(View.VISIBLE);
}
@Override
public void onPageError(int errorCode, String description, String failingUrl) {
Toast.makeText(MainActivity.this, "onPageError(errorCode = "+errorCode+", description = "+description+", failingUrl = "+failingUrl+")", Toast.LENGTH_SHORT).show();
}
@Override
public void onDownloadRequested(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
Toast.makeText(MainActivity.this, "onDownloadRequested(url = "+url+", userAgent = "+userAgent+", contentDisposition = "+contentDisposition+", mimetype = "+mimetype+", contentLength = "+contentLength+")", Toast.LENGTH_LONG).show();
/*final String filename = UUID.randomUUID().toString();
if (AdvancedWebView.handleDownload(this, url, filename)) {
// download successfully handled
}
else {
// download couldn't be handled because user has disabled download manager app on the device
}*/
}
@Override
public void onExternalPageRequest(String url) {
Toast.makeText(MainActivity.this, "onExternalPageRequest(url = "+url+")", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
我使用了一种简单的方法来验证url并返回true / false值。如果url有效,则按原样打开url。如果网址无效,请将“.com”添加到网址,然后再打开。
In onCreate use methos like this:
if(isURL(url)){
try{
mWebView.loadUrl(url);
urlEditText.setText(mWebView.getUrl());
imm.hideSoftInputFromWindow(urlEditText.getWindowToken(), 0);
}catch(Exception e)
{
e.printStackTrace();
}
}else{
url = url + ".com";
try{
mWebView.loadUrl(url);
urlEditText.setText(mWebView.getUrl());
imm.hideSoftInputFromWindow(urlEditText.getWindowToken(), 0);
}catch(Exception e)
{
e.printStackTrace();
}
}
Method to check if url is valid:
public boolean isURL(String url)
{
if(Patterns.WEB_URL.matcher(url).matches()){
return true;
}else{
return false;
}
}