I am trying to get data from android SQLite database in the form of Cursor object and by looping i have create a json object than json array and sending it to server using volley.
But i am unable to retrieve this data on my REST api which on codeigniter.
Please let me know where i am tacking wrong step?
Here is my Android code -
//on click listener place order button
btn_place_order = (Button) findViewById(R.id.btn_place_order);
btn_place_order.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (MainActivity.CheckNetwork.isInternetAvailable(Cart.this)) //returns true if internet available
{
new android.app.AlertDialog.Builder(Cart.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Order Conformation")
.setMessage("Do you want to place order?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
JSONArray resultSet = new JSONArray();
JSONObject rowObject = new JSONObject();
Cursor res = myDb.getAllData();
if(res.getCount() != 0) {
while (res.moveToNext()) {
String id = res.getString(0);
String quantity = res.getString(1);
String price = res.getString(2);
try {
rowObject.put(TAG_ID, id);
rowObject.put(TAG_QUANTITY, quantity);
rowObject.put(TAG_PRICE, price);
} catch (JSONException e) {
e.printStackTrace();
}
resultSet.put(rowObject);
}
//closing the cursor
res.close();
}
//JSONArray jsArray = new JSONArray(personList);
saveOrder(name, mobile, email, address_send, resultSet);
}
})
.setNegativeButton("Not Now", null)
.show();
} else {
Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_LONG).show();
}
}
});
Here is my Codeigniter Controller code -
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Cart extends CI_Controller {
public function __construct()
{
parent::__construct();
//load model
$this->load->model('sf/cart_model');
$this->load->library('cart');
$this->load->library('email');
$this->load->library('parser');
}
public function save_order()
{
// This will store all values which inserted from user.
$customer = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'address' => $this->input->post('address'),
'phone' => $this->input->post('phone')
);
// And store user imformation in database.
$cust_id = $this->cart_model->insert_customer($customer);
$order = array(
'date' => date('Y-m-d'),
'customerid' => $cust_id
);
$ord_id = $this->cart_model->insert_order($order);
//Decode JSON into an Array
$json = $this->input->post('data');
$data = json_decode($json, true);
if ($data != null):
foreach ($data ->result_array() as $item):
$order_detail = array(
'orderid' => $ord_id,
'productid' => $item['TAG_ID'],
'quantity' => $item['TAG_QUANTITY'],
'price' => $item['TAG_PRICE']
);
// Insert product imformation with order detail, store in cart also store in database.
$order_id = $this->cart_model->insert_order_detail($order_detail);
endforeach;
endif;
if ($order_id != null) {
$response["error"] = FALSE;
$response["msg"] = $json;
echo json_encode($response);
}
else {
$response["error"] = TRUE;
$response["msg"] = $json;
echo json_encode($response);
}
}
}
Please help me!! Thanks!!