doukai2839 2015-03-04 04:29
浏览 33
已采纳

如何比较Android和MySQL变量的值?

I am trying to compare the value from EditText in Android and compare it with a value from MySQL with PHP.

Here is my java code:

public class DisplayInformation extends MainActivity {
    private Button confirmCode;
    private ProgressDialog pDialog;
    JSONParser jsonParser = new JSONParser();
    HttpPost httppost;
    StringBuffer buffer;
    HttpResponse response;
    HttpClient httpclient;
    EditText smsCode;
    List<NameValuePair> nameValuePairs;
    private static final String LOGIN_URL = "http://10.0.2.2/callarocket/confirmedRequest.php";
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_screen);
        TextView textView1 = (TextView)findViewById(R.id.textView1);
        TextView textView2 = (TextView)findViewById(R.id.textView2);
        TextView textView3 = (TextView)findViewById(R.id.textView3);
        TextView textView4 = (TextView)findViewById(R.id.textView4);
        TextView textView5 = (TextView)findViewById(R.id.textView5);

        textView1.setText(getIntent().getStringExtra("displayName"));
        textView2.setText(getIntent().getStringExtra("displayContact"));
        textView3.setText(getIntent().getStringExtra("displayAddress"));
        textView4.setText(getIntent().getStringExtra("displayStore"));
        textView5.setText(getIntent().getStringExtra("displayRequest"));

        smsCode = (EditText) findViewById(R.id.smscode);

        confirmCode = (Button)findViewById(R.id.confirmsmscode);
        //confirmCode.setOnClickListener(this);
        confirmCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pDialog = ProgressDialog.show(DisplayInformation.this, "",
                        "Validating Code...", true);
                new Thread(new Runnable() {
                    public void run() {
                        login();
                    }
                }).start();
            }
        });
    }

   // public void onClick(View v) {
        // TODO Auto-generated method stub
        //new CreateUser().execute();


    //}

    public void login(){
        try{

            httpclient=new DefaultHttpClient();
            httppost= new HttpPost("http://10.0.2.2/callarocket/check_code.php"); // make sure the url is correct.
            //add your data
            nameValuePairs = new ArrayList<NameValuePair>(1);
            // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
            //int smsNum = Integer.parseInt(smsCode.getText().toString());

            nameValuePairs.add(new BasicNameValuePair("smsCode",smsCode.getText().toString()));  // $Edittext_value = $_POST['Edittext_value'];
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            //Execute HTTP Post Request
            response=httpclient.execute(httppost);

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

            if(response.equalsIgnoreCase("Code Found")){
                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(DisplayInformation.this,"Code Matched", Toast.LENGTH_SHORT).show();
                    }
                });

                //startActivity(new Intent(AndroidPHPConnectionDemo.this, UserPage.class));
            }else{
                showAlert();
            }

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

And here is my PHP code:

<?php 
    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();

    //$smsCode = 8044;
    $smsCode = isset($_POST['smsCode']);
    $query_search = "SELECT * FROM userrequests WHERE smsCode='".$smsCode."'";
    $query_exec = mysqli_query($db->getConnection(),$query_search) or die(mysqli_query());
    $row = mysqli_num_rows($query_exec);
    //echo $rows;
     if($row == 0) { 
        echo "Invalid SMS Code"; 
     }
     else  {
     //Authentication code is correct
        echo "Code matched"; 
    }





?>

There is no error but it always show "Invalid Code" although the code entered is correct. How do I fix this ?

  • 写回答

2条回答 默认 最新

  • dongnuo6310 2015-03-04 04:51
    关注

    Please change code as :

     if(response.equalsIgnoreCase("Code matched")){
                    runOnUiThread(new Runnable() {
                        public void run() {
                            Toast.makeText(DisplayInformation.this,"Code Matched", Toast.LENGTH_SHORT).show();
                        }
                    });
    
                    //startActivity(new Intent(AndroidPHPConnectionDemo.this, UserPage.class));
                }else{
                    showAlert();
                }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试