亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

而不是 JSON 數據鏈接返回.txt 文件如何在 android 中獲取 JSON 數據

而不是 JSON 數據鏈接返回.txt 文件如何在 android 中獲取 JSON 數據

蕪湖不蕪 2023-04-26 13:49:13
當我在瀏覽器中打開該鏈接時,我正在使用該鏈接,它會生成一個 json.txt 文件并下載它。我想在 android 應用程序中獲取這個 txt 文件,并想從這個 txt 文件中獲取 JSON 數據。
查看完整描述

1 回答

?
暮色呼如

TA貢獻1853條經驗 獲得超9個贊

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

    }


}


查看完整回答
反對 回復 2023-04-26
  • 1 回答
  • 0 關注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號