2012-10-22 50 views
1

我嘗試在Android下載網站源代碼時出現問題。正在下載網站源代碼。 Android

EditText textbox = (EditText) findViewById(R.id.editText1); 

URL link; 
HttpURLConnection httpcon; 
String wynik; 

try 
{ 
    link = new URL("http://stackoverflow.com/faq"); //example site 

    httpcon = (HttpURLConnection) link.openConnection(); 
    httpcon.connect(); 

    InputStream inpText = null; 
    inpText = link.openStream(); 
    byte[] bText = new byte[httpcon.getContentLength()]; 
    int readSize = inpText.read(bText); 
    wynik = new String(bText); 
    httpcon.disconnect(); 
    inpText.close(); 

    textbox.append(wynik); 
} 
catch(Exception e) 
{ 
    // 
} 

當我運行AVD 2.2文本框的代碼包含了所有網站的源,但是當我的手機採用Android 2.3.5(或ADV 2.3.3)上運行該文本框只包含幾行。

它看起來完全一樣:

  • 我的手機= 1012個字節:

    <title>Frequently Asked Questions - Stack Overflow</title> 
    <link rel="shortcut icon" href="http://cdn.sstatic.net/stackoverflow/img/favicon.ico"> 
    <link rel="apple-touch-icon" href="http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png"> 
    <link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href="/opensearch.xml"> 
    
    
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
    <script type="text/javascript" src="http://cdn.sstatic.net/js/stub.js?v=8a629d6e9fb6"></script> 
    <link rel="stylesheet" type="text/css" href="http://cdn.sstatic.net/stackoverflow/all.css?v=645537e2c00d"> 
    
    <script src="http://cdn.sstatic.net/Js/help.js?v=fc9fb0517db2" type="text/javascript"></script> 
    <script type="text/javascript"> 
        StackExchange.ready(function() { 
         StackExchange.help.init({ host: 'stackoverflow.com', bgColor: '#FFF', bg 
    
  • ADV 2.2 = 50個984字節

我在做什麼錯?

預先感謝您,並對我的英語感到抱歉。 Patrick Patrick

+0

我不知道這個答案,但不要忘記把網絡相關的操作放在一個單獨的線程中,因爲新版本的android會在這種情況下崩潰。在這裏閱讀:http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html –

回答

0

BufferedReader會讓它變得更好。我認爲InputStream可能有一些你可以讀取的字節數量有限。

+0

Thx。一切都與BufferedReader一起工作 – John