我有一个活动,通过检查服务器的凭据来验证用户。 这是我的代码。
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()方法。我的代码有任何问题。任何建议或建议将受到高度赞赏。提前谢谢大家!
答案 0 :(得分:0)
这里没有任何复杂的事情发生;你的回答显然不是说“用户发现”;所以你应该彻底检查一下。
只需附加调试器,在该行设置断点,并查看响应在这种情况下实际说明的内容。当你可以暂停并逐步查看/查看各种变量的状态时,应该很明显有什么不妥。
或者,您需要发布生成响应的函数,以便我们可以看到在您输入的情况下它会返回什么。我们目前没有足够的信息来全面诊断问题。