03-02-2013 01:01 AM
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
03-02-2013 04:32 AM
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.
03-02-2013 04:57 AM
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
03-02-2013 04:57 AM
as u said i am creating a new confact,new condescriptor and call d getconenction with the redirected url
03-02-2013 05:27 AM
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) {
}
}
03-02-2013 10:13 AM
Ah - I see you got this code from one of my samples:
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.
03-05-2013 03:36 AM
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
03-05-2013 04:16 AM - edited 03-05-2013 04:17 AM
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.
03-12-2013 12:54 AM
Ya did and fixed