Tecnologia do Blogger.
RSS

Re: [androidbrasil-dev] Erro na aplicação

Demorei bastante, eu sei!


Obs: Algumas variáveis estão em arquivos de "resources";
        No meu retorno eu coloco o total de registros na primeira linha por comodidade;
        Repare que eu exibo uma barra de progresso para o usuário e atualizo em intervalos de 100 registros para não cair novamente no problema de memória;
        A classe não está mais assim, 

Vamos lá, primeiro o download do arquivo:

private File DownloadFile(String URL)
{
InputStream in = null;
File file = new File(root, "agencies.txt");

try {
in = OpenHttpConnection(URL);
if (in.available() > 0) {
BufferedInputStream bis = new BufferedInputStream(in);

ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}

FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}

return file;                
}

Em seguida persisto os dados:

protected void setAgencies(File file) {
        try {
            agenciesModel.open();
            Long totalAgenciesDatabase = agenciesModel.getTotalAgencies();
            agenciesModel.close();

            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            Integer totalAgencies = gson.fromJson(in.readLine(), Integer.class);
            if (totalAgencies > totalAgenciesDatabase) {
                Agencies agency = AgenciesModel.getJsonAgency(in.readLine());

                notificationManager.notify(NMAGENCIE, notification);
                notification.contentView.setProgressBar(R.id.status_progress, totalAgencies, counter, false);

                agenciesModel.open();

                InsertHelper ih = new InsertHelper(agenciesModel.db, AgenciesModel.DATABASE_TABLE);

                final int colServerId = ih.getColumnIndex(AgenciesModel.KEY_SERVERID);               
                final int colName = ih.getColumnIndex(AgenciesModel.KEY_NAME);
                ...

                while (agency != null) {
                    try {
                        if (!agenciesModel.hasAgency(agency.getServerId())) {

                            ih.prepareForInsert();

                            ih.bind(colServerId, agency.getServerId());
                            if (agency.getUniorg() != null)
                                ih.bind(colName, agency.getName());
                            ...

                            ih.execute();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    counter++;

                    if (counter % 100 == 0) {
                        notification.contentView.setProgressBar(R.id.status_progress, totalAgencies, counter, false);
                        notificationManager.notify(NMAGENCIE, notification);
                    }

                    agency = AgenciesModel.getJsonAgency(in.readLine());
                };
                agenciesModel.close();
            }

            in.close();
        } catch (Exception e) {
            notifyError(e);
        }

        notificationManager.cancel(NMAGENCIE);
    }


Com o método getJsonAgency(string) eu chamo o método Gson.fromJson(string);

Espero que tenha ajudado!

--
Sabino, Regis S.
Programmer - Web Designer - Full .NET Platform
(55) 11 8545-9314 / (55) 11 2456-7901

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 comentários:

Postar um comentário