无法使用JSON插入行

时间:2014-07-19 20:55:33

标签: php android json

有人可以帮帮我!我不能使用JSON创建一行。

/**
     * Background Async Task to Create new product
     * */
    class CreateNewProduct extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(NewServiceActivity.this);
            pDialog.setMessage("Creating Product..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        /**
         * Creating product
         * */
        protected String doInBackground(String... args) {
            // getting updated data from EditTexts
            String ticketno = txtTicketNo.getText().toString();
            String status = txtStatus.getText().toString();
            String bracode = txtBrCode.getText().toString();
            String compid = txtcomId.getText().toString();
            String compname = txtcomName.getText().toString();
            String comcity = txtcomAddress.getText().toString();
            String repby= txtRepBy.getText().toString();
            String repon = txtRepOn.getText().toString();
            String sect = txtSection.getText().toString();
            String dept = txtDept.getText().toString();
            String problem = txtProblem.getText().toString();
            String creby = txtCreatedBy.getText().toString();
            String dtstart = dtStart.getText().toString();
            String dtstop = dtStop.getText().toString();

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("report_no", ticketno));
            params.add(new BasicNameValuePair("status", status));
            params.add(new BasicNameValuePair("branch_code", bracode));
            params.add(new BasicNameValuePair("company_id", compid));
            params.add(new BasicNameValuePair("company_name", compname));
            params.add(new BasicNameValuePair("city", comcity));
            params.add(new BasicNameValuePair("reported_by", repby));
            params.add(new BasicNameValuePair("reported_on", repon));
            params.add(new BasicNameValuePair("section", sect));
            params.add(new BasicNameValuePair("department", dept));
            params.add(new BasicNameValuePair("problem", problem));
            params.add(new BasicNameValuePair("created_by", creby));
            params.add(new BasicNameValuePair("dt_start", dtstart));
            params.add(new BasicNameValuePair("dt_finish", dtstop));

            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                    "POST", params);

            // check log cat fro response
            Log.d("Create Response", json.toString());

这是我在数据库中插入行的java类。

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['report_no']) && isset($_POST['status']) &&         isset($_POST['branch_code']) && isset($_POST['company_id']) && isset($_POST['company_name']) && isset($_POST['city']) && isset($_POST['reported_by']) && isset($_POST['reported_on']) && isset($_POST['section']) && isset($_POST['department']) && isset($_POST['problem']) && isset($_POST['created_by']) && isset($_POST['dt_start']) && isset($_POST['dt_finish'])) {

$report_no = $_POST['report_no'];
$status = $_POST['status'];
$branch_code = $_POST['branch_code'];
$company_id = $_POST['company_id'];
$company_name = $_POST['company_name'];
$city = $_POST['city'];
$reported_by = $_POST['reported_by'];
$reported_on = $_POST['reported_on'];
$section = $_POST['section'];
$department = $_POST['department'];
$problem = $_POST['problem'];
$created_by = $_POST['created_by'];
$dt_start = $_POST['dt_start'];
$dt_finish = $_POST['dt_finish'];


// include db connect class
$filepath = realpath (dirname(__FILE__));
require_once($filepath."/db_connect.php");

// connecting to db
$db = new DB_CONNECT();

// mysql inserting a new row
$result = mysql_query("INSERT INTO report(report_no, status, branch_code, company_id, company_name, city, reported_by, reported_on, section, department, problem, created_by, datetime_start, datetime_finish) VALUES($report_no, '$status', '$branch_code', '$company_id', '$company_name', '$city', '$reported_by', '$reported_on', $section, $department, '$problem', '$created_by', '$dt_start', '$dt_finish')");

// check if row inserted or not
if ($result) {
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "Report successfully created.";

    // echoing JSON response
    echo json_encode($response);
} else {
    // failed to insert row
    $response["success"] = 0;
    $response["message"] = "Oops! An error occurred.";

    // echoing JSON response
    echo json_encode($response);
}
} else {
    // required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";

// echoing JSON response
echo json_encode($response);
}
?>   

这是我的剧本。当我执行应用程序时,logcat说缺少必填字段。请帮帮我。

编辑:我现在看到问题,但我还没有解决方案。发送到类中edittexts的JSON在我的设备中运行时会显示数据,但是当我打算在edittexts中使用数据时,它表示它是null。

0 个答案:

没有答案