2012-10-22 45 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