而不是 JSON 数据链接返回.txt 文件如何在 android 中获取 JSON 数据

当我在浏览器中打开该链接时,我正在使用该链接,它会生成一个 json.txt 文件并下载它。我想在 android 应用程序中获取这个 txt 文件,并想从这个 txt 文件中获取 JSON 数据。



芜湖不芜
浏览 107回答 1
1回答

暮色呼如

new DownloadFileFromURL().execute("your_file_downloadable_url");class DownloadFileFromURL extends AsyncTask<String, String, String> {&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Before starting background thread&nbsp; &nbsp; &nbsp;* */&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onPreExecute() {&nbsp; &nbsp; &nbsp; &nbsp; super.onPreExecute();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Starting download");&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Downloading file in background thread&nbsp; &nbsp; &nbsp;* */&nbsp; &nbsp; @Override&nbsp; &nbsp; protected String doInBackground(String... f_url) {&nbsp; &nbsp; &nbsp; &nbsp; int count;&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String root = Environment.getExternalStorageDirectory().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Downloading");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; URL url = new URL(f_url[0]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; URLConnection conection = url.openConnection();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conection.connect();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // getting file length&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int lenghtOfFile = conection.getContentLength();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // input stream to read file - with 8k buffer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InputStream input = new BufferedInputStream(url.openStream(), 8192);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Output stream to write file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OutputStream output = new FileOutputStream(root+"/downloadedfile.txt");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte data[] = new byte[1024];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long total = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((count = input.read(data)) != -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total += count;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // writing data to file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.write(data, 0, count);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // flushing output&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.flush();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // closing streams&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input.close();&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e("Error: ", e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* After completing background task&nbsp; &nbsp; &nbsp;* **/&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onPostExecute(String file_url) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Downloaded");&nbsp; &nbsp; &nbsp; &nbsp; pDialog.dismiss();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java