how to replace url in webview android studio?

耗尽温柔 提交于 2020-05-17 07:33:08

问题


i am getting first that URL contain ad then i want to replace ad with as how to replace ad with as.

  @Override
protected void onStart() {
    super.onStart();
    Intent intent = getIntent();
    Uri data = intent.getData();
      //want to check contains in data and if contains i want to replace it
    if(data.toString().contains("ad")){
        data.toString() = data.toString().replace("ad", "xyz");
    }

    try {

        webView.loadUrl(data.toString());
    }
    catch (Exception e){
        e.printStackTrace();
    }
}

回答1:


The method that may help you is below which you should put into the @override method

String myUrl = url;
myUrl = myUrl.replace("ad", as);

Then use the new string wherever you want . You can't change the original url :)

The Method replace returns the string value as API DOC says

public String replace (CharSequence target, CharSequence replacement)

Try to do this :

@Override
protected void onStart() {
super.onStart();
Intent intent = getIntent();
Uri data = intent.getData();
String mString;
  //want to check contains in data and if contains i want to replace it
if(data.toString().contains("ad")){
    mString = data.toString().replace("ad", "xyz");
}

try {

    webView.loadUrl(mString);
}
catch (Exception e){
    e.printStackTrace();
}
}


来源:https://stackoverflow.com/questions/61409306/how-to-replace-url-in-webview-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!