读取HTML正文 [英] read text from html body

查看:200
本文介绍了读取HTML正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用我登录了用户名和密码的用户,如果做的正确的返回值1如果不是那么0,通过调用一个网址作为参数的用户名和密码,结果0或1显示HTML主体。

In my android application i'm signing up a user with username and password, if done correctly it returns value "1" if not then "0" by calling a URL with a username and password as parameters and the result 0 or 1 shows in html body.

我的注册是完全做什么,但HTML结果0或1没有显示,而不是显示

my signup is doing perfectly but the html result 0 or 1 is not showing, instead of showing

    java.io.BufferedInputStream@23888f88

我写了一个code阅读HTML数据

I've written a code to read the html data

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正文?

Now what should i do to get the html body text ?

感谢

推荐答案

您可以试试这个。字符串str将包含您的答复。

You could try this. The String str will contain your response.

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();
        }  

您可以把它变成一个功能等。

You can put it into a function etc

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");

希望它能帮助

这篇关于读取HTML正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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