谷歌计算器的计算结果为Android [英] google calculator results for android

查看:406
本文介绍了谷歌计算器的计算结果为Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提出这要求发送一个数学问题,像1 + 1谷歌的计算器一个Android应用程序,我需要得到它显示在网页上的结果。我如何在Android做到这一点?

I am making an android application which requires to send a mathematical question like 1+1 to google's calculator and I need to get that result which is displayed on the web. How can I achieve this on android?

推荐答案

一种可能性是为您要计算,然后用一个URLConnection打开URL和阅读网页源$ C ​​$ C的方程网址找到答案的方程式。

One possibility is to create a URL for the equation you are trying to calculate and then use a URLConnection to open the URL and read the webpage source code to find the answer to the equation.

例如,如果您有公式:

2 + 2

接着的URL来计算与谷歌浏览器计算器的结果将是:
<一href=\"https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=2%2B2\" rel=\"nofollow\">https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=2%2B2

Then the URL to calculate the result with the Google Chrome calculator would be: https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=2%2B2

您必须构建在URL为你解方程正确的查询。在这个网址末尾的查询具有方程2 + 2

You will have to construct the proper query in the URL for the equation you are solving. In this URL the query at the end has the equation 2+2:

Q = 2%2B2的(其中,%2B重新presents +号)

q=2%2B2 (where the %2B represents the + sign)

构造URL与一个URLConnection打开并阅读源后。答案的方程式将在该元素:

After constructing the URL open it with a URLConnection and read the source. The answer to the equation will be in this element:

<span class="cwcot" id="cwos">4</span>

所以,你可以为了找到特定span元素和检索式的结果分析源。

So you can parse the source in order to find that particular span element and retrieve the result of your equation.

这是比你预期的可能更多的工作,但它是我能想到的来完成你的要求的唯一解决方案。此外,这种方法可能是易出错的,并且可以很容易地断裂。我会考虑用完全不同的方法,如发动意图使用计算器应用程序的移动设备(尽管这种方法也有问题,以及)上。

This is probably more work than you expected but it is the only solution I can think of to accomplish what you asked. Also, this approach may be error prone and may break easily. I would consider using a different approach altogether such as launching an intent to use the calculator app on the mobile device (even though this approach has issues as well).

编辑:

这工作对我来说(这将输出:2 + 2 = 4):

This worked for me (it will output: 2 + 2 = 4):

public static void test() {
    try {
        String source = getUrlSource();
        String span = "<span class=\"nobr\"><h2 class=\"r\" style=\"display:inline;font-size:138%\">";

        int length = span.length();         
        int index = source.indexOf(span) + length;

        String equation = source.substring(index, source.indexOf("<", index));

        System.out.println( "equation: " + equation);           
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static String getUrlSource() throws IOException {
    String url = "https://www.google.com/search";

    String charset = "UTF-8";
    String param1 = "2+2";

    String query = String.format("?q=%s", URLEncoder.encode(param1, charset));

    HttpsURLConnection urlConn = (HttpsURLConnection)new URL(url + query).openConnection();

    urlConn.setRequestProperty("User-Agent", "Mozilla/5.0");
    urlConn.setRequestProperty("Accept-Charset", charset);

    BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
    String inputLine;
    StringBuilder a = new StringBuilder();
    while ((inputLine = in.readLine()) != null)
        a.append(inputLine);
    in.close();

    return a.toString();
}

这篇关于谷歌计算器的计算结果为Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆