简体   繁体   English

如何使用Android移动应用程序单击网页上的按钮

[英]How to click a button on a Webpage using Android mobile application

For this site 对于这个网站

What i am trying to simulate is clicking the button "7" using HttpClient POST and then retrieving the value of the Input screen of the calculator using JSoup and setting it in an edit text box on the android app. 我要模拟的是使用HttpClient POST单击按钮“ 7”,然后使用JSoup检索计算器的“输入”屏幕的值,并将其设置在android应用程序的编辑文本框中。 The code is running fine with no errors, but i am getting a blank entry. 代码运行正常,没有错误,但是我得到一个空白条目。

I utilized HttpClient of Apache library. 我利用了Apache库的HttpClient。 Here is the code 这是代码

    // 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();
        }

    }


}

Does anyone have any idea what i am doing wrong here? 有人知道我在做什么错吗? The website is not mine and therefore i cannot add JavaScript. 该网站不是我的,因此我无法添加JavaScript。 Am new to Android and Java Programming. 是Android和Java编程的新手。

Please let me know if i can provide any other information i may have missed here. 请让我知道我是否可以提供我可能在这里错过的任何其他信息。

Thank you. 谢谢。

You can monitor network using your browser. 您可以使用浏览器监视网络。 On chrome shortcut is ctrl+shift+i . 在chrome上,快捷键是ctrl + shift + i Select the network tab, do the action you want and watch what http requests is being sent. 选择“网络”标签,执行所需的操作,然后查看正在发送的HTTP请求。 In your case no requests is send because that calculator works purely in browser and making no connections to server. 在您的情况下,不会发送任何请求,因为该计算器仅在浏览器中工作,并且不与服务器建立连接。 That means you need to interpret javascript. 这意味着您需要解释javascript。 Such work is excellently carried by library called Selenium webdriver . 此类工作由称为Selenium webdriver的库很好地完成。 It's a headless web browser that you can control from your code. 这是一个无头的Web浏览器,您可以从代码中进行控制。 Try it. 试试吧。

EDIT: Selenium do not work on Android. 编辑: Selenium在Android上不起作用。 Android has built in view that you can use as browser - WebView. Android内置了可用作浏览器的视图-WebView。 And it can run javascript functions. 它可以运行javascript函数。 You can do javascript calls. 您可以进行JavaScript调用。 Look here and here . 看这里这里 Good luck 祝好运

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

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