我尝试在MyJavaScriptInterface类中加载另一个url但是没有工作
此代码用于加载网址。
class MyJavaScriptInterface
{
@JavascriptInterface
@SuppressWarnings("unused")
public void processHTML(String html)
{
Log.e("processHTML::",html);
Log.e("url2::",url2);
if (html.contains("class")){
Log.e("<span class::","yes");
browser.loadUrl(url2);
}else {
PRB.setVisibility(View.INVISIBLE);
}
}
}
显示此日志:
Log.e("<span class::","yes");
并且这个条件没问题:
if (html.contains("class")){
我的代码java:
package chindi.ir.chindi;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class MainActivity extends AppCompatActivity {
private WebView browser;
private WebSettings webSettings;
private RelativeLayout PRB;
private Boolean showJS,isFirst,isSs,isFirst2;
private String url="http://www.chindi.ir";
private String url2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
browser = (WebView) findViewById(R.id.WebView);
PRB=(RelativeLayout) findViewById(R.id.kkk);
WebSettings webSettings = browser.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
Bundle extras = getIntent().getExtras();
showJS =extras.getBoolean("jsload",false);
isSs =extras.getBoolean("isSs",false);
url2=extras.getString("urlload2");
browser.loadUrl(extras.getString("urlload"));
isFirst=true;
isFirst2=true;
class MyJavaScriptInterface
{
@JavascriptInterface
@SuppressWarnings("unused")
public void processHTML(String html)
{
Log.e("processHTML::",html);
Log.e("url2::",url2);
if (html.contains("class")){
Log.e("<span class::","yes");
browser.loadUrl(url2);
}else {
PRB.setVisibility(View.INVISIBLE);
}
}
}
browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
browser.setWebViewClient(new WebViewClient(){
@Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Toast.makeText(MainActivity.this, url, Toast.LENGTH_SHORT).show();
if (url.endsWith(".m00000p4")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "video/mp4"); view.getContext().startActivity(intent);
}
else {
view.loadUrl(url);
} return true;
}
public void onPageFinished(WebView view, String url)
{
Log.e("onPageFinished::",url);
Log.e("isSs::",isSs.toString());
Log.e("isFirst2::",isFirst2.toString());
if(showJS&&isFirst) {
isFirst=false;
browser.loadUrl("javascript:geoFindMe()");
}
if (isSs&&isFirst2){
isFirst2=false;
browser.loadUrl("javascript:window.HTMLOUT.processHTML(document.getElementById('select2-w0-container').innerHTML);");
}else {
PRB.setVisibility(View.INVISIBLE);
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Toast.makeText(MainActivity.this, description, Toast.LENGTH_SHORT).show();
}
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
// TODO show you progress image
super.onPageStarted(view, url, favicon);
if (url.equals("https://chindi.ir/")|url.equals("https://chindi.ir")|url.equals("https://www.chindi.ir/")|url.equals("https://www.chindi.ir")|url.equals("http://chindi.ir/")|url.equals("http://chindi.ir")) {
finish();
}else {
PRB.setVisibility(View.VISIBLE);
}
}
});
}
public void onBackPressed() {
if (browser.canGoBack()) {
browser.goBack();
} else {
finish();
}
}
}
答案 0 :(得分:0)
我认为你不是从主线程调用browser.loadUrl(url2)。 你可以尝试:
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
browser.loadUrl(url2);
}
});