Use DownloadManager to download from an URL accessible through a DefaultHttpClient - android

In my Android app I have to download files from URLs. I use the Android DownloadManager but the URLs are accessible only through a DefaultHttpClient. Infact these URLs are the result of several POST requests. I mean: I'm allowed to download the files only if I'm logged in as user. Could someone help me in managing this situation? I think that it's not possibile to use the DM to download the files by putting directly the link of the requested resource. When I have to download a file there already is an active DefaultHttpClient and I'm logged in.
The code I use is the following:
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(Uri.parse(client.HttpsGet(url));
enqueue = dm.enqueue(request);
The HttpsGet is a method of my custom class HttpsClient that does a HTTP GET. In this case I obtain the file encoded in text (like if I open a pdf with notepad). If I directrly put the url of the file (without calling the HttsGet) the devices downloads a blank file.

For posterity...
I solved my problem thanks to this thread: How do download a file with login using HttpURLConnection
But I had to renounce to the DownloadManager
Hope this helps!

Related

Content-Disposition response header in DownloadManager

I have to download a file and was using downloadManager for it. The filename for the file resides in the Content-Disposition of the response header.
Is there anyway of accessing the response header from DownloadManager.Request class or downloadManager could support this out of the box.
I don't want to make a HEAD api call to the server and increase the server hit count. We can assume we don't have HEAD call functionality.
You maybe have a look to OkHttp3 instead.
implementation 'com.squareup.okhttp3:okhttp:3.14.1'

Android DownloadManager/DOWNLOAD_SERVICE: how do you decode content-encoding: gzip?

I can't seem to be able to get DownloadManager in Java/Android properly decode gzip files (in this case from S3 bucket). Same link decodes properly when downloading from, say, Chrome, which seems to use DownloadManager as well. There doesn't seem to be anything in the docs that explain how to do this.
here's some sample code. The result is the file gets downloaded, but is still in gzipped format. Not the case when downloading with chrome. How do I do this properly?
downmanager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(Uri.parse("https://somurl.com/somefile.wad"));
request.addRequestHeader("Accept-Encoding", "gzip, deflate");
Uri localUri = Uri.parse("file://"+this.getExternalFilesDir(null).getPath()+"/test.wad");
request.setDestinationUri(localUri);
downmanager.enqueue(request);

Setting the proxy for android's DownloadManager class

I have an app with a WebView, which downloads videos and plays them in a VideoView.
To manage the downloads I use android's handy DownloadManager API.
Unfortunately in some situations I need to use a proxy.
I have successfully set up the proxy for the WebView using reflection as detailed in this stackoverflow question, but I am not sure how I can set the DownloadManager to use a proxy as well..
Is this possible?
If not, what are my alternatives?
Thanks
I couldn't find a way to do this with the DownloadManager so I ended up implementing my own (simplified) download manager using an AsyncTask.
It's possible then to pass a Proxy object to Url.openConnection as below:
Proxy proxy = new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(proxyHost, proxyPort));
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
Once you've got the proxied connection you can download content as per usual.

embedded http server on android to server assets folder

I am using an embedded http server from the following example on
https://github.com/nikkiii/embedhttp
The http server code is initiated as
File filesDir = getFilesDir();
HttpServer server = new HttpServer();
server.addRequestHandler(new HttpStaticFileHandler(new File(".")));
server.addRequestHandler(new HttpStaticFileHandler(filesDir));
server.bind(8081);
// Start the server thread
server.start();
The http server is embedded so as to serve a www folder from the server as http://localhost:8081/folderpath/index.html . Then in turn I will be using WebView to access the localhost url.
In webview I try to access.
myWebView.loadUrl("`http://localhost:8081/`");
myWebView.loadUrl("`http://localhost:8081/index.html`");
myWebView.loadUrl("`http://localhost:8081/www/index.html`");
myWebView.loadUrl("`http://localhost:8081/assets/www/index.html`");
But none of them work. What is the probable solution to such a situation where the assets/www flder needs to be served from localhost running on android. Thanks.
The Android assets folder is not available via getFilesDir(): getFilesDir() returns a different directory. To serve resources in the assets folder you'll need to use AssetManager (http://developer.android.com/reference/android/content/res/AssetManager.html) and do a little translation from your request paths to the appropriate resource.

Access a folder from a server on Android

If I want to access folders that are uploaded on a server, how would I access it from the Android project? I tried to use the following
public URL(String protocol, String host, String file)
My Codes:
URL url = new URL("file", "www.websitename.com", "/folderX");
MyClassName path = new MyClassName(url);
Seems like it couldn't access the folder on the server still. Is there anything that I miss or I should be using some other ways? There are no errors in the logcat so I am not sure what is it. No MalformedURLException being thrown as well.
I you want to access the folder and its files from your server you need FTP account details to connect to your server from your android device.There are more FTP Client libraries available to access the files one of them is
http://commons.apache.org/net/download_net.cgi

Categories

Resources