您可以使用
volleyGoogle提供的库,也可以使用其他方法
libraries,这取决于您要如何发送数据,最好的方法是
JSON使生活更轻松,从想要与后端同步并发送数据的sqlite获取数据在
JsonObjectRequest使用凌空抽空的情况下,例如您的请求可能如下所示
jsonObjectRequest postForm = new JsonObjectRequest(Request.Method.POST, URL, YourJsonData, new Response.Listener<JSONObject>() { @Override public void onResponse(JSonObject response) { // here you can get your response. },new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // here you can tell there is something went wrong. });您可以添加一个新值,该值指示本地数据库中的值是否已同步。例如,假设您有一个名为student的表,并且此表包含三列,分别是ID,NAME和当您的响应返回成功时在上面的代码中同步过的行,该行已同步的列用true
| false表示该行是否与您的后端同步或者没有。为了从数据库中获取数据,您应该执行以下操作。
public String getStudents() { List<Student> students = new ArrayList<Student>(); String query = "SELECt * FROM " + STUDENT+ "where synced=0"; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(query, null); if (cursor.moveToFirst()) { do { Student st = new Student(); st.setId(cursor.getString(cursor.getColumnIndex(ID))); st.setName(cursor.getString(cursor.getColumnIndex(NAME))); st.setSynced(cursor.getInt(cursor.getColumnIndex(SYNCED))); students.add(st); } while (cursor.moveTonext()); } db.close(); return new Gson().toJson(students); }


