[英]How to listen to elements changing in WebView using Jsoup?
I'm trying to retrieve a value of a hidden input HTML element using Jsoup, the value is "none" when the user isn't logged in and when the user is logged in (in the WebView app) it gives their username, i successfuly was able to fetch the element i want using Jsoup but i'm trying to listen on the WebView to fetch the value of the element when they're actually logged in and the element's value is different from "none", i used the onPageStarted and onPageFinished with no avail as they keep sending me the original value (none) when the user isn't logged in yet, this is my MainActivity 我正在尝试使用Jsoup检索隐藏输入HTML元素的值,当用户未登录时值为“none”,并且当用户登录时(在WebView应用程序中)它提供了用户名,i successfuly能够使用Jsoup获取我想要的元素但是我正在尝试在WebView上实际登录时获取元素的值并且元素的值与“none”不同,我使用了onPageStarted和onPageFinished没有用,因为当用户还没有登录时他们继续向我发送原始值(无),这是我的MainActivity
MainActivity.java MainActivity.java
package com.gci.gestioncapteursincendie;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
WebView webView;
String webView_url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//storing webView defined in activity_main.xml inside a WebView instance webView
webView = (WebView) findViewById(R.id.webview);
//enabling js in the WebView
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient( new WebViewClientImpl(this){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
new MyTask().execute();
}
});
//defining the website the webView loads when the app is launched
webView.loadUrl("http://gestioncapteursincendie.herokuapp.com");
webView_url = webView.getUrl();
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if(task.isSuccessful()){
String token = task.getResult().getToken();
System.out.println("token: "+token);
}
else
{
System.out.println("Token Not Generated");
}
}
});
}
private class MyTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
Document doc = null;
Element x = null;
try {
doc = Jsoup.connect(webView_url).get();
x = doc.getElementById("loggedin");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Webview url= " +webView_url);
System.out.println("loggedin= " + x);
return x.val();
}
@Override
protected void onPostExecute(String result) {
System.out.println("RESULT: "+result);
}
}
}
Jsoup is not a browser and it is not synchronized with the WebView (which is basically a browser!) Jsoup不是浏览器,它与WebView(基本上是浏览器!)不同步。
So you would only get the same content of the the WebView, if the full state of the site is reflected in the URL, which I very much doubt, because this would be very insecure for the website. 因此,如果网站的完整状态反映在URL中,您将只获得与WebView相同的内容,我非常怀疑,因为这对网站来说是非常不安全的。
So here is what happens in your code I think (I did not carefully analyze it): 所以这就是我认为你的代码中发生了什么(我没有仔细分析它):
What you need to do: 你需要做什么:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.