java - 在有效用户的情况下执行错误的循环

标签 java php android mysql response

我有一项 Activity 通过检查来自服务器的凭据来验证用户。 这是我的代码。

PaymentActivity.java

package com.example.androidphpmy;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class PaymentActivity extends Activity {
  Button b;
  EditText et,pass;
  TextView tv;
  HttpPost httppost;
  StringBuffer buffer;
  HttpResponse response;
  HttpClient httpclient;
  List<NameValuePair> nameValuePairs;
  ProgressDialog dialog = null;

  @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_payment);

      b = (Button)findViewById(R.id.Button01);  
      et = (EditText)findViewById(R.id.accountno);
      pass= (EditText)findViewById(R.id.password);
      tv = (TextView)findViewById(R.id.tv);

      b.setOnClickListener(new OnClickListener() {


          @Override
          public void onClick(View v) {

          dialog = ProgressDialog.show(PaymentActivity.this, "", 
            "Validating user...", true);
          new Thread(new Runnable() {
            public void run() {
            payment();                        
            }
            }).start();               
          }
          });
    }

  void payment(){
    try{            

      httpclient=new DefaultHttpClient();
      httppost= new 
        HttpPost("http://tanushreedutta.site40.net/payment_new/check.php");
      //add your data
      nameValuePairs = new ArrayList<NameValuePair>(2);
      // Always use the same variable name for posting i.e the android side variable name and
      php side variable name should be similar,    
          nameValuePairs.add(new           
              BasicNameValuePair("accno",et.getText().toString().trim())); 
      // $Edittext_value = $_POST['Edittext_value'];
      nameValuePairs.add(new 
          BasicNameValuePair("bpassword",pass.getText().toString().trim())); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      //Execute HTTP Post Request
      response=httpclient.execute(httppost);
      // edited by James from coderzheaven.. from here....
      ResponseHandler<String> responseHandler = new      
        BasicResponseHandler();
      final String response = httpclient.execute(httppost, responseHandler);
      System.out.println("Response : " + response); 
      runOnUiThread(new Runnable() {
          public void run() {
          tv.setText("Response from PHP : " + response);
          dialog.dismiss();
          }
          });

      if(response.equalsIgnoreCase("User Found")){
        runOnUiThread(new Runnable() {
            public void run() {
            Toast.makeText(PaymentActivity.this,"Payment Successful",Toast.LENGTH_SHORT).show();
            }
            });

        startActivity(new Intent(PaymentActivity.this, MainActivity.class));
      }
      else{
        showAlert();                
      }

    }catch(Exception e){
      dialog.dismiss();
      System.out.println("Exception : " + e.getMessage());
    }
  }
  public void showAlert(){
    PaymentActivity.this.runOnUiThread(new Runnable() {
        public void run() {
        AlertDialog.Builder builder = new AlertDialog.Builder(PaymentActivity.this);
        builder.setTitle("Payment Error.");
        builder.setMessage("User not Found.")  
        .setCancelable(false)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
          }
          });                     
        AlertDialog alert = builder.create();
        alert.show();               
        }
        });
  }
}

响应生成php文件

<?php
$hostname_localhost ="url";
$database_localhost ="databasename";
$username_localhost ="myusername";
$password_localhost ="xxxx";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
 or
trigger_error(mysql_error(),E_USER_ERROR);

mysql_select_db($database_localhost, $localhost);

$accno = $_POST['accno'];
$bpassword = $_POST['bpassword'];
$query_search = "select * from details where accno = '".$accno."' AND bpassword =      
'".$bpassword. "'";
$query_exec = mysql_query($query_search) or die(mysql_error());
$rows = mysql_num_rows($query_exec);
//echo $rows;
if($rows == 0) { 
echo "No Such User Found"; 
}
else  {
echo "User Found"; 
}

?>

现在的问题是,每当我在响应文本中输入帐号和密码时,它都会给我正确的输出,但在任何情况下(有效或无效用户)它都会执行“else”语句,即执行 showAlert() 方法。我的代码有什么问题吗?任何建议或意见将不胜感激。提前谢谢大家!

最佳答案

这里没有什么复杂的事情发生;您的回答显然不是说“找到用户”;所以你应该彻底检查一下。

只需附加您的调试器,在该行设置一个断点,然后查看在这种情况下响应的实际内容。当您可以在那里暂停并单步浏览/查看各种变量的状态时,应该很明显出了什么问题。

或者,您需要发布生成响应的函数,以便我们可以看到它会根据您的输入返回什么。此处没有足够的信息供我们目前全面诊断问题。

关于java - 在有效用户的情况下执行错误的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22287692/

相关文章:

javascript - 添加基于php数据库结果的表单确认

java - 如何在不创建数据库的情况下保存谜语游戏中的答案?

java - 在java swing中设置窗口的最小大小限制

java - mvn 单元测试公共(public)资源文件夹和读取文件功能

Android Studio 无法在 Elementary OS 中运行

android - 如何在 Kotlin 中为环境设置不同的常量

java - 无限滚动图像 ViewPager

java - Java 方法的退出点

php - gridview 小部件过滤器中的 yii2 下拉列表

php - 如何在 Drupal 中进行绝对重定向?