Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - download JSON file from url

Is it possible to download a file with JSON data inside it from a URL? Also, the files I need to get have no file extension, is this a problem or can i force it to have a .txt extension upon download?

UPDATE: I forgot to mention, that the website requires a username and password entered in order to access the site which i know. There a way to input these values in as I retrieve the file?

like image 916
Jamie Avatar asked Feb 17 '11 09:02

Jamie


1 Answers

Have you tried using URLConnection?

private InputStream getStream(String url) {
    try {
        URL url = new URL(url);
        URLConnection urlConnection = url.openConnection();
        urlConnection.setConnectTimeout(1000);
        return urlConnection.getInputStream();
    } catch (Exception ex) {
        return null;
    }
}

Also remember to encode your params like this:

String action="blabla";
InputStream myStream=getStream("http://www.myweb.com/action.php?action="+URLEncoder.encode(action));
like image 79
Gero Avatar answered Oct 01 '22 08:10

Gero