How to read a file from the internet
While coding y next app for Android, I found out that I needed to read files from URL locations and saven them on my Android device.
I searched for the first sample and put that code to work in my app. That was a quick disappointment. It dit not work and it gave me the message:
DefaultRequestDirector(5824): Authentication error: Unable to respond to any of these challenges: {}
I did not know what to make of this, since my file on the net is not protected by authentication. A lot of searching on the web did not reveal any solution to my problem (only solutions where you had to authenticate).
After long hours of searching I found a workaround for my problem. Just send in some dummy authentication and it will work.
HTTPClient / UrlConnection example
Here is my sample of my workaround
HttpGet httpget = new HttpGet(_url);
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin","admin"));
String _generatedFilename = generateFilename(_filename, savedconfiglist);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (response.getStatusLine().getStatusCode()==200) {
saveArmyConfig(_generatedFilename, entity.getContent());
} else {
Log.d("TESTING", "error receiving content");
}
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
Kind regards,
Marc