如何使用volly将Json字符串发送给php ?

时间:2022-10-22 19:46:23

I want to send a json string to a php file. I know how to send string values. But i don't know how to send json string and decode it using php.

我想将json字符串发送到php文件。我知道如何发送字符串值。但是我不知道如何发送json字符串并使用php解码它。

This is the json look like.

这是json。

{"sending_items":[{"order_no":"70000106","items":"example item","items_no":"2000450","plant":"2200","quantity":"2"}]}

This is the method.

这是方法。

private void checkOrderNo() {

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

            @Override
            public void onResponse(String response) {


                    }

                } 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();*/
                VolleyErrorHandle.handleVolleyErrorTwo(error, pDialog, findViewById(R.id.user_selectItem_root));
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                // Posting params to register url
                Map<String, String> params = new HashMap<String, String>();
                params.put("json_string", 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);
    }

This is My PhP code..

这是我的PhP代码。

<?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); 

    ?>

1 个解决方案

#1


1  

Base on your java code, you put your json_string in a parameter called json_string, and use POST method to send to server.

基于java代码,将json_string放入名为json_string的参数中,并使用POST方法发送到服务器。

So you can get data at php like this:

你可以从php中获取数据

<?php
    $jsonString = $_POST['json_string']; 
    $data = json_decode($jsonString);

    //now you can get your JSON data, and do whatever you want :)
?>

#1


1  

Base on your java code, you put your json_string in a parameter called json_string, and use POST method to send to server.

基于java代码,将json_string放入名为json_string的参数中,并使用POST方法发送到服务器。

So you can get data at php like this:

你可以从php中获取数据

<?php
    $jsonString = $_POST['json_string']; 
    $data = json_decode($jsonString);

    //now you can get your JSON data, and do whatever you want :)
?>