我正在创建android应用程序,以便从android设备与我的localhost服务器进行通信。我想将matrix_id
和course_code
传递给PHP查询。
Logcat错误:
04-02 13:30:45.060: E/JSON Parser(17463): Error parsing data [No value for exam_place]
我为此搜索了很多,但我不满意我的查询。
请帮助我。
这是PHP脚本。
// array for JSON response
$response = array();
include('include/db_connect.php');
mysql_select_db($database_localhost,$localhost);
// check for post data
if (isset($_GET["matrix_id"]) && $_GET["course_code"]) {
echo $matrix_id = $_GET['matrix_id'];
echo $course_code = $_GET['course_code'];
// get a subject from subject table
$result = mysql_query("SELECT * FROM student WHERE matrix_id = '$matrix_id' AND course_code = '$course_code'");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$student = array();
$student["matrix_id"] = $result["matrix_id"];
$student["course_code"] = $result["course_code"];
$student["name"] = $result["name"];
$student["email"] = $result["email"];
$student["exam_place"] = $result["exam_place"];
$student["exam_date"] = $result["exam_date"];
$student["time"] = $result["time"];
$student["desk_no"] = $result["desk_no"];
// success
$response["success"] = 1;
// user node
$response["student"] = array();
array_push($response["student"], $student);
// echoing JSON response
echo json_encode($response);
} else {
// no subject found
$response["success"] = 0;
$response["message"] = "No subject found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no subject found
$response["success"] = 0;
$response["message"] = "No subject found";
// echo no users JSON
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);
}
?>
这是我的java脚本。
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// post matrix_id and course_code GET parameters
params.add(new BasicNameValuePair(TAG_MATRIX_ID, matrix_id));
params.add(new BasicNameValuePair(TAG_COURSE_CODE, course_code));
// getting JSON string from URL
JSONObject json = jsonParser.makeHttpRequest(url_subject_details, "GET",params);
// Check your log cat for JSON reponse
Log.d("Single Subject JSON: ", json.toString());
int success;
try {
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
if(json != null){
// successfully received subject details
JSONArray subjectObj = json.getJSONArray(TAG_STUDENT); // JSON Array
// get first subject object from JSON Array
JSONObject subject = subjectObj.getJSONObject(0);
exam_place = subject.getString(TAG_EXAM_PLACE);
exam_date = subject.getString(TAG_EXAM_DATE);
time = subject.getString(TAG_TIME);
desk_no = subject.getString(TAG_DESK_NO);
}
}
else {
//no subject found
}
}
catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data [" + e.getMessage()+"] "+json);
}
return null;
}