Android: One writer don't wirte to file

45 views Asked by At

Here is my function:

//This function is for parsing the online xml file and adding those to the ".txt" file
private void downloadData() throws Exception {
    Thread thread = new Thread(){
        public void run(){
            HttpURLConnection http = null;
            InputStream xmlStream = null;
            try {
                URL url = new URL(rateUrl);
                http = (HttpURLConnection) url.openConnection();
                XMLPullParserHandler parser = new XMLPullParserHandler();

                currency_name.add("EUR");
                currency_rate.add(1.0);
                parser.parse(http.getInputStream(), currency_name, currency_rate);

                //Uppdate ui on the ui-thread
                runOnUiThread(new Runnable(){
                    public void run() {
                        adapter.notifyDataSetChanged();

                        //Write to file, THIS IS WHERE THE PROBLEM IS
                        PrintWriter writer = null;
                        try {
                            OutputStream os = MainActivity.this.openFileOutput(currency_file, Context.MODE_PRIVATE);
                            writer = new PrintWriter(os);
                            //Currency data files
                            for(int i = 0; i < currency_name.size(); i++){
                                writer.println(currency_name.get(i).toString() + ":" + currency_rate.get(i).toString());
                            }

                            //Date file (time)
                            String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime());
                            OutputStream os2 = MainActivity.this.openFileOutput(time_file, Context.MODE_PRIVATE);
                            writer = new PrintWriter(os2);
                            writer.println(timeStamp);
                        }
                        catch(IOException ioe) {
                            showToast("Error while writing to files.");
                        }
                        finally {
                            if(writer != null) {
                                writer.close();
                            }
                        }
                    }
                });
            }
            catch (Exception e) {
                showToast("Some error");
            }
            finally{
                try{
                    if(xmlStream != null){
                        xmlStream.close();
                    }
                }
                catch (Exception e){
                    showToast("some error2");
                }
                if(http != null){
                    http.disconnect();
                }
            }
        }
    };
    thread.start();
}

On the outputstream (first one) when it is supposed to write to the currency_file, it wont do so, however the second writer dose write the date on in the time_file. have i missed anything?

0

There are 0 answers