我试图模拟的是使用 HttpClient POST 单击按钮“7”,然后使用 JSoup 检索计算器输入屏幕的值并将其设置在 Android 应用程序上的编辑文本框中。代码运行良好,没有错误,但我得到一个空白条目。
我使用了 Apache 库的 HttpClient。这是代码
// Title AsyncTask
private class Title extends AsyncTask<Void, Void, Void> {
String title, response1;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
try {
makePostRequest();
//postData("");
// Connect to the web site
Document document = Jsoup.connect(url).get();
// Get the html document title
title = document.title();
Elements E1 = document.select("input[name=Input]");
Element E2 = E1.first();
title = E2.val();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Set title into TextView
EditText txttitle = (EditText) findViewById(R.id.editText);
txttitle.setText(title);
mProgressDialog.dismiss();
}
private void makePostRequest() {
HttpClient httpClient = new DefaultHttpClient();
// replace with your url
HttpPost httpPost = new HttpPost(url);
//Post Data
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);
nameValuePair.add(new BasicNameValuePair("seven",""));
//Encoding POST data
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
// log exception
e.printStackTrace();
}
//making POST request.
try {
HttpResponse response = httpClient.execute(httpPost);
// write response to log
Log.d("Http Post Response:", response.toString());
} catch (ClientProtocolException e) {
// Log exception
e.printStackTrace();
} catch (IOException e) {
// Log exception
e.printStackTrace();
}
}
}
有人知道我在这里做错了什么吗?该网站不是我的,因此我无法添加 JavaScript。我是 Android 和 Java 编程新手。
如果我可以提供我可能在这里错过的任何其他信息,请告诉我。
谢谢。
最佳答案
您可以使用浏览器监控网络。 Chrome 上的快捷键是 ctrl+shift+i。选择网络选项卡,执行您想要的操作并观察正在发送的 http 请求。在您的情况下,不会发送任何请求,因为该计算器纯粹在浏览器中工作并且不与服务器建立连接。这意味着你需要解释 javascript。名为 Selenium webdriver 的库很好地完成了此类工作。它是一个 headless 网络浏览器,您可以通过代码进行控制。尝试一下。
编辑: Selenium 不适用于 Android。 Android 内置了可以用作浏览器的 View - WebView。并且它可以运行javascript函数。您可以进行 JavaScript 调用。 Look here和 here 。祝你好运
关于java - 如何使用 Android 移动应用程序单击网页上的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35524614/