-1

次のコードを使用して zip を sd カードにダウンロードしようとすると、NullpointerException が発生します。いくつかの尋問を試みたとき、zipファイルが実際にはダウンロードされていないことがわかりました。コードに何らかの変更が必要かどうか、または zip に何か問題があるかどうかを助けていただけますか? Mはその点だけにこだわった。助けてください...私のコードは次のとおりです...

    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String from = "http://192.168.1.63/ZipFile/Text.zip" ;
        String to = Environment.getExternalStorageDirectory() + "/newunzip/";

        try {
            ((TextView) findViewById(R.id.display)).append("\n in try after function :");
            downloadFile(from, to);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            ((TextView) findViewById(R.id.display)).append("\n \n Exception occured :");
            ((TextView) findViewById(R.id.display)).append("\n \n Exception message is :"+e.getMessage());
            ((TextView) findViewById(R.id.display)).append("\n \n Exception is :"+e.toString());
        }

    }

    private void downloadFile(String from, String to) throws Exception 
    {
        ((TextView) findViewById(R.id.display)).append("\n \n in function call :");
        HttpURLConnection conn = (HttpURLConnection)new URL(from).openConnection();
        conn.setDoInput(true);
        conn.setConnectTimeout(100000); // timeout 100 secs
        conn.connect();

        ((TextView) findViewById(R.id.display)).append("\n \n Connecting to url :"+ conn);
        InputStream input = conn.getInputStream();

        byte[] b = null;
        input.read(b);
        ((TextView) findViewById(R.id.display)).append("\n \n input method :"+ b);

        FileOutputStream fOut = new FileOutputStream(to);

        byte[] b1 = null;
        input.read(b1);
        ((TextView) findViewById(R.id.display)).append("\n \n output method :"+ b1);

        int byteCount = 0;
        byte[] buffer = new byte[4096];
        int bytesRead = -1;
        while ((bytesRead = input.read(buffer)) != -1) 
        {
            ((TextView) findViewById(R.id.display)).append("\n \n reading/writing files :");
            fOut.write(buffer, 0, bytesRead);
            byteCount += bytesRead;
        }
        fOut.flush();
        ((TextView) findViewById(R.id.display)).append("\n \n flush & close :");
        fOut.close();
    }

}
4

1 に答える 1

0

You are using null byte data to read from stream;

    byte[] b = null;
    input.read(b);
    ((TextView) findViewById(R.id.display)).append("\n \n input method :"+ b);

Comment these lines.

于 2011-06-03T05:17:34.943 に答える