开发者

read text from html body

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.

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

    java.io.BufferedInputStream@23888f88

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 {开发者_JAVA技巧
         InputStream in = new BufferedInputStream(urlConnection.getInputStream());
         //readStream(in);
         response = in.toString();
       }
        finally {
         urlConnection.disconnect();
       }

       return response;
}

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

Thanks


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;

}

and just call

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

Hope it helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜