android webview点击模拟

发布于 2024-11-09 11:00:39 字数 88 浏览 0 评论 0原文

我想知道是否可以从网站加载框架,填充文本框,按提交按钮并从该页面获取结果?我需要在后台执行此操作,这样我的网络视图就不会显示,只显示编辑文本和文本视图......

I was wondering if it's possible to load a frame from a website, fill a textbox, press submit button and take the results from that page? I need to do this in background so my webview is never showed, only an edittext and a textview are showed...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁虫ゝ 2024-11-16 11:00:39

你不能使用 http 请求做你提到的事情吗?
你需要 WebView 做什么?

Can't you do the stuff you mentioned using a http request?
What do you need the WebView for?

樱花坊 2024-11-16 11:00:39

您可以使用 httpGet 获取页面,并使用 httpPost 在用户按下提交时发布结果。您所要做的就是确保帖子包含网页期望的数据,并采用其期望的格式。

检查所需内容的一个好方法是使用 Firefox Live HHTP Headers add on (https://addons.mozilla.org/en-US/firefox/addon/live-http-headers/)。

例如,GET 可能是

URI uri = new URI(<your URL here>);
HttpGet request = new HttpGet(uri);
HttpResponse response = defaultHttpClient.execute(request);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity);

然后您在 Android 视图中放置编辑文本框和按钮等。当用户按下“提交”时,您会从编辑文本框中获取文本,然后构造一个 POST,如下所示。

URI uri = new URI(<your URL here - without a host>);
HttpPost request = new HttpPost(urri);
request.setHeader("Content-type", "application/x-www-form-urlencoded");

List<NameValuePair> bodyParams = new ArrayList<NameValuePair>();
bodyParams.add(new BasicNameValuePair(<parameter name string>, <parameter value string>);
// repeat additional bodyParams.add() as necessary for further parameters
request.setEntity(new UrlEncodedFormEntity(bodyParams, HTTP.UTF_8));
HttpHost httpHost = new HttpHost(<your host here>);
HttpResponse response = defaultHttpClient.execute(httpHost, request);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity);

You can use httpGet to get the pages and httpPost to post the results when the user presses submit. All you have to do is make sure that the post carries the data that the web page expects, and in the format it expects.

A good way to check what is required is use the Firefox Live HHTP Headers add on (https://addons.mozilla.org/en-US/firefox/addon/live-http-headers/).

For example the GET might be

URI uri = new URI(<your URL here>);
HttpGet request = new HttpGet(uri);
HttpResponse response = defaultHttpClient.execute(request);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity);

Then you put up your Android view with edit text boxes and buttons etc. When the user presses Submit, you get the text form the edit text box, and then construct a POST as below.

URI uri = new URI(<your URL here - without a host>);
HttpPost request = new HttpPost(urri);
request.setHeader("Content-type", "application/x-www-form-urlencoded");

List<NameValuePair> bodyParams = new ArrayList<NameValuePair>();
bodyParams.add(new BasicNameValuePair(<parameter name string>, <parameter value string>);
// repeat additional bodyParams.add() as necessary for further parameters
request.setEntity(new UrlEncodedFormEntity(bodyParams, HTTP.UTF_8));
HttpHost httpHost = new HttpHost(<your host here>);
HttpResponse response = defaultHttpClient.execute(httpHost, request);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文