2
皆さんのおかげで、光を当てることができます。私はphpとphpMyAdminを介してMySQL DBに接続しているAndroidのCapstoneプロジェクトに取り組んでいます。 3つのパラメータ(childId、date1、date5)をAndroidからphpスクリプトに渡そうとしていますが、動作していません。私が試して結果をプリントアウトすると、Logcatにエラーが表示されます。私は2つのパラメータ(childIdとdate1)を渡すだけですべてが機能するので、問題は私のPHPスクリプトにあると思います。どんな助けでも大歓迎です。ここに私のcode.phpが最初です:Android、php、MySQL、phpMyAdmin
<?php
require "conn.php";
$response = array();
if($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$childId = $_POST["childId"];
$date1 = $_POST["date"];
$date5 = $_POST["date"];
$sql = "SELECT * FROM child_sessions WHERE childId like '$childId' AND date BETWEEN '$date1' AND '$date5'";
$result = $conn->query($sql);
if($result->num_rows > 0) {
$response["child_sessions"] = array();
while($row = mysqli_fetch_array($result)) {
$child_sessions = array();
$child_sessions["date"] = $row["date"];
$child_sessions["timeIn"] = $row["timeIn"];
$child_sessions["timeOut"] = $row["timeOut"];
$child_sessions["duration"] = $row["duration"];
$child_sessions["childId"] = $row["childId"];
$child_sessions["sessionCost"] = $row["sessionCost"];
array_push($response["child_sessions"], $child_sessions);
}
// success
$response["success"] = 1;
echo json_encode($response);
} else {
echo "Error";
}
$conn->close();
?>
そしてここでは、Androidからの私の接続です:事前に
private String childSessions(String childId, String date1, String date5) {
InputStream inputStream = null;
String line = null;
Log.d(TAG, "childSessions: " + childId);
Log.d(TAG, "childSessions: " + date1);
Log.d(TAG, "childSessions: " + date5);
try {
String login_url = Constants.RETRIEVE_CHILD_SESSION_FOR_WEEK;
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod(Constants.REQUEST_METHOD);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("childId", "UTF-8") + "=" + URLEncoder.encode(childId, "UTF-8") + "&"+
URLEncoder.encode("date", "UTF-8") + "=" + URLEncoder.encode(date1, "UTF-8") + "&" +
URLEncoder.encode("date", "UTF-8") + "=" + URLEncoder.encode(date5, "UTF-8");
bufferedWriter.write(post_data);
Log.d(TAG, "childSessions: " + post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
Log.d(TAG, "childSessions: " + bufferedReader);
String result = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
Log.d(TAG, "childSessions: " + result);
}
bufferedReader.close();
//result += System.getProperty("line.separator") + responseOutput.toString();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
感謝。