Http URL connection can’t read completely

Put your problem here if it does not fit any of the other categories.

Http URL connection can’t read completely

Postby JamesAndroid » Mon Dec 21, 2009 9:32 am

Hi guys.

I am facing with problem related Http Connection. My code :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. URL url = null;
  5.  
  6. try {
  7.  
  8. url = new URL(_url);
  9.  
  10. } catch (MalformedURLException e) {
  11.  
  12. }
  13.  
  14. if (url != null) {
  15.  
  16. HttpURLConnection urlConn = null;
  17.  
  18. InputStreamReader isr = null;
  19.  
  20. try {
  21.  
  22.         urlConn = (HttpURLConnection)url.openConnection();
  23.  
  24.     urlConn.setRequestMethod("GET");
  25.  
  26.         urlConn.setConnectTimeout(45000);
  27.  
  28.         if(response == HttpURLConnection.HTTP_OK) {
  29.  
  30.             StringBuffer readData = new StringBuffer("");
  31.  
  32.         int size = 1024;
  33.  
  34.             char[] buffer = new char[size];
  35.  
  36.         int len;
  37.  
  38.             isr = new InputStreamReader(urlConn.getInputStream());
  39.  
  40.             while ((len = isr.read(buffer, 0, size)) > 0) {
  41.  
  42.                 readData.append(buffer, 0, len);
  43.  
  44.             }
  45.  
  46.         }
  47.  
  48.     } catche(Exception e) {
  49.  
  50.     } finally {
  51.  
  52.       if(urlConn != null) {
  53.  
  54.            try {
  55.  
  56.                 urlConn.disconnect();
  57.  
  58.            } catch(Exception e) {                      
  59.  
  60.            }
  61.  
  62.           }
  63.  
  64.       if(isr != null) {
  65.  
  66.            try {
  67.  
  68.              isr.close();
  69.  
  70.            } catch(Exception e) {                              
  71.  
  72.       }                
  73.  
  74.     }
  75.  
  76. }
  77.  
  78.  
  79.  
  80.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


This code can't download data completely. For example : Total size to read : 13901 bytes Above code can read size : 12937 bytes

What is wrong here ?

Please advice guys.
JamesAndroid
Junior Developer
Junior Developer
 
Posts: 23
Joined: Mon Dec 07, 2009 7:23 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 22 guests