Sqlite does not input data into the database during an Asynctask

Asked

Viewed 29 times

1

I’m trying to add data to DB inside an Asynctask. In it I catch the Inputstream coming from an httpURLConnection, I interpret the JSON and saved in the local Sqlite. But I don’t 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 method Mapitens takes care of the interpretation of the data and save in local DB, this so:

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 did that 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 dodos in DB, the result comes out as true, getCount() from the table returns me number of positive records. In all logs, the information is shown correctly. When I make 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.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.