【问题标题】:how to replace url in webview android studio?如何在 webview android studio 中替换 url?
【发布时间】:2020-04-24 13:16:44
【问题描述】:

我首先得到的 URL 包含广告,然后我想用 as 替换广告。如何用 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();
    }
}

【问题讨论】:

  • 任何人都可以帮助我..

标签: android android-studio webview


【解决方案1】:

可能对你有帮助的方法是你应该把它放在@override方法中

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

然后在任何你想要的地方使用新的字符串。 您无法更改原始网址 :)

方法替换返回 API DOC 所说的字符串值

public String replace (CharSequence target, CharSequence replacement)

尝试这样做:

@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();
}
}

【讨论】:

  • 为什么要将 data.toString() 分配给其他值不这样做尝试分配给答案中的变量,还有一件事是尝试将您的错误放入logcat 和你问的问题。
猜你喜欢
  • 2014-02-14
  • 1970-01-01
  • 2018-10-25
  • 2014-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多