1

I have a problem when I trying to download website source in 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)
{
    //
}

When I run this code on AVD 2.2 textbox contains all website source but when i run this on my phone with Android 2.3.5 (or ADV 2.3.3) textbox contains only few lines.

It looks exactly like this:

  • my phone = 1012 bytes:

    <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 bytes

What am I doing wrong?

Thank you in advance and sorry for my English. Patrick

John
  • 339
  • 1
  • 3
  • 13
  • i don't know the answer for this , but don't forget to put network-related operations in a separate thread , as newer versions of android would crash on this case . read here: http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html – android developer Oct 22 '12 at 22:05

1 Answers1

0

BufferedReader would make it better. I think InputStream may have some limited amount of bytes you can read.

Community
  • 1
  • 1
cyborg86pl
  • 2,597
  • 2
  • 26
  • 43