【发布时间】:2015-08-11 16:36:30
【问题描述】:
我创建了一个从服务器发送和读取数据的应用程序。它应该对收到的响应做出反应(来自服务器的响应正常工作)。问题是我检查的 if 语句中的代码从未执行过,应用程序总是执行 else 块中的代码。我的 PHP 代码是:
<?php
require "init.php";
$user_name = $_POST["user_name"];
$user_pass = $_POST["user_pass"];
$user_name = utf8_encode($user_name);
$user_pass = utf8_encode($user_pass);
$sql_query = "SELECT user_name FROM user_info WHERE user_name ='".$user_name."' AND user_pass = '".$user_pass."' ;";
$result = mysqli_query($con, $sql_query);
if(mysqli_num_rows($result) == 1){
echo "Login Success..Welcome ";
}else{
echo"null";
}
mysqli_close($con);
?>
这是我的 AsyncTask 类:
public class SendLogData extends AsyncTask <String, Void, String>{
String serverURL = "http://192.168.1.105/myapp/login.php";
Intent startapp ;
private Context mcontext;
private String response;
private String error = null;
ProgressDialog alertDialog;
public SendLogData(Context context, Intent intent) {
startapp = intent;
mcontext = context;
}
@Override
protected void onPreExecute() {
alertDialog = new ProgressDialog(mcontext);
alertDialog.setMessage("Connecting to server");
}
@Override
protected String doInBackground(String... params) {
String username = params[0];
String password = params[1];
try {
URL url = new URL(serverURL);
HttpURLConnection client = (HttpURLConnection) url.openConnection();
client.setRequestMethod("POST");
client.setDoOutput(true);
client.setDoInput(true);
OutputStream outputStream = client.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String data = URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" +
URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = client.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String line = "";
while ((line = bufferedReader.readLine())!=null)
{
response = "";
response+= line;
}
bufferedReader.close();
inputStream.close();
client.disconnect();
return response;
} catch (IOException e) {
error = e.getMessage();
}
return null;
}
@Override
protected void onPostExecute(String result) {
if (result.equals("null")){// This is were, the appp never actives the if, and goes right to the else
Toast.makeText(mcontext, "You dont have a account with us.", Toast.LENGTH_LONG).show();
}else{
alertDialog.setMessage(result);
alertDialog.show();
mcontext.startActivity(startapp);
}
}
}
注意:由于我没有从应用程序中收到任何错误,因此我无法调试它,并且 logcat 没有显示任何相关内容。
【问题讨论】:
-
看看以“while ((line = bufferedReader.readLine())!=null)”开头的块——你真的想从缓冲阅读器中取出最后一行吗?
-
是的,我只想要服务器检索到的回声,我不需要阅读其他任何内容。唯一的问题是,如果我从服务器收到“null”,应用程序无法处理它,并直接进入 else 块。
-
您能否将 Toast 中的“结果”显示为“onPostExecute()”中的第一条语句?顺便说一句,您可以通过编写自己的日志条目来使用内联调试 --> android.util.Log
-
"result" 显示在 doInBackground 中返回的字符串 "response"。因此,如果服务器返回“Login Success..Welcome”,它将显示该文本,并且与 php 的 else 中的 echo 相同。
-
请原谅我的坚持:“将/应该显示”或“确实显示”?毕竟,我们谈论的是理解意外行为。那么“null”在哪里丢失了呢?