我是Android编程的新手,我试图从localhost数据库中创建一个简单的注册登录系统。注册工作正常,但是当我试图从登录中获得响应时,我得到了一些奇怪的xml \ html文本,而不仅仅是数据。
这是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]
我不知道出了什么问题......我不明白为什么回答是这样的......
答案 0 :(得分:0)
错误告诉您PHP没有在POST中获取用户名/密码。删除httpRequestParams.connect();
- 您在设置参数之前过早连接。无论如何getResponseCode()
都可以做到。
答案 1 :(得分:0)
请删除这些行
$userDetails = array($name, $username, $password, $age);
echo json_encode($userDetails);