【问题标题】:Instead of JSON data link is returning .txt file how to get JSON data in android而不是 JSON 数据链接返回 .txt 文件如何在 android 中获取 JSON 数据
【发布时间】:2019-12-06 00:55:32
【问题描述】:

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

【问题讨论】:

  • 您可以将文件下载到 Android 设备中的特定文件夹,然后在您的代码中获取该文件,然后将其中的数据提取为 Json。
  • @yash786 这是一个实时应用程序然后存储文件和获取文件需要太多时间我想在运行时处理。
  • 您可以在运行时处理这些事情,无需担心您必须在后台线程中下载文件,下载完成后您可以访问该文件并从中提取 Json 数据。跨度>
  • @yash786 你能帮我看看如何在后台下载文件并在应用程序中阅读。我在这里没有找到任何帮助。因为每个人都在下载文件并存储它。
  • 我会帮助你的,我们一步一步来。首先尝试在 AsyncTask 的帮助下下载文件

标签: java android json file fetch


【解决方案1】:
new DownloadFileFromURL().execute("your_file_downloadable_url");

class DownloadFileFromURL extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        System.out.println("Starting download");


    }

    /**
     * Downloading file in background thread
     * */
    @Override
    protected String doInBackground(String... f_url) {
        int count;
        try {
            String root = Environment.getExternalStorageDirectory().toString();

            System.out.println("Downloading");
            URL url = new URL(f_url[0]);

            URLConnection conection = url.openConnection();
            conection.connect();
            // getting file length
            int lenghtOfFile = conection.getContentLength();

            // input stream to read file - with 8k buffer
            InputStream input = new BufferedInputStream(url.openStream(), 8192);

            // Output stream to write file

            OutputStream output = new FileOutputStream(root+"/downloadedfile.txt");
            byte data[] = new byte[1024];

            long total = 0;
            while ((count = input.read(data)) != -1) {
                total += count;

                // writing data to file
                output.write(data, 0, count);

            }

            // flushing output
            output.flush();

            // closing streams
            output.close();
            input.close();

        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }

        return null;
    }



    /**
     * After completing background task
     * **/
    @Override
    protected void onPostExecute(String file_url) {
        System.out.println("Downloaded");

        pDialog.dismiss();
    }

}

【讨论】:

  • 它在下载文件时进行安全检查,这在浏览器中运行良好。 2019-07-29 11:08:06.383 16000-16061/com.camera.pro.s10.procamera.app.downloadfile E/错误:发生:java.io.FileNotFoundException:google.com/sorry/index?continue=https://…
  • @MujahidRasool 在课程外,它会给出一个安全检查错误,您必须接受运行时权限才能读取和写入存储。
  • @MujahidRasool 我希望您已成功下载文件并将其存储在您的手机存储中。?是的,那么我们可以继续下一步从本地存储中检索文件。
  • 我在我的服务器上创建了一个外部 api,然后我从我自己的服务器访问此链接,因为它正在给我进行安全检查。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-11
  • 2014-07-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-20
相关资源
最近更新 更多