Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: setRequestProperty throwing error in HttpDownload

Hi Simon i implemented as u suggested,and i am able to read the content range now,thnx to you.But my problem is that if the url redirects to another url to fetch the actual resource to download,i am getting the same  "Stream not in setup state error" in the setrequestproerty line

 

 ConnectionFactory cnf=new ConnectionFactory();
            	ConnectionDescriptor conDescriptor = cnf.getConnection(currentFile);
            	 if(conDescriptor != null ) {
            		 conn = (HttpConnection) conDescriptor.getConnection();  
            		 conn.setRequestProperty("Range", "bytes=" +
         					rangeStart + "-" + rangeEnd);
            		 int status = conn.getResponseCode();
     	            
     	            System.out.println("==>> Connection Url :: "+ currentFile);
     	            System.out.println("==>> Response Code  :: " + status);
     	            
     	            if ((status == HttpConnection.HTTP_MOVED_TEMP) || (status == HttpConnection.HTTP_MOVED_PERM)){           
     					String redirectedUrl = conn.getHeaderField("Location");
     					System.out.println("==>> First redirectedUrl >>>"+redirectedUrl);
     					
     					conn = (HttpConnection) conDescriptor.getConnection();  
     					 conn.setRequestProperty("Range", "bytes=" +
              					rangeStart + "-" + rangeEnd);
     					status = conn.getResponseCode();
     				}

 Here is my code

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: setRequestProperty throwing error in HttpDownload

On a redirect you need to re-try the request, including adding any requestHeaders.  All the redirect does is provide you with a replacement URL to use.  You should create a new URL using this information and use that as a basis of your retry request, including asking ConnectionFactory for a new descriptor.  If you do that the processing should work correctly. 

Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: setRequestProperty throwing error in HttpDownload

if its 302 i am doing this now

 

ConnectionFactory cnf1=new ConnectionFactory();
     	            	ConnectionDescriptor conDescriptor1 = cnf1.getConnection(redirectedUrl);
     	            	 if(conDescriptor1 != null ) {
     	            		conn.close();
          					conn=null;
     	            		conn = (HttpConnection) conDescriptor1.getConnection();  
        					 conn.setRequestProperty("Range", "bytes=" +
                 					rangeStart + "-" + rangeEnd);
     	            	 }

 but not getting the content-range

Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: setRequestProperty throwing error in HttpDownload

as u said i am creating a new confact,new condescriptor and call d getconenction with the redirected url

Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: setRequestProperty throwing error in HttpDownload

Or else can you suggest me any other way aprt from the below procedure to show progressbar while downloading

 

if ( contentRangeHeader != null ) {
                    // See if we have finished
                    int indexOfSlash = contentRangeHeader.lastIndexOf('/');
                    if ( indexOfSlash > 0 && indexOfSlash < contentRangeHeader.length() - 1 ) {
                        // Extract the length (note, might by "*")
                        String fileLengthString = contentRangeHeader.substring(indexOfSlash+1);
                        // if this same String, prefixed by "-", precedes the /, then we have finished...
                        // But the range will actually be 1 less than the total (starts at byte 0)
                        try {
                            reportedFileSize = Long.parseLong(fileLengthString);
                            String checkForLength = "-" + Long.toString(reportedFileSize-1);
                            int checklength = checkForLength.length();
                            if ( checkForLength.length() < indexOfSlash ) {
                                // Room for it...
                                String endOfRangeLength = contentRangeHeader.substring(indexOfSlash - checklength, indexOfSlash);
                                if ( endOfRangeLength.equals(checkForLength) ) {
                                    if ( observer != null ) {
                                        observer.update(100);
                                    }
                                    break;
                                } else {
                                    if ( observer != null ) {
                                        int nextPercentage = (int) ((100 * totalSize)/reportedFileSize);
                                        if ( nextPercentage != _lastPercentageAdvised ) {
                                            observer.update(nextPercentage);
                                            _lastPercentageAdvised = nextPercentage;
                                        }
                                    }
                                }
                                    
                            }
                        } catch (Exception e) {
                        }
                    } 
                } else {
                    // not chunked - got it all...
                    break;
                }

                /*
                 * Pause to allow connections to close and other Threads
                 * to run.
                 */
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                }
            }

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: setRequestProperty throwing error in HttpDownload

Ah - I see you got this code from one of my samples:

http://supportforums.blackberry.com/t5/Java-Development/Sample-quot-Please-Wait-quot-Screen-part-3/t...

 

I see no reason why that code will not work.  Again I would look for a problem in your code. 

 

In pseudo code, the process I would us is as follows:

 

1) get ConnectionFactory Object and set appropriately

2) set URL you are using

3) get Descriptor

4) get Connection

5) set Headers

6) get response code

7) if 302, close connection, determine new URL and return to step 3

8) If 206 - you will have a content range to use and there is more

9) If you get a 416, then you will also have a content range and you probably requested off the end of the file so you have finished

.... you can figure out the rest.

 

The point here is the processing of the 302 - I am not sure it is correct.

 

And one final thing - what happens if you start the download with the correct URL and it does not get redirected?  if that works, then you have got to think that your redirect processing is not correct.  So test that to check.

 

Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: setRequestProperty throwing error in HttpDownload

hey peter i finally removed the getHeader of content range and tried with con.getcontentlength() and reading each byte and calculating the % which seem to have solved my problem.Now suppose my download is going on,and i want to stop it in between,it doesnt stops and instead completes the download,how to stop the thread

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: setRequestProperty throwing error in HttpDownload

[ Edited ]

If you are using the sample, there is a loop in DownloadCombiner starting:

 

while (true) {

 

Just change that to check a flag, have a method that sets the flag.  When you want to stop, call that method.  Then the loop will complete the next 'chunk, but that is all. 

Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: setRequestProperty throwing error in HttpDownload

Ya did and fixed