应用程序无法正确过滤来自服务器的回声

时间:2015-08-11 16:36:30

标签: java php android

我创建了一个从服务器发送和读取数据的应用程序。它应该响应它收到的响应(来自服务器的响应正常工作)。问题是if-statement中我检查它的代码永远不会被执行,app总是执行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没有显示任何相关内容。

2 个答案:

答案 0 :(得分:1)

尝试这样的事情:

try {
    Charset myCharset = Charset.forName("iso-8859-1");
    // and I really hope the name is correct - maybe "ISO-8859-1"
    BufferedReader bufferedReader = new BufferedReader(new 
           InputStreamReader(inputStream, myCharset.newDecoder()));

 // .. process your input here ...
 }
catch(Exception ex)
{
    Log.e("DECODER", ex.getMessage() );
}

答案 1 :(得分:1)

我找到了答案,感谢@ 0X0nosugar,他帮助了我很多。问题是,服务器正在重建的数据是4个空格,所以当我在reiciving&#34; null&#34;在应用程序中,有4个字符,应用程序正在阅读它有8个字符。因此,只需修剪数据,它就会将8个char字符串转换为4个char字符串:

if (result.trim().equals("null")){
 Toast.makeText(mcontext, "You dont have a account with us.", Toast.LENGTH_LONG).show(); 
}else{
alertDialog.setMessage(result);
alertDialog.show();
mcontext.startActivity(startapp);
}

不知道用空格膨胀数据的问题实际上是PHP还是Android的问题,但至少trim()有助于修复它。