如何编写JAVA CLASS和PHP API来发布和读取现有的MySQL数据库

时间:2012-06-14 13:42:43

标签: java php android mysql

我正在开发一个应用程序,允许员工请求该帖子的时间并从现有的MySQL数据库中读取。但是我在解决整个代码段(PHP,JAVA)中要解决的问题时遇到了一些麻烦。任何专家都可以告诉我这是如何解决的吗?

这是我目前收到的错误消息

D/Create Response(284): {"message":"Required field(s) is missing","success":0}

请求PHP API

<?php
// array for JSON response
$response = array();

// check for the fields
if (isset($_POST['title']) && isset($_POST['request_date']) && isset($_POST['reqEndDate']) && isset($_POST['reason']) && isset($_POST['requestor']) && isset($_POST['status']) && isset($_POST['submitDate']) && isset($_POST['explanation']) && isset($_POST['hours']) && isset($_POST['id'])) {

    $title = $_POST["request_title"];
    $date = $_POST["request_date"];
    $eDate = $_POST["reqEndDate"];
    $reason = $_POST["reason"];
    $requestor = $_POST["requestor"];
    $status = $_POST["status"]; 
    $dateSubmitted = $_POST["submitDate"];
    $explanation = $_POST["explanation"];
    $numhours = $_POST["hours"];
    $empid = $_POST['id'];


    // mysql inserting a new row
    $result = mysql_query("INSERT INTO requests(request_title, request_date, reqEndDate, reason, requestor, status, submitDate, explanation, hours, empid) 
                            VALUES('$title', '$date', '$eDate', '$reason', '$requestor', '$status', '$dateSubmitted', '$explanation', '$numhours', '$empid')");


} else {

    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    echo json_encode($response);
}
?>

申请JAVA CLASS

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("request_title", title));
            params.add(new BasicNameValuePair("request_date", date));
            params.add(new BasicNameValuePair("reqEndDate", eDate));
            params.add(new BasicNameValuePair("hours", hours));
            params.add(new BasicNameValuePair("reason", reason));
            params.add(new BasicNameValuePair("explanation", explanation));

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

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

            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // successfully created request
                    Intent i = new Intent(getApplicationContext(), AllRequestsActivity.class);
                    startActivity(i);

                    // closing this screen
                    finish();
                } else {
                    // failed to create request
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }



    }
}

3 个答案:

答案 0 :(得分:2)

我认为错误信息非常具有描述性。

PHP代码中的第一个if

在请求中包含所有必填字段。

答案 1 :(得分:0)

PHP脚本正在寻找在$ _POST中设置的10个变量,但是你的Java类只在“params”中发送了6个。

答案 2 :(得分:0)

POST请求中不存在下面“if”中提到的字段之一

if (isset($_POST['title']) && isset($_POST['request_date']) && isset($_POST['reqEndDate']) && isset($_POST['reason']) && isset($_POST['requestor']) && isset($_POST['status']) && isset($_POST['submitDate']) && isset($_POST['explanation']) && isset($_POST['hours']) && isset($_POST['id'])) {