lundi 28 mars 2016

How can i Insert JSON data into the SQLite database in android

I want to insert the data from JSON array into the SQLite database. I can get the the data type JSON but when insert into sqlite it have two records with duplicate data, actually it's have more than two data.

public void getDetail(final String user_id) {

    String tag_string_req = "Get Data";

    StringRequest strReq = new StringRequest(Request.Method.POST,
            AppConfig.URL_getDeliveryDetail, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.e(TAG, "Get Data Respone : " + response.toString()+"\n");
            try {
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");

                for (int i = 0; i < jObj.length() ; i++) {
                    if (!error) {

                        JSONObject delivery = jObj.getJSONObject("delivery");

                        String id_plan_group = delivery.getString("id_plan_group");
                        String customer = delivery.getString("DELIVERYCUSTOMER");
                        String address = delivery.getString("DELIVERYADDRESS1");

                        db.addDeliveryList(id_plan_group, customer, address);
                    } else {
                        // Error in login. Get the error message
                        String errorMsg = jObj.getString("error_msg");
                        Toast.makeText(getApplicationContext(),
                                errorMsg, Toast.LENGTH_LONG).show();
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Can't get the data", Toast.LENGTH_LONG).show();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Get Data Error : " + error.getMessage());
        }
    }){
        @Override
        protected Map<String, String> getParams() {
            // Posting parameters to login url
            Map<String, String> params = new HashMap<String, String>();
            params.put("user_id", user_id);
            return params;
        }
    };
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}

And this the logcat yo show success insert data enter image description here

This one is php to get data from sql server enter image description here

Aucun commentaire:

Enregistrer un commentaire