使用volley将Json String发送到php并对其进行解码

时间:2016-06-06 05:17:22

标签: php json android-volley

我知道如何使用volley库发送数据并从php中获取数据。问题是,我想发送一个Json字符串并从php端解码这些数据。

这是我用来使用param发送数据的方法。最后一项是json String

private void checkOrderNo() {
        pDialog.setMessage("Sending...");
        showDialog();

        DateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
        final String nowDate = df.format(new Date());

        //final day of the month
        Date today = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(today);
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DATE, -1);
        Date lastDayOfMonth = calendar.getTime();
        DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        final String lastDate = sdf.format(lastDayOfMonth);

        Log.d("Last day ", sdf.format(lastDayOfMonth) + " // Today" + nowDate);

        // Tag used to cancel the insert
        String tag_string_req = "req_insert";

        final StringRequest strReq = new StringRequest(Request.Method.POST,
                AppConfig.URL_ITEM_DETAILS_SEND, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                hideDialog();
                try {
                    JSONObject jObj = new JSONObject(response);
                    if (jObj.names().get(0).equals("found")) {

                        newOrderId = jObj.getString("found").toString();
                        orderIdForItemTable = newOrderId;

                        Log.d(TAG, "newOrderId: " + newOrderId);
                        Log.d(TAG, "New repID 2 inserted into sqlite: " + newOrderId + " " + nowDate);
                        sqLiteHandler.addItemDetails(newOrderId, repID, dealerID, nowDate, lastDate, selectedDisChannel);

                        finish();
                        Bundle basket = new Bundle();
                        basket.putString("dealerName", dealerName);
                        basket.putString("orderNo", newOrderId);
                        basket.putString("jsonString", json_string);
                        Intent intent = new Intent(SelectItem.this, ItemCart.class);
                        intent.putExtras(basket);
                        startActivity(intent);
                        finish();

                    } else {
                        Toast.makeText(getApplicationContext(), "Invalied Request", Toast.LENGTH_SHORT).show();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Inserting Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                // Posting params to register url
                Map<String, String> params = new HashMap<String, String>();
                params.put("order_no", orderId);
                params.put("repID", repID);
                params.put("dealerID", dealerID);
                params.put("nowDate", nowDate);
                params.put("lastDate", lastDate);
                params.put("disChannel", selectedDisChannel);
                params.put("jsonString", json_string);

                return params;
            }
        };

        strReq.setRetryPolicy(new DefaultRetryPolicy(15000, 1,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }

这是我的排球博士

<?php

require_once 'include/Config_test.php';

    $con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die("connection failed");
    mysql_select_db(DB_DATABASE,$con) or die("db selection failed");

    $order_no = $repID = $dealerID = $nowDate = $jsonString = $lastDate = $disChannel = "";

    if(isset($_POST['order_no'])){
        $order_no = $_POST['order_no'];
        $repID = $_POST['repID'];
        $dealerID = $_POST['dealerID'];
        $nowDate = $_POST['nowDate'];
        $lastDate = $_POST['lastDate'];
        $disChannel = $_POST['disChannel'];
        $jsonString= $_POST['jsonString'];
    }

    $result = mysql_query("SELECT MAX(order_no) FROM tbl_items_header_t");
    $row = mysql_fetch_row($result);
    if($row[0] < 70000000){
        $highest_id = 70000000;
    } else{
        $highest_id = $row[0] + '1';
    }
    //$highest_id = $row[0] + '1';

    $query = mysql_query("INSERT INTO tbl_items_header_t(order_no,rep_no,dealer_no,order_date,last_date,dis_channel,status) 
    VALUES('$highest_id','$repID','$dealerID','$nowDate','$lastDate','$disChannel','')");

    $json['found']= $highest_id;    
    echo json_encode($json);



?>

我知道使用 DefaultHttpClient 发送json String但不推荐使用它。我也必须使用两个PHP。我想用排球来做。

这是我用于使用DefaultHttpClient获取json String的内容。有效。但是我想在排球中使用它。

<?php

require_once 'include/Config_test.php';

    $con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die("connection failed");
    mysql_select_db(DB_DATABASE,$con) or die("db selection failed");

    $postdata = file_get_contents('php://input'); 
    $data = json_decode($postdata, true);

   if (is_array($data['sending_items'])) {
        foreach ($data['sending_items'] as $record) {
            $order_no = $record['order_no'];
            $items = $record['items'];
            $items_no = $record['items_no'];
            $plant = $record['plant'];
            $quantity = $record['quantity'];

            mysql_query("INSERT INTO tbl_item_list(order_no, items, items_no, plant, quantity) VALUES('$order_no', '$items', '$items_no', '$plant', '$quantity')");

      }
   }

echo json_encode($data);
mysql_close($con); 

?>

0 个答案:

没有答案