我有多个PHP文件执行相同的操作,但信息不同。无论使用哪个PHP文件,我都有相同的android代码。
这是android代码:
URL url = new URL("http://url/NewMessage.php");
String data = URLEncoder.encode("Username", "UTF-8")
+ "=" + URLEncoder.encode(Username, "UTF-8");
URLConnection conn = url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
wr.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
其余代码读取json ...
PHP代码:
<?php
//Create Connection
$con = mysqli_connect("mysql.binhoster.com", "bla", "bla","bla");
//Check Connection
if(mysqli_connect_errno()){
$Response['Response'] = "Connection Error";
$Response['Error'] = 1;
$Response['Message'] = mysqli_connect_error();
echo json_encode($Response);
mysqli_close($con);
}
//Create Result Variable
$Result = array();
//Retrieve Info
$Username = $_POST['Username'];
//Create Query
$Result = mysqli_query($con,"SELECT * FROM Users WHERE Username='$Username'");
//Execute Query and loop through results
if (!$Result){
$Response['Response'] = "Sorry there was an Error";
$Response['Error'] = 2;
$Response['Message'] = "Sorry there was an error, please try again later!";
echo json_encode($Response);
mysqli_close($con);
}
else {
while ($row = mysqli_fetch_array($Result)){
if($row){
$Response['Response'] = "Success";
$Response['Error'] = 0;
$Response['Message'] = "User exists";
echo json_encode($Response);
}
else {
$Response['Response'] = "Failure";
$Response['Error'] = 1;
$Response['Message'] = "User does not exist!";
echo json_encode($Response);
}
}
}
其他大约5个PHP文件在性质上与上面那些非常相似。 android代码的唯一区别取决于我使用的PHP文件是URL和String数据。当我从缓冲的阅读器中读取所有内容时,除了<script type="text/javascript">
之外,我什么也得不到服务器。但是,它适用于我的任何其他PHP文件。基于此,我假设问题在PHP文件中。但是,当我在服务器的文件管理器中双击php文件时,它会给我一个响应,它会在新窗口中显示json。我已经多次查看了PHP代码,但我无法弄清楚错误是什么。有谁看到我哪里出错了?令我感到困惑的主要问题是,举个例子,我在android代码中唯一要改变的是我的其他PHP文件的URL,&#34; LogIn.php&#34;,它会给我一个回应。
编辑:
经过一段时间的捣乱之后,我认为这绝对是PHP文件的一部分。当我发布某些内容时,它只返回任何内容如果我什么都不给它,它会因某种原因返回成功的回复。