Ver Mensaje Individual
  #2 (permalink)  
Antiguo 15/04/2015, 10:10
Avatar de Maverick2786
Maverick2786
 
Fecha de Ingreso: diciembre-2012
Mensajes: 107
Antigüedad: 11 años, 4 meses
Puntos: 1
Respuesta: Problema con la sincronizacion de Sqlite con Mysql

Aqui la solucion

Dentro de mi clase que hace la consulta a sqlite
Código Java:
Ver original
  1. public JSONObject getJSONfromSQLite(){
  2.         JSONObject json_row = new JSONObject();
  3.         SQLiteDatabase database = this.getWritableDatabase();
  4.         int cont=0;
  5.        
  6.         String selectQuery = "SELECT  id_sqlite, user, company, form, checkBox, date FROM chequeo where status = '2' ";
  7.        
  8.        
  9.         Cursor cursor = database.rawQuery(selectQuery, null);
  10.        
  11.         if (cursor.moveToFirst()) {
  12.             do {
  13.                 JSONObject json_val = new JSONObject();
  14.                 try{
  15.                     cont++;
  16.                     json_val.put("id_sqlite", cursor.getString(0));
  17.                     json_val.put("user", cursor.getString(1));
  18.                     json_val.put("company", cursor.getString(2));
  19.                     json_val.put("form", cursor.getString(3));
  20.                     json_val.put("checkBox", cursor.getString(4));
  21.                     json_val.put("date", cursor.getString(5));
  22.                     json_row.put("cont"+cont,json_val);
  23.                    
  24.                 }
  25.                 catch(Exception e)
  26.                 {
  27.                     Log.e("getJSONfromSQLite catch", e.getMessage());
  28.                 }
  29.             } while (cursor.moveToNext());
  30.         }
  31.         database.close();
  32.        
  33.         JSONObject json = new JSONObject();
  34.        
  35.         try{
  36.             json.put("json",json_row);
  37.             json.put("cont",cont);
  38.         }
  39.         catch(Exception e){
  40.             Log.e("json catch", e.getMessage());
  41.         }
  42.        
  43.         return  json;
  44.     }

Dentro de la clase que manda los datos al servidor
Código Java:
Ver original
  1. public void sync(View v) {
  2.         // Tag used to cancel the request
  3.         String tag_string_req = "req_login";
  4.        
  5.  
  6.         pDialog.setMessage("Logging in...");
  7.         pDialog.show();
  8.  
  9.         StringRequest strReq = new StringRequest(Method.POST,
  10.                 AppConfig.URL_SYNC, new Response.Listener<String>() {
  11.                     public void onResponse(String response) {
  12.                        
  13.                         try {
  14.                             Log.d("Login Response",
  15.                                     "Login Response: " + response.toString());
  16.                             pDialog.cancel();
  17.                            
  18.                             String user = config.getUser();
  19.                            
  20.                             JSONObject jObj = new JSONObject(response);
  21.                             boolean error = jObj.getBoolean("error");
  22.  
  23.                             // Check for error node in json
  24.                             if (!error) {
  25.                                 // user successfully logged in
  26.                                 // Create login session
  27.                                
  28.                                 int contLogin = objSqlt.getUser(user);
  29.                                 if (contLogin == 0) {
  30.                                     JSONObject obj = jObj.getJSONObject("user");
  31.                                     Integer id_mysql = obj.getInt("id_mysql");
  32.                                     String user1 = obj.getString("user");
  33.                                     String pass_encrypted = obj.getString("pass_encrypted");
  34.                                     String pass1 = obj.getString("pass");
  35.  
  36.                                     // Inserting row in users table
  37.                                     objSqlt.addUser(id_mysql, user1, pass_encrypted, pass1);
  38.                                 }
  39.  
  40.                             } else {
  41.                                 session.setControl_login(false);
  42.                                 String errorMsg = jObj.getString("error_msg");
  43.                                 Toast.makeText(getApplicationContext(),
  44.                                         errorMsg, Toast.LENGTH_LONG).show();
  45.                             }
  46.                         } catch (JSONException e) {
  47.                             e.printStackTrace();
  48.                         }
  49.                     }
  50.                 }, new Response.ErrorListener() {
  51.                     public void onErrorResponse(VolleyError error) {
  52.                         Log.e("Sysn Error: ", "Error en la conexion");
  53.                         Toast.makeText(getApplicationContext(),
  54.                                 "Error en la conexion", Toast.LENGTH_LONG).show();
  55.                         pDialog.cancel();
  56.                     }
  57.                 }) {
  58.             @Override
  59.             protected Map<String, String> getParams() {
  60.                 Map<String, String> params = new HashMap<String, String>();
  61.                 JSONObject json = objSqlt.getJSONfromSQLite();
  62.                
  63.                 try{
  64.                     params.put("json", json.getString("json"));
  65.                     params.put("cont", json.getString("cont"));
  66.                 } catch(Exception e){
  67.                     Log.e("getParams", e.getMessage());
  68.                 }
  69.                 return params;
  70.             }
  71.         };
  72.  
  73.         // Adding request to request queue
  74.         AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
  75.     }