Skip to main content
adicionou 4117 caracteres ao conteúdo
Fonte Link

Editado

Fonte do parsejson e insert no banco

final DBController sett = new DBController(getBaseContext());
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

final String formattedDate = df.format(c.getTime());

RequestQueue queue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        try {
            JSONObject json = response.getJSONObject("DATA");
            Iterator keys = json.keys();

            while(keys.hasNext()) {
                String currentDynamicKey = (String)keys.next();
                if (!currentDynamicKey.equals("SYSTEM") && !currentDynamicKey.equals("SA3")) {
                    JSONObject currentDynamicValue = json.getJSONObject(currentDynamicKey);

                    JSONArray records = currentDynamicValue.getJSONArray("RECORD");
                    JSONArray dbo = (JSONArray) currentDynamicValue.getJSONObject("SETTINGS").get("DBO");
                    String createTable = dbo.getString(0).toString();

                    createTable = createTable.replace("\u0000", "");
                    createTable = createTable.replace("\\u0000", "");

                    sett.dropTable(currentDynamicKey);
                    sett.createTable(createTable);

                    for(int i=0; i < records.length(); i++) {
                        JSONObject jsonOBject = records.getJSONObject(i);
                        Map<String, String> dataArray = new HashMap<String, String>(jsonOBject.length());

                        for (int x=0; x < jsonOBject.length(); x++) {
                            String key = jsonOBject.names().getString(x).toString();
                            String value = jsonOBject.getString(key).toString();

                            dataArray.put(key, value);
                        }

                        sett.insertDataHash(currentDynamicKey, dataArray);
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("---[Erro no sync]---", e.getMessage());
        }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.d("---[Volley]---", "Error: " + error.getMessage());
        hidepDialog();
    }
}){
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Accept", "application/json");
        headers.put("Content-type", "application/json");
        headers.put("Token", appToken);
        headers.put("DateTime", formattedDate);
        headers.put("Mac", deviceMac);
        headers.put("user", user);
        return headers;
    }
};
queue.add(jsonObjReq);

Json

{
  "RET": true,
  "DATA": {
    "SYSTEM": {
      "SYNC": true
    },
    "TABELA1": {
      "RECORD": [
        {
          "CAMPO1": "AAAAA",
          "CAMPO2": "00000",
          "CAMPO3": "AAA00"
        },
        {
          "CAMPO1": "BBBBBB",
          "CAMPO2": "111111",
          "CAMPO3": "BBB111"
        },
      ]
    },
    "TABELA2": {
      "RECORD": [
        {
          "CAMPO1": "CCCCCC",
          "CAMPO2": "000000",
          "CAMPO3": "CCCC00"
        },
        {
          "CAMPO1": "DDDDDD",
          "CAMPO2": "222222",
          "CAMPO3": "DDD222"
        },
      ]
    }    
  }
}

Editado

Fonte do parsejson e insert no banco

final DBController sett = new DBController(getBaseContext());
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

final String formattedDate = df.format(c.getTime());

RequestQueue queue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        try {
            JSONObject json = response.getJSONObject("DATA");
            Iterator keys = json.keys();

            while(keys.hasNext()) {
                String currentDynamicKey = (String)keys.next();
                if (!currentDynamicKey.equals("SYSTEM") && !currentDynamicKey.equals("SA3")) {
                    JSONObject currentDynamicValue = json.getJSONObject(currentDynamicKey);

                    JSONArray records = currentDynamicValue.getJSONArray("RECORD");
                    JSONArray dbo = (JSONArray) currentDynamicValue.getJSONObject("SETTINGS").get("DBO");
                    String createTable = dbo.getString(0).toString();

                    createTable = createTable.replace("\u0000", "");
                    createTable = createTable.replace("\\u0000", "");

                    sett.dropTable(currentDynamicKey);
                    sett.createTable(createTable);

                    for(int i=0; i < records.length(); i++) {
                        JSONObject jsonOBject = records.getJSONObject(i);
                        Map<String, String> dataArray = new HashMap<String, String>(jsonOBject.length());

                        for (int x=0; x < jsonOBject.length(); x++) {
                            String key = jsonOBject.names().getString(x).toString();
                            String value = jsonOBject.getString(key).toString();

                            dataArray.put(key, value);
                        }

                        sett.insertDataHash(currentDynamicKey, dataArray);
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("---[Erro no sync]---", e.getMessage());
        }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.d("---[Volley]---", "Error: " + error.getMessage());
        hidepDialog();
    }
}){
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Accept", "application/json");
        headers.put("Content-type", "application/json");
        headers.put("Token", appToken);
        headers.put("DateTime", formattedDate);
        headers.put("Mac", deviceMac);
        headers.put("user", user);
        return headers;
    }
};
queue.add(jsonObjReq);

Json

{
  "RET": true,
  "DATA": {
    "SYSTEM": {
      "SYNC": true
    },
    "TABELA1": {
      "RECORD": [
        {
          "CAMPO1": "AAAAA",
          "CAMPO2": "00000",
          "CAMPO3": "AAA00"
        },
        {
          "CAMPO1": "BBBBBB",
          "CAMPO2": "111111",
          "CAMPO3": "BBB111"
        },
      ]
    },
    "TABELA2": {
      "RECORD": [
        {
          "CAMPO1": "CCCCCC",
          "CAMPO2": "000000",
          "CAMPO3": "CCCC00"
        },
        {
          "CAMPO1": "DDDDDD",
          "CAMPO2": "222222",
          "CAMPO3": "DDD222"
        },
      ]
    }    
  }
}
Fonte Link

Sincronização de dados no android

Então, estou em um projeto que preciso guardar uma grande massa de dados vindo de uma api json no SQLite do android, algo em torno de 20 mil registros, para poder trabalhar offline sem problemas.

Só que a sincronização dessa massa de dados está demorando muito tempo no aplicativo, cerca de uns 10 a 15 min. E fica totalmente inviavel de o usuário ficar esperando esse tempo todo para download dos dados.

Cheguei a dar uma olhada também no couchbase lite.

Minha pergunta é, O sqlite trabalha bem com uma grande quantidade de registros? Se sim, teria uma forma diminuir o tempo de sincronia dos dados para poder usar o sqlite? Como salvar o json inteiro dentro do couchbase.