1

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. The code is running fine with no errors, but i am getting a blank entry.

I utilized HttpClient of Apache library. 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. Am new to Android and Java Programming.

Please let me know if i can provide any other information i may have missed here.

Thank you.

Emzor
  • 1,380
  • 17
  • 28
TommyTin
  • 11
  • 4
  • How do you know that clicking the buttons is a POST request? If I were to write that calculator, it would purely be javascript. – OneCricketeer Feb 20 '16 at 14:32
  • Bummer.... thanks for your comment – TommyTin Feb 20 '16 at 14:54
  • I'm sure there are REST API calculators somewhere if that's all youre wanting to do – OneCricketeer Feb 20 '16 at 14:55
  • Actually this was just intended to be a proof of concept of sorts for something i had wanted to do, which was to simulate a button click. My true intention was to enter some information in text fields and click a button that submits that information. I choose this website because of the number buttons and i thought it would be very simple to use. Apologies for the vagueness, i am very new to HTML and Android. Again thanks for your comment. – TommyTin Feb 20 '16 at 15:02

1 Answers1

0

You can monitor network using your browser. On chrome shortcut is ctrl+shift+i. Select the network tab, do the action you want and watch what http requests is being sent. 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. Such work is excellently carried by library called Selenium webdriver. It's a headless web browser that you can control from your code. Try it.

EDIT: Selenium do not work on Android. Android has built in view that you can use as browser - WebView. And it can run javascript functions. You can do javascript calls. Look here and here. Good luck

Community
  • 1
  • 1
callOfCode
  • 893
  • 8
  • 11
  • Thanks Justas. I'll take a look. – TommyTin Feb 20 '16 at 14:54
  • Another thing to mention is that WebView is one of the most heavyweight components, so it must be used with caution. In that case I would run that script on webserver and send requests from your app to your own webapp that does all heavy work for you. – callOfCode Feb 20 '16 at 15:03
  • Thanks. The intended function is quite lightweight but i'll keep that in mind. Would you recommend HtmlUnit? It seems to be the closest to what i am looking for but many have complained over its various compile time errors... – TommyTin Feb 20 '16 at 15:06
  • I haven't used it but they claim that HtmlUnit javascript support is excellent so give it a try. – callOfCode Feb 20 '16 at 15:13