简体   繁体   English

如何使用Jsoup监听WebView中的元素更改?

[英]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): 所以这就是我认为你的代码中发生了什么(我没有仔细分析它):

  1. You create a WebView 您创建一个WebView
  2. In that Webview you log in. For that usually form POST requests are sent to the web server and the web server responds with setting a couple of cookies in the browser so that you maintain the logged in state for the next requests. 在该Webview中,您登录。通常,POST请求被发送到Web服务器,Web服务器通过在浏览器中设置几个cookie来响应,以便您保持下一个请求的登录状态。 The URL of the page however does not change in a way that would let you in to take over that session. 但是,页面的URL不会以允许您接管该会话的方式更改。
  3. You create a new session with the web server by connecting to it with JSoup's http connection feature. 您可以通过使用JSoup的http连接功能连接到Web服务器来创建新会话。 The web server responds with a site that of course has no logged in user. Web服务器使用当然没有登录用户的站点进行响应。
  4. You parse that page -> not logged in 您解析该页面 - >未登录

What you need to do: 你需要做什么:

  1. same as above 与上述相同
  2. same as above 与上述相同
  3. find the WebView function that retrieves the HTML of the currently loaded page and use it to put all content into a string 找到检索当前加载页面的HTML的WebView函数,并使用它将所有内容放入字符串中
  4. parse the string using JSoup. 使用JSoup解析字符串。 Do not use the JSoup connect method. 不要使用JSoup connect方法。
  5. Find what you were looking for 找到你要找的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM