我创建了一个可以从服务器发送和读取数据的应用程序。它应该对收到的响应做出反应(来自服务器的响应正常工作)。问题是我检查过的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也未显示任何相关内容。

最佳答案

尝试这样的事情:

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() );
}

10-06 13:21
查看更多