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"
},
]
}
}
}