Android - 从url下载JSON文件 [英] Android - download JSON file from url

查看:172
本文介绍了Android - 从url下载JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从URL下载包含JSON数据的文件?此外,我需要获取的文件没有文件扩展名,这是一个问题还是我可以强制它下载时具有.txt扩展名?

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?

推荐答案

您是否尝试过使用URLConnection?

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));

这篇关于Android - 从url下载JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆