duanlvxi8652 2017-12-18 16:07
浏览 48

如何通过volley将Cursor数据从SQLite数据库插入MySQL数据库,其中REST api是codeigniter

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!!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来
    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法