0

" http://sample.com/test "のような xml データが大量にある URL に問題があり、mainUI にプログレス バーを表示できません。

必要なことをしてください。

4

2 に答える 2

1

AsyncTaskでは、バックグラウンド タスクが xml をローカル メモリに保存します

                    int count;
                    try {
                        URL url = new URL("http://sample.com/test");
                        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("/data/data/com.pc.demo/temp.xml");
                        byte data[] = new byte[1024];
                        long total = 0;
                        while ((count = input.read(data)) != -1) {
                            total += count;
                            int progress = (int)((total*100)/lenghtOfFile) ;
                            System.out.println("update---"+progress);
                            publishProgress(progress);
                            output.write(data, 0, count);
                        }

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

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

onPostExecute

     File yourFile = new File("/data/data/com.pc.demo/temp.xml");
     InputStream input =  new BufferedInputStream(new FileInputStream(yourFile), 8086);

onProgressUpdate

    setProgressPercent(progress[0])
于 2013-04-25T05:26:50.573 に答える