Java 如果用户有效,则执行错误的循环

Java 如果用户有效,则执行错误的循环,java,php,android,mysql,response,Java,Php,Android,Mysql,Response,我有一个活动,它通过检查来自服务器的凭据来验证用户。 这是我的密码 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.Htt

我有一个活动,它通过检查来自服务器的凭据来验证用户。 这是我的密码

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();               
        }
        });
  }
}
package com.example.androidphmy;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.ResponseHandler;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.BasicResponseHandler;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.app.ProgressDialog;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入android.widget.Toast;
公共类PaymentActivity扩展活动{
按钮b;
编辑文本,通过;
文本视图电视;
HttpPost-HttpPost;
字符串缓冲区;
HttpResponse响应;
HttpClient-HttpClient;
列出nameValuePairs;
ProgressDialog=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment);
b=(按钮)findViewById(R.id.Button01);
et=(EditText)findViewById(R.id.accountno);
pass=(EditText)findViewById(R.id.password);
tv=(TextView)findviewbyd(R.id.tv);
b、 setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
dialog=ProgressDialog.show(PaymentActivity.this,“,
“正在验证用户…”,true);
新线程(newrunnable()){
公开募捐{
付款();
}
}).start();
}
});
}
无效付款(){
试试{
httpclient=新的DefaultHttpClient();
httppost=新
HttpPost(“http://tanushreedutta.site40.net/payment_new/check.php");
//添加您的数据
nameValuePairs=新的ArrayList(2);
//发布时始终使用相同的变量名,即android端变量名和
php端变量名应该类似,
nameValuePairs.add(新的
BasicNameValuePair(“accno”,et.getText().toString().trim());
//$Edittext_value=$_POST['Edittext_value'];
nameValuePairs.add(新的
BasicNameValuePair(“bpassword”,pass.getText().toString().trim());
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
response=httpclient.execute(httppost);
//由来自coderzheaven的James编辑。从这里。。。。
ResponseHandler ResponseHandler=新
BasicResponseHandler();
最终字符串响应=httpclient.execute(httppost,responseHandler);
System.out.println(“响应:+Response”);
runOnUiThread(新的Runnable(){
公开募捐{
setText(“来自PHP的响应:+Response”);
dialog.dismise();
}
});
if(response.equalsIgnoreCase(“用户找到”)){
runOnUiThread(新的Runnable(){
公开募捐{
Toast.makeText(PaymentActivity.this,“支付成功”,Toast.LENGTH_SHORT.show();
}
});
startActivity(新意图(PaymentActivity.this、MainActivity.class));
}
否则{
showarert();
}
}捕获(例外e){
dialog.dismise();
System.out.println(“异常:+e.getMessage());
}
}
公开作废showAlert(){
PaymentActivity.this.runOnUiThread(新的Runnable(){
公开募捐{
AlertDialog.Builder=新建AlertDialog.Builder(PaymentActivity.this);
builder.setTitle(“付款错误”);
builder.setMessage(“未找到用户”)
.setCancelable(错误)
.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,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"; 
}

?>

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

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


或者,您需要发布生成响应的函数,这样我们就可以看到在给定输入的情况下它将返回什么。此处没有足够的信息,我们目前无法完全诊断问题。

请正确设置代码格式。无法判断缩进是怎么回事。您的字符串响应打印到控制台上的是什么?这里的问题可能是对
的服务调用http://tanushreedutta.site40.net/payment_new/check.php
,所以我们需要知道它返回的值。您注意到有一个名为
response
的实例变量,还有一个同名的局部变量吗?我会检查您是否在某个时候使用了错误的版本。@Nate C-K-我做了上面提到的一些更改,现在它工作正常了。非常感谢您付出了这么多的努力并帮助了我。@w00te-响应生成文件是我的check.php文件。请检查更新的问题。感谢you@user3399423-好吧,你刚才在上面的评论中说,你得到的不仅仅是“用户方”