SQLite does not insert data into the database during an AsyncTask

1

I'm trying to add data in the DB inside an AsyncTask. In it I get the InputStream coming from an httpURLConnection, interpreting the JSON and saved in the local SQLite. But I do not even know if this is the best "practice" to do ...

            URL url = new URL(URL);
            httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 ( compatible ) ");
            httpURLConnection.setRequestProperty("Accept", "*/*");
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);

            try (DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream())) {
                wr.write(data);
                wr.flush();
            } catch (Exception e) {
                Log(Tags.WSERROR, e.getMessage());
            }

            int status = httpURLConnection.getResponseCode();

            if (status != HttpURLConnection.HTTP_OK) {
                inputStream = httpURLConnection.getErrorStream();
                Log(Tags.WSERROR, "Code: " + httpURLConnection.getResponseCode() + "\t\tMsg: " + httpURLConnection.getErrorStream());
            } else {
                inputStream = httpURLConnection.getInputStream();
            }

            mapItens(inputStream);
            if (inputStream == null) {
                return null;
            }

The MapItens method takes care of interpreting the data and saving it to the local DB, it looks like this:

Map<String, String> mapPedidos(InputStream inputStream) throws IOException {

    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    String linha;

    StringBuffer buffer = new StringBuffer();
    while ((linha = reader.readLine()) != null) {
        buffer.append(linha + "\n");
    }

    Log(Tags.WSINFO, "Itens - " + buffer.toString());
    if (buffer.length() == 0) {
        return null;
    }

    List<Itens> registers = new ArrayList<>();
    try {
        JSONObject req = new JSONObject(buffer.toString());
        Log(Tags.WSINFO, "Itens são: " + req.toString());

        JSONArray locs = req.getJSONArray("DADOS");
        Log(Tags.WSINFO, "locs SIZE: " + locs.length());

        for (int i = 0; i < locs.length(); ++i) {
            JSONObject rec = locs.getJSONObject(i);
            Itens it = new Pedido();
            it.nome = rec.getString("IT_NOME");
            it.desc = rec.getString("IT_DESC");
            registers.add(it);
        }


    } catch (JSONException e) {
        Log(Tags.WSERROR, e.getMessage());
    } finally {


        for (Itens i : registers) {
            Log(Tags.WSINFO, "Item : " + i.nome + " sendo criado!");
            boolean r = i.create();

            if (r)
                Log(Tags.WSINFO, i.nome + " created!");
            else
                Log(Tags.WSINFO, i.nome + " error");
        }

        Log(Tags.WSINFO, "Count in DB: " + getInstance().getCount());
        getInstance().CloseDB();
    }

    ArrayList<Itens> p = Itens.getAll();
    Log(Tags.WSINFO, "Select nos itens iniciando");
    if (p != null)
        for (Itens pe : p) {
            Log(Tags.WSINFO, "Itens " + pe.nome);
        }
    Log(Tags.WSINFO, "Select nos itens finalizando");

    if (httpURLConnection != null) {
        httpURLConnection.disconnect();
    }

    if (reader != null) {
        reader.close();
    }

    return getData;

}

I made this same logic in another class to get other information from another webservice. It works perfectly.

Another point is that there is no exception in the insertion of the dodos in the DB, the result comes out as true, the getCount () of the table returns me number of positive re-registrations. In all logs, the information is displayed correctly. When I do the select, ArrayList of items comes null , apparently. The biggest problem is that in the rest of every application, these SQLite select methods work normally.

    
asked by anonymous 06.12.2018 / 14:43

0 answers