【问题标题】:Can't fetch dynamically changed HTML content(by JavaScript) in android using AsyncTask.无法使用 AsyncTask 在 android 中获取动态更改的 HTML 内容(通过 JavaScript)。
【发布时间】:2016-09-15 12:58:10
【问题描述】:

我正在使用 python 脚本来动态更改我的网页内容。网页链接为:[HERE]

网页包含有两列的表格:
1) 介于 1 到 5 之间的值。
2) 以及上传到页面的时间。

它由页面上的 JavaScript 动态显示。请参阅打开上面链接的页面来源。

现在我正在创建一个 android 应用程序,它通过获取该页面并解析 HTML 在文本视图中显示值。但页面源仅包含 JavaScript 代码。因此,每当我获取 HTML 时,它只会显示 JavaScript,即使它包含多于一行的值。

我的问题是如何动态获取这些值?

我正在使用 AsyncTask 并每 10 秒安排一次。我的代码是:

public class MainActivity extends AppCompatActivity {

TextView et ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    et = (TextView) findViewById(R.id.editText);

    DownloadWebPageTask task = new DownloadWebPageTask();
    task.execute(new String[]{"https://rasppiclient.herokuapp.com/"});

callAsynchronousTask();

}



public void callAsynchronousTask() {
    final Handler handler = new Handler();
    Timer timer = new Timer();
    TimerTask doAsynchronousTask = new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                public void run() {
                    try {
                        DownloadWebPageTask performBackgroundTask = new DownloadWebPageTask();
                        // PerformBackgroundTask this class is the class that extends AsynchTask
                        performBackgroundTask.execute(new String[]{"https://rasppiclient.herokuapp.com/"});
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                    }
                }
            });
        }
    };
    timer.schedule(doAsynchronousTask, 0, 10000); //execute in every 50000 ms
}



public class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        String response = "";
        for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try {
                HttpResponse execute = client.execute(httpGet);
                InputStream content = execute.getEntity().getContent();

                BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
                String s = "";
                while ((s = buffer.readLine()) != null) {
                    response += s;
                }

            } catch (Exception e) {


            }
        }
        return response;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        Toast.makeText(MainActivity.this,s,Toast.LENGTH_LONG).show();

    }
}}

【问题讨论】:

  • 这不适用于DefaultHttpClient。您必须打开网站,以便执行 javascript,然后将其废弃。
  • 如何在安卓中做到这一点? @MuratK。

标签: javascript java android html android-asynctask


【解决方案1】:

这不适用于DefaultHttpClient。您可以做的是评估 javascript,然后使用 AndroidJSCore 获取内容。

JSContext context = new JSContext();
context.evaluateScript(yourJsString);
context.property("getWhatYouWantHere");

github 上的一个例子是这样的

JSContext context = new JSContext();
context.evaluateScript("a = 10");
JSValue newAValue = context.property("a");
System.out.println(df.format(newAValue.toNumber())); // 10.0
String script =
  "function factorial(x) { var f = 1; for(; x > 1; x--) f *= x; return f; }\n" +
  "var fact_a = factorial(a);\n";
context.evaluateScript(script);
JSValue fact_a = context.property("fact_a");
System.out.println(df.format(fact_a.toNumber())); // 3628800.0

【讨论】:

  • 但是我有 JavaScript 可以为页面生成表格视图。所以我必须在这里将整个 JavaScript 设置为字符串??
  • 查看我提到的页面上的脚本
  • @14bce109 是的,但这不适用于 ajax。在这种情况下,您可以使用 android WebView 类进行加载和抓取。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-22
  • 2013-07-24
  • 2020-04-30
  • 2015-03-30
  • 1970-01-01
相关资源
最近更新 更多