【发布时间】: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