0

I'm following the tutorial to download content from webpage. http://developer.android.com/training/basics/network-ops/connecting.html#download (code is copied below so you don't have to go to this link)

It use len = 500 in this example and I change it to big value such as 50000 but while experimenting I realize this method will only download the first 4048 characters of a webpage no matter how large I set len to be. So I'm wondering if I should use another method to download web content.

Actually I'm not downloading normal webpage, I've put a php script on my server to search in my database then encode a json array as the content of the page, it's not very large, about 20,000 characters..

Main codes from the above link:

// Given a URL, establishes an HttpUrlConnection and retrieves
// the web page content as a InputStream, which it returns as
// a string.
private String downloadUrl(String myurl) throws IOException {
InputStream is = null;
// Only display the first 500 characters of the retrieved
// web page content.
int len = 500; // I've change this to 50000

try {
    URL url = new URL(myurl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(10000 /* milliseconds */);
    conn.setConnectTimeout(15000 /* milliseconds */);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    // Starts the query
    conn.connect();
    int response = conn.getResponseCode();
    Log.d(DEBUG_TAG, "The response is: " + response);
    is = conn.getInputStream();

    // Convert the InputStream into a string
    String contentAsString = readIt(is, len);
    return contentAsString;

// Makes sure that the InputStream is closed after the app is
// finished using it.
} finally {
    if (is != null) {
        is.close();
    } 
  }
}  


// Reads an InputStream and converts it to a String.
public String readIt(InputStream stream, int len) throws IOException,        UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");        
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
}
Arch1tect
  • 4,128
  • 10
  • 48
  • 69

1 Answers1

1

Are you sure it's not just LogCat truncating the message?

(Android - Set max length of logcat messages)

Try:

  • Printing out line by line in your readIt method
  • Doing this (Displaying More string on Logcat)
  • Saving to SD card and looking at the file
  • Actually doing what you want to do with it (put it in a TextView or whatever)
Community
  • 1
  • 1
Ken Wolf
  • 23,133
  • 6
  • 63
  • 84