2011-10-12 77 views
0

在我的Android應用程序中,我使用用戶名和密碼註冊用戶,如果正確完成,則返回值「1」,如果不是則通過調用帶有用戶名和密碼的URL作爲參數的「0」並且結果0或1顯示在html正文中。從html正文中讀取文本

我的註冊是完全做,但HTML結果0或1沒有顯示,而不是顯示

[email protected] 

,我寫了一個代碼閱讀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

你可以試試這個。字符串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"); 

希望它可以幫助

+0

非常感謝你:) – fean

+0

你的大多是歡迎。 –