2

我有这个网址,http://www.google.com/ig/calculator?hl=en&q=1USD %3D%3FINR

它以文本形式返回当前的 USD->INR 转换率。

屏幕上显示的文本是页面 HTML 源代码中的唯一文本。

我一直在努力获取 HTML 源代码,因为我尝试过的很多东西都返回了异常。

例如:

public static String getHtml(String url)
throws ClientProtocolException, IOException
{

    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpGet httpGet = new HttpGet(url);
    HttpResponse response = httpClient.execute(httpGet, localContext);
    String result = "";

    BufferedReader reader = new BufferedReader(new InputStreamReader(
            response.getEntity().getContent()));

    result = reader.readLine();
    return result;
}

调用时返回异常

try {
String test = getHtml("google.com/ig/calculator?hl=en&q=1USD%3D%3FINR");
    Log.d("ASDADS", test);
    } catch (Exception e) {
        Log.d("ASDASD", "FAILED");
    }

另外,我在 Manifest 中添加了 INTERNET 权限,我的网络也正常工作。

4

1 回答 1

0

使您的网址类似于,

"http://www.google.com/ig/calculator?hl=en&q=1USD%3D%3FINR"

它的结果也是JSON而不是HTML ..所以像JSON一样解析结果..

于 2012-09-10T11:19:03.180 回答