我是新來的Android編程,我試圖從本地主機數據庫做一個簡單的註冊登錄系統。註冊工作正常,但是當我試圖從登錄中獲得響應時,我得到了一些奇怪的xml \ html類文本,而不僅僅是數據。Java android httpURLConnection很奇怪
這是Java代碼:
@Override
protected User doInBackground(Void... params)
{
HashMap<String, Object> dataToSend = new HashMap<>();
dataToSend.put("username", user.getUsername());
dataToSend.put("password", user.getPassword());
URL server;
HttpURLConnection httpRequestParams;
User returnedUser = null;
String response = "";
try {
server = new URL(SERVER_ADDRESS + "FetchUserData.php");
httpRequestParams = (HttpURLConnection)server.openConnection();
httpRequestParams.setReadTimeout(10000);
httpRequestParams.setConnectTimeout(15000);
httpRequestParams.setRequestMethod("POST");
httpRequestParams.setDoInput(true);
httpRequestParams.setDoOutput(true);
httpRequestParams.connect();
OutputStream os = httpRequestParams.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
System.out.println(dataToSend);
System.out.println(getPostDataString(dataToSend));
writer.write(getPostDataString(dataToSend));
writer.flush();
writer.close();
os.close();
int responseCode = httpRequestParams.getResponseCode();
System.out.println(responseCode);
if (responseCode == HttpURLConnection.HTTP_OK)
{
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(httpRequestParams.getInputStream()));
while ((line = br.readLine()) != null)
{
response += line;
System.out.println(response);
}
br.close();
} else
{
response = "";
throw new Exception(responseCode + "");
}
System.out.println(response);
JSONObject jObject = new JSONObject(response.toString());
if(jObject.length() == 0)
{
returnedUser = null;
}else
{
String name = jObject.getString("name");
int age = jObject.getInt("age");
returnedUser = new User(name, user.getUsername(), user.getPassword(), age);
}
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (ProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
}
return returnedUser;
}
這是php文件:
<?php
$con= new mysqli("localhost", "root", "", "users");
if ($con->connect_error)
{
die("Connection failed: " . $con->connect_error);
}
$username = $_POST["username"];
$password = $_POST["password"];
echo "HEY------------" . $username;
$query = "SELECT * FROM users WHERE username='?' AND password='?'";
$statement = $con->prepare($query);
$statement->bind_param("ss", $username, $password);
if ($statement->execute() === TRUE)
{
echo "User selected successfully.";
} else
{
echo "Error: " . $query . "<br>" . $con->error;
}
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $name, $username, $password, $age);
mysqli_stmt_fetch($statement);
$userDetails = array($name, $username, $password, $age);
mysqli_stmt_close($statement);
echo json_encode($userDetails);
$con->close();
?>
這是我得到的迴應:
<br /><font size='1'><table class='xdebug-error xe-notice xe-scream' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>(!)</span> SCREAM: Error suppression ignored for</th></tr><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>(!)</span> Notice: Undefined index: username in D:\Programs\WampServer\www\FetchUserData.php on line <i>9</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr><tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>684616</td><td bgcolor='#eeeeec'>{main}()</td><td title='D:\Programs\WampServer\www\FetchUserData.php' bgcolor='#eeeeec'>..\FetchUserData.php<b>:</b>0</td></tr></table></font><br /><font size='1'><table class='xdebug-error xe-notice xe-scream' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>(!)</span> SCREAM: Error suppression ignored for</th></tr><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>(!)</span> Notice: Undefined index: password in D:\Programs\WampServer\www\FetchUserData.php on line <i>10</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr><tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>684616</td><td bgcolor='#eeeeec'>{main}()</td><td title='D:\Programs\WampServer\www\FetchUserData.php' bgcolor='#eeeeec'>..\FetchUserData.php<b>:</b>0</td></tr></table></font>HEY------------<br /><font size='1'><table class='xdebug-error xe-warning xe-scream' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>(!)</span> SCREAM: Error suppression ignored for</th></tr><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>(!)</span> Warning: mysqli_stmt::bind_param() [<a href='mysqli-stmt.bind-param'>mysqli-stmt.bind-param</a>]: Number of variables doesn't match number of parameters in prepared statement in D:\Programs\WampServer\www\FetchUserData.php on line <i>16</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr><tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>684616</td><td bgcolor='#eeeeec'>{main}()</td><td title='D:\Programs\WampServer\www\FetchUserData.php' bgcolor='#eeeeec'>..\FetchUserData.php<b>:</b>0</td></tr><tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0111</td><td bgcolor='#eeeeec' align='right'>703168</td><td bgcolor='#eeeeec'>mysqli_stmt->bind_param()</td><td title='D:\Programs\WampServer\www\FetchUserData.php' bgcolor='#eeeeec'>..\FetchUserData.php<b>:</b>16</td></tr></table></font>User selected successfully.[null,null,null,null]
這是我得到的當我使用www文件夾中的php文件時:
User selected successfully.[user,username,pass,12]
我不知道什麼是錯的......我不知道爲什麼反應是這樣的..
什麼是你想要的預期的響應? – Tauqir