从URL获取HTML源代码

时间:2012-09-10 11:17:27

标签: android html url get

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

将当前的USD-> INR转换率作为文本返回。

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

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

例如:

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权限,我的网络也正常运行。

1 个答案:

答案 0 :(得分:0)

使您的网址类似,

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

此外,它的结果是 JSON 而不是 HTML ..所以解析结果就像JSON ..