hi with this code i can download from public folder of my site but when i want to download from subfolder of site like http://xxxx.xxx.xx/xxx the download complete with 1 kb what's the problem?
B4X:
package anywheresoftware.b4a.downloadmanager;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.ResultReceiver;
import anywheresoftware.b4a.BA;
public class DownloadService extends IntentService {
public static final int UPDATE_PROGRESS = 8344;
public static final int FINISH = 8345;
boolean success;
ResultReceiver receiver;
HttpClient httpClient;
HttpResponse response;
public DownloadService() {
super("DownloadService");
}
@Override
protected void onHandleIntent(Intent intent) {
success = false;
String options[] = intent.getStringArrayExtra("options");
receiver = (ResultReceiver) intent.getParcelableExtra("receiver");
try {
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 30000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 30000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
httpClient = new DefaultHttpClient(httpParameters);
HttpGet request = new HttpGet();
request.setURI(new java.net.URI(options[0]));
request.addHeader("Range", "bytes=" + options[2] + "-");
response = httpClient.execute(request);
// this will be useful so that you can show a typical 0-100% progress bar
long fileLength = 0;
Header contentLengthHeader = response.getFirstHeader("Content-Length");
if (contentLengthHeader != null) {
fileLength = Integer.parseInt(contentLengthHeader.getValue());
}
// download the file
InputStream input = new BufferedInputStream(response.getEntity().getContent());
OutputStream output = new FileOutputStream(options[1],true);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
Bundle resultData = new Bundle();
resultData.putLongArray("progress", new long[]{total,fileLength});
receiver.send(UPDATE_PROGRESS, resultData);
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
success = true;
} catch (IOException e) {
BA.Log(e.getMessage());
success = false;
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
BA.Log(e.getMessage());
success = false;
}
}
@Override
public void onDestroy() {
httpClient.getConnectionManager().shutdown();
Bundle resultData = new Bundle();
resultData.putBoolean("finish", success);
receiver.send(FINISH, resultData);
}
}