Android - download JSON file from url

前端 未结 4 1158
执念已碎
执念已碎 2020-12-18 04:58

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 .t

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-18 05:11

    Basically you would do something like:

    Code:

    URL address = URL.parse("http://yoururlhere.com/yourfile.txt"); 
    URLConnection conn = new URLConnection(address);
    InputStream is = conn.getInputStream(); 
    BufferedInputStream bis = new BufferedInputStream(is);
    ByteArrayBuffer bab = new ByteArrayBuffer(64); 
    int current = 0;
    
    while((current = bis.read()) != -1) {
      bab.append((byte)current); 
    }
    
    FileOutputStream fos = new FileOutputStream(new File(filepath));
    fos.write(bab.toByteArray());
    fos.close();
    

提交回复
热议问题