Solution 1 :
You could try using the HTTPS protocol instead of the HTTP protocol, as described here.
Problem :
I switched my hosting provider from Hostinger to Digital Ocean Spaces, the files are the same, only the url is different.
Playing the hosted mp3 file with media player still works with the new URL but downloading it doesn’t.
I get the following crash:
java.io.FileNotFoundException: https://myapp-content.ams3.digitaloceanspaces.com/MyApp/sounds/b9a14d77-d122-4bd9-9f2d-5dd7ad9b90ee.mp3
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:255)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:211)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:30)
at com.myapp.d.EventHandlerClass$4.run(EventHandlerClass.java:252)
at java.lang.Thread.run(Thread.java:920)
The link is working (not the one I posted here) even if I access it in inkognito, i can also download the sound in chrome.
But somehow the downloading process in my app doesnt work with the mp3 files hosted on Digital Ocean.
Any Ideas?
This line is causing the problem:
InputStream inputStream = urlConnection.getInputStream();
With the old hosting provider I’m getting status code 200, with the new one 400
Here is the whole download Function:
try {
URL url = new URL(filePath);
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
File storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File directory = new File(storage.getAbsolutePath());
directory.mkdirs();
String fileNameWithoutSpecialCharacters = fileName.replaceAll("\W", "");
file = new File(directory, fileNameWithoutSpecialCharacters + ".mp3");
FileOutputStream fileOutput = new FileOutputStream(file);
Log.i("download:", "Code: " + urlConnection.getResponseCode());
InputStream inputStream = urlConnection.getInputStream();
totalSize = urlConnection.getContentLength();
int downloadedSize = 0;
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
Log.i("download:", downloadedSize + "/" + totalSize);
}
fileOutput.close();
ClipboardManager clipboard = (ClipboardManager) MainActivity.mainActivityWeakReference.get().getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("test", "test");
clipboard.setPrimaryClip(clip);
} catch (IOException e) {
e.printStackTrace();
ClipboardManager clipboard = (ClipboardManager) MainActivity.mainActivityWeakReference.get().getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("test", e.toString());
clipboard.setPrimaryClip(clip);
}
Comments
Comment posted by luckrd
The HTTP code 400 means that, from the server’s point of view, an incorrect request was sent. Please include some more code so that we can understand how the request is generated.
Comment posted by HavanaSun
@Sapphirex I added the download function to my question. This function works perfectly fine with my old URL
Comment posted by HavanaSun
Unfortunately this is not working (I updated my question with the new code) The files hosted on Hostinger work no matter if its http or https, the files on DigitalOcean don’t work at all. Here is a working link with a audio file which is not downloadable