我试图从 webview evalueteJavascript 值获取一些字符串返回
我不知道这里出了什么问题,直到昨天为止它都工作正常,我没有对此代码进行任何更改,但不知道为什么它给我这个错误
尝试调用虚拟方法“boolean” 空对象引用上的 java.lang.String.equals(java.lang.Object)'
我在这里甚至没有使用任何布尔值,我有点困惑这里到底出了什么问题,请任何人帮助我
Main Nav 方法必须评估 JavaScript,其中第一个检查线程页面是否有效或已损坏
表示页面的
var brokenPage = document.querySelector('html[id=instagram]');
检查包含主要出现在损坏页面或私人个人资料中的元素
这两个
"var splashScreenDark = document.querySelector('html[class=" + selectorsdark + "]');" +
"var splashScreenLite = document.querySelector('html[class=" + selectorslite + "]');" +
检查页面是否有效且不是私人帐户或非线程网页的意外页面
第二个评估 JavaScript 有一个主要元素检查,可以区分评论页面和帖子页面
如果
document.querySelector('[data-pressable-container=true]');
为真,则表示它是评论页面,如果为假,则表示它是帖子页面
public void linkValidationAndUrl(String url) throws IOException {
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.white));
textStatus.setText(getString(R.string.validating_link));
if (url.startsWith("https://www.threads.net") || url.startsWith("https://threads.net")) {
MainNav(url, new AccountStatusCallback() {
@Override
public void onAccountStatusReceived(String[] accountStatus) {
// Regex pattern for profile link
Pattern profileLinkPattern = Pattern.compile("^https://(?:www\\.)?threads\\.net/@[^/]+$");
Pattern post1Pattern = Pattern.compile("(?<=.net\\/t\\/).*(?=\\/\\?)|(?<=post\\/).*");
Matcher profileMatcher = profileLinkPattern.matcher(url);
Matcher post1Matcher = post1Pattern.matcher(url);
if (accountStatus[0].equals("true")) {
textStatus.setText(null);
Toast.makeText(getApplicationContext(), "Broken Url or Private Profile", Toast.LENGTH_SHORT).show();
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
textStatus.setText(getString(R.string.brokenorprivateStr));
} else if (accountStatus[0].equals("unexpected")) {
textStatus.setText(null);
Toast.makeText(getApplicationContext(), "Unexpected Error", Toast.LENGTH_SHORT).show();
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
textStatus.setText(getString(R.string.unexpected_error));
} else if (accountStatus[0].equals("false")) {
if (profileMatcher.matches()) {
textStatus.setText(null);
textStatus.setText(getString(R.string.profile_link));
Intent iPr = new Intent(getApplicationContext(), ProfileActivity.class);
startActivity(iPr);
} else if (post1Matcher.find()) {
textStatus.setText(null);
if (accountStatus[1].equals("true")) {
//Navigate to comment page
textStatus.setText("Comment Link Provided");
Intent iC = new Intent(getApplicationContext(), CommentActivity.class);
startActivity(iC);
} else if (accountStatus[1].equals("false")) {
// Navigate to post page
textStatus.setText("Post Link Provided");
Intent iP = new Intent(getApplicationContext(), PostActivity.class);
startActivity(iP);
} else if (accountStatus[1].equals("unexpected")) {
textStatus.setText(null);
Toast.makeText(getApplicationContext(), "Unexpected Error", Toast.LENGTH_SHORT).show();
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
textStatus.setText(getString(R.string.unexpected_error));
}
} else {
textStatus.setText(null);
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
textStatus.setText(getString(R.string.threadlink_invaliddataurl));
}
}else {
textStatus.setText(null);
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
textStatus.setText("Unable To Get Data");
}
}
});
} else if ((!url.startsWith("https://")) && (url.contains("www.threads.net") || url.contains("threads.net"))) {
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
textStatus.setText(getString(R.string.threadLink_NoHttps));
} else {
Toast.makeText(MainActivity.this, "Invailed Url", Toast.LENGTH_SHORT).show();
textStatus.setText(null);
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
textStatus.setText(getString(R.string.threadLink_False));
}
}
public void MainNav(String profileUrl, AccountStatusCallback callback) {
final String[] AccountAndCommentStatus = {null, null};
String selectorsdark = "\"_9dls __fb-dark-mode\"";
String selectorslite = "\"_9dls __fb-light-mode\"";
AtomicInteger evaluationCounter = new AtomicInteger(); // Define the evaluationCounter here
webView.loadUrl(profileUrl);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
// Delay the JavaScript execution by 2000 milliseconds (2 seconds)
// Execute JavaScript to get the account status
new Handler().postDelayed(() -> {
webView.evaluateJavascript("javascript: var brokenPage = document.querySelector('html[id=instagram]');\n" +
"var splashScreenDark = document.querySelector('html[class=" + selectorsdark + "]');" +
"var splashScreenLite = document.querySelector('html[class=" + selectorslite + "]');" +
"var pageAvailability = 'null';\n" +
"if (brokenPage) {\n" +
" pageAvailability = 'true';\n" +
"}else if (splashScreenDark || splashScreenLite){\n" +
" pageAvailability = 'false'\n" +
"}else{pageAvailability = 'null'}\n" +
"pageAvailability;", value -> {
if (value != null) {
// Remove the double quotes around the value
value = value.replace("\"", "");
switch (value) {
case "true":
AccountAndCommentStatus[0] = "true";
break;
case "false":
AccountAndCommentStatus[0] = "false";
break;
case "null":
AccountAndCommentStatus[0] = "unexpected";
break;
}
// Increase the evaluation counter
evaluationCounter.getAndIncrement();
// Check if both evaluations are done
if (evaluationCounter.get() == 2 && callback != null) {
callback.onAccountStatusReceived(AccountAndCommentStatus);
}
}else {
Toast.makeText(getApplicationContext(),"Unexpected Error Main Page",Toast.LENGTH_LONG).show();
}
});
}, 2000);
new Handler().postDelayed(() -> {
// Execute JavaScript to get the comment status
webView.evaluateJavascript("javascript: var pageAvail = document.querySelector('html[id=instagram]');\n" +
"var commentExist = 'null';\n" +
"if (!pageAvail) {\n" +
"\n" +
" var firstData = document.querySelector('[data-pressable-container=true]');\n" +
" if (firstData) {\n" +
" var repliesElement = firstData.querySelector('svg[aria-hidden=true][fill=none]');\n" +
" if (repliesElement) {\n" +
" commentExist = 'true';\n" +
" } else { commentExist = 'false';}\n" +
" }\n" +
"}\n" +
"commentExist;\n", value -> {
if (value != null) {
// Remove the double quotes around the value
value = value.replace("\"", "");
switch (value) {
case "true":
AccountAndCommentStatus[1] = "true";
break;
case "false":
AccountAndCommentStatus[1] = "false";
break;
case "null":
AccountAndCommentStatus[1] = "unexpected";
break;
}
// Increase the evaluation counter
evaluationCounter.getAndIncrement();
// Check if both evaluations are done
if (evaluationCounter.get() == 2 && callback != null) {
callback.onAccountStatusReceived(AccountAndCommentStatus);
}
}else {
Toast.makeText(getApplicationContext(),"Unexpected Error Post/Comment Page",Toast.LENGTH_LONG).show();
}
});
}, 4000);
}
});
}
public interface AccountStatusCallback {
void onAccountStatusReceived(String[] accountStatus);
}
不知道出了什么问题,但在移动设备重新启动后我没有遇到问题,任何人都可以解释发生了什么以及我如何避免它?
请任何人也可以帮助我在 linkValidation() 方法中编写更好的逻辑代码,现在一团糟