从html正文中读取文本

时间:2011-10-12 07:38:00

标签: android

在我的Android应用程序中,我正在使用用户名和密码注册用户,如果正确完成,则返回值“1”,如果不是则返回“0”,方法是调用带有用户名和密码作为参数的URL,结果为0或者1在html体中显示。

我的注册效果很好,但是html结果0或1没有显示,而不是显示

    java.io.BufferedInputStream@23888f88

我写了一个代码来读取html数据

public String getDataFromURL(String urlString) throws Exception {
    String response="";
    URL url = new URL(urlString);
       HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
       try {
         InputStream in = new BufferedInputStream(urlConnection.getInputStream());
         //readStream(in);
         response = in.toString();
       }
        finally {
         urlConnection.disconnect();
       }

       return response;
}

现在我该怎么做才能获得html正文?

由于

1 个答案:

答案 0 :(得分:1)

你可以试试这个。 String str将包含您的响应。

String str;
 try
        {
            HttpClient client = new DefaultHttpClient();
            HttpPost postalcall = new HttpPost("http://www.page.com/page.php");
            HttpResponse response = client.execute(postalcall);


            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
            {
                str = EntityUtils.toString(response.getEntity());
            }
        }catch(IOException e){
            e.printStackTrace();
        }  

你可以将它放入一个功能等

Public String fetchContent(String http) {
    String str;
     try
            {
                HttpClient client = new DefaultHttpClient();
                HttpPost postalcall = new HttpPost(http);
                HttpResponse response = client.execute(postalcall);


                if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
                {
                    str = EntityUtils.toString(response.getEntity());
                }
            }catch(IOException e){
                e.printStackTrace();
            }  
     return str;

}

然后致电

String response = fetchContent("http://www.page.com/page.pgp");

希望有所帮助