通过android

时间:2019-05-23 10:00:14

标签: android mysql sqlite rest android-volley

在我的应用程序中,我有一个表单,该表单的所有字段都保存在SQLite数据库中,包括sync_state列。如果sync_state为0,则该项目不保存在联机数据库中,但是,如果sync_state为1,则将其保存在联机数据库中。我的仪表板上有一个按钮,该按钮列出了sync_state为0的所有SQLite行的列表,并将它们保存在在线MySQL数据库中。但是此按钮无法正常工作。有些行未保存,有些重复。我进行了很多搜索,但找不到任何解决方案。

用于创建要在线保存的列表的方法:

public List<FullModel> getAllDataforSync(){
    SQLiteDatabase database =this.getWritableDatabase();
    String query = "SELECT * FROM " + DatabaseHelper.TABLE_NAME + " where " + TABLE_COL35 + " = 0" ;
    Cursor cursor = database.rawQuery(query, null);

    List<FullModel> list = new ArrayList<>();
    while (cursor.moveToNext()){
        FullModel listModel = new FullModel(cursor.getString(1),cursor.getString(2),
                cursor.getString(3),cursor.getString(4),cursor.getString(5),
                cursor.getString(6),cursor.getBlob(7),cursor.getString(8),
                cursor.getString(9),cursor.getLong(10),cursor.getLong(11),
                cursor.getLong(12),cursor.getLong(13),cursor.getLong(14),cursor.getLong(15),
                cursor.getLong(16),cursor.getLong(17),cursor.getLong(18),cursor.getLong(19),
                cursor.getLong(20),cursor.getLong(21),cursor.getLong(22),cursor.getLong(23),
                cursor.getLong(24),cursor.getLong(25),cursor.getLong(26),cursor.getLong(27),
                cursor.getLong(28),cursor.getLong(29),cursor.getLong(30),cursor.getLong(31),
                cursor.getInt(32));
        listModel.setId(cursor.getInt(cursor.getColumnIndex(DatabaseHelper.TABLE_COL1)));

        list.add(listModel);
    }
    return list;
}

在这里调用该方法:

builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (checkNetworkConnection()){

                        allDataList = databaseHelper.getAllDataforSync();
                        Toast.makeText(DashBoardActivity.this, ""+allDataList.size(), Toast.LENGTH_SHORT).show();;
                        if (allDataList.size()==0){
                            AlertDialog.Builder builder2 = new AlertDialog.Builder(DashBoardActivity.this);
                            LayoutInflater inflater2 = getLayoutInflater();
                            View view2 = inflater2.inflate(R.layout.nodatatoshow_dialog_box, null);
                            builder2.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.cancel();
                                }
                            });
                            builder2.setView(view2);
                            AlertDialog alertDialog = builder2.create();
                            alertDialog.show();
                        }else{
                        for (FullModel fullModel : allDataList) {

                            sname = fullModel.getSale_name();
                            sphone = fullModel.getSale_mobile();
                            scnic = fullModel.getSale_cnic();
                            bname = fullModel.getBuyer_name();
                            bmobile = fullModel.getBuyer_mobile();
                            bcnic = fullModel.getBuyer_cnic();
                            date = fullModel.getDate();
                            location = fullModel.getLocation();
                            picture = fullModel.getPicture();
                            goatQ = fullModel.getGoatQ();
                            goatR = fullModel.getGoatP();
                            goatT = fullModel.getGoatT();
                            sheepQ = fullModel.getSheepQ();
                            sheepR = fullModel.getSheepP();
                            sheepT = fullModel.getSheepT();
                            cowQ = fullModel.getCowQ();
                            cowR = fullModel.getCowP();
                            cowT = fullModel.getCowT();
                            camelQ = fullModel.getCamelQ();
                            camelR = fullModel.getCamelP();
                            camelT = fullModel.getCamelT();
                            horseQ = fullModel.getHorseQ();
                            horseR = fullModel.getHorseP();
                            horseT = fullModel.getHorseT();
                            donkeyQ = fullModel.getDonkeyQ();
                            donkeyR = fullModel.getDonkeyP();
                            donkeyT = fullModel.getDonkeyT();
                            buffQ = fullModel.getBufQ();
                            buffR = fullModel.getBufP();
                            buffT = fullModel.getBufT();
                            totalAm = fullModel.getTotalAm();

                            StringRequest stringRequest = new StringRequest(Request.Method.POST, SERVER_URL, new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                    try {
                                        JSONObject jsonObject = new JSONObject(response);
                                        String Response = jsonObject.getString("response");
                                        if (Response.equals("OK")) {
                                            Toast.makeText(DashBoardActivity.this, "Data saved in Application server", Toast.LENGTH_SHORT).show();
                                        } else {
                                            Toast.makeText(DashBoardActivity.this, "Data not saved" + Response, Toast.LENGTH_SHORT).show();
                                        }
                                    } catch (JSONException e) {
                                        Toast.makeText(DashBoardActivity.this, "" + e.getMessage(), Toast.LENGTH_LONG).show();
                                    }
                                }
                            }, new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {

                                }
                            }) {
                                @Override
                                protected Map<String, String> getParams() throws AuthFailureError {
                                    Map<String, String> params = new HashMap<>();
                                    params.put("name", sname);
                                    params.put("sale_mobile", sphone);
                                    params.put("sale_cnic", scnic);
                                    params.put("buyer_name", bname);
                                    params.put("buyer_mobile", bmobile);
                                    params.put("buyer_cnic", bcnic);
                                    params.put("picture", String.valueOf(picture));
                                    params.put("date", date);
                                    params.put("location", location);
                                    params.put("a_type", "");
                                    params.put("price", "");
                                    params.put("goat_q", "" + goatQ);
                                    params.put("goat_r", "" + goatR);
                                    params.put("goat_t", "" + goatT);
                                    params.put("sheep_q", "" + sheepQ);
                                    params.put("sheep_r", "" + sheepR);
                                    params.put("sheep_t", "" + sheepT);
                                    params.put("cow_q", "" + cowQ);
                                    params.put("cow_r", "" + cowR);
                                    params.put("cow_t", "" + cowT);
                                    params.put("camel_q", "" + camelQ);
                                    params.put("camel_r", "" + camelR);
                                    params.put("camel_t", "" + camelT);
                                    params.put("horse_q", "" + horseQ);
                                    params.put("horse_r", "" + horseR);
                                    params.put("horse_t", "" + horseT);
                                    params.put("donkey_q", "" + donkeyQ);
                                    params.put("donkey_r", "" + donkeyR);
                                    params.put("donkey_t", "" + donkeyT);
                                    params.put("buff_q", "" + buffQ);
                                    params.put("buff_r", "" + buffR);
                                    params.put("buff_t", "" + buffT);
                                    params.put("total_am", "" + totalAm);

                                    return params;
                                }
                            };
                            MySingleton.getInstance(DashBoardActivity.this).addToRequestQueue(stringRequest);
                            for (FullModel fullModel1 : allDataList) {
                                databaseHelper.updateSyncState(fullModel1.getId(), SYNC_STATE_OK);
                                }
                            }
                        }

                    }else{
                        AlertDialog.Builder builder1 = new AlertDialog.Builder(DashBoardActivity.this);
                        LayoutInflater inflater1 = getLayoutInflater();
                        View view1 = inflater1.inflate(R.layout.no_internet_dialog, null);
                        builder1.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                        builder1.setView(view1);
                        AlertDialog alertDialog = builder1.create();
                        alertDialog.show();
                    }
                }
            });
            builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            builder.setView(view);
            builder.setCancelable(false);
            AlertDialog alertDialog = builder.create();
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.setCancelable(false);
            alertDialog.show();

请帮助!

1 个答案:

答案 0 :(得分:0)

您将列表作为单独的列表对象发送到服务器。 尝试仅按一个请求发送完整列表。 您可以参考以下链接:[https://stackoverflow.com/a/48782187/11469878][1]