2013-11-09 70 views
0

我正在開發一個android應用程序,其中用戶將用戶名和密碼保存到數據庫,並將值保存在數據庫中。當用戶下一次來時,他可以登錄而不是註冊,所以當時用戶輸入的用戶名和密碼與表中保存的數據一起檢查。如果用戶名與密碼匹配,用戶將被引導到下一個頁面會顯示一條錯誤消息。在我的情況下,註冊過程工作正常,並在登錄過程中。當執行過程不起作用時。我添加了我的活動和php文件,用於連接數據庫與應用程序和logcat。請檢查這個,如果有任何錯誤,請幫我解決它。謝謝。Android MySql密碼驗證不起作用

活動

public class SignInActivity extends Activity 


{ 
    /*LoginDataBaseAdapter loginDataBaseAdapter;*/ 
    Button btnsignin; 
    String name,password, psw; 


    // Progress Dialog 
    private ProgressDialog pDialog; 

    // JSON parser class 
    JSONParser jsonParser = new JSONParser(); 



    // single product url 
    private static final String url_get_name = "http://iascpl.com/app/get_name_details.php"; 


    // JSON Node names 
    private static final String TAG_SUCCESS = "success"; 
    private static final String TAG_PRODUCT = "product"; 
    private static final String TAG_PASSWORD = "password"; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.signin_xm); 


     /*loginDataBaseAdapter=new LoginDataBaseAdapter(this); 
     loginDataBaseAdapter=loginDataBaseAdapter.open();*/ 

     btnsignin = (Button) findViewById (R.id.button401); 



     btnsignin.setOnClickListener(new View.OnClickListener() 

     { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 




       new GetNameDetails().execute(); 

      } 
     }); 
    } 


     class GetNameDetails extends AsyncTask<String, String, String> { 

      /** 
      * Before starting background thread Show Progress Dialog 
      * */ 
      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       pDialog = new ProgressDialog(SignInActivity.this); 
       pDialog.setMessage("Loading the result... Please wait..."); 
       pDialog.setIndeterminate(false); 
       pDialog.setCancelable(true); 
       pDialog.show(); 
      } 

      /** 
      * Getting product details in background thread 
      * */ 
      protected String doInBackground(String... args) 


      { 

       final EditText et1 = (EditText) findViewById (R.id.editText401); 
       final EditText et2 = (EditText) findViewById (R.id.editText402); 




       name =et1.getText().toString(); 
       password = et2.getText().toString(); 




          // Building Parameters 
          List<NameValuePair> params = new ArrayList<NameValuePair>(); 
          params.add(new BasicNameValuePair("name", name)); 




          // getting product details by making HTTP request 
          // Note that product details url will use GET request 
          JSONObject json = jsonParser.makeHttpRequest(
            url_get_name, "GET", params); 



          // check your log for json response 
          Log.d("Single Product Details", json.toString()); 


          // json success tag 

          try { 
          int success = json.getInt(TAG_SUCCESS); 

          if (success == 1) { 
           // successfully received product details 
           JSONArray productObj = json 
             .getJSONArray(TAG_PRODUCT); // JSON Array 

           // get first product object from JSON Array 
           final JSONObject product = productObj.getJSONObject(0); 

           // product with this pid found 
           // Edit Text 




           runOnUiThread(new Runnable() { 
            @Override 
            public void run() 
            { 
             // TODO Auto-generated method stub 
             try { 

              psw = product.getString(TAG_PASSWORD); 
              if (password == psw); 
              { 
               Toast.makeText(SignInActivity.this, "Login Successfull", Toast.LENGTH_LONG).show(); 


               Intent i = new Intent(SignInActivity.this,HomePageActivity.class); 
               startActivity(i); 
              } 





             } catch (JSONException e) { 
              // TODO Auto-generated catch block 
              e.printStackTrace(); 
             } 
            } 
           }); 

          }else{ 
           // product with pid not found 
          } 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 




        return null; 
       } 
/** 
          * After completing background task Dismiss the progress dialog 
          * **/ 
          protected void onPostExecute(String file_url) 
          { 
           // dismiss the dialog once got all details 
           pDialog.dismiss(); 
          } 



      } 
    } 

PHP

<?php 

/* 
* Following code will get single product details 
* A product is identified by product id (pid) 
*/ 
// array for JSON response 



$response = array(); 




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



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




// check for post data 
if (isset($_GET["name"])) { 
    $name = $_GET['name']; 

    // get a product from products table 
    $result = mysql_query("SELECT *FROM numerol WHERE name = $name"); 

    if (!empty($result)) { 
     // check for empty result 
     if (mysql_num_rows($result) > 0) { 

      $result = mysql_fetch_array($result); 

      $product = array(); 
      $product["name"] = $result["name"]; 
      $product["passwordr"] = $result["password"]; 
      // $product["price"] = $result["price"]; 
      //$product["description"] = $result["description"]; 
      $product["created_at"] = $result["created_at"]; 
      $product["updated_at"] = $result["updated_at"]; 


      // success 
      $response["success"] = 1; 

      // user node 
      $response["product"] = array(); 

      array_push($response["product"], $product); 

      // echoing JSON response 
      echo json_encode($response); 
     } else { 
      // no product found 
      $response["success"] = 0; 
      $response["message"] = "No product found"; 

      // echo no users JSON 
      echo json_encode($response); 
     } 
    } else { 
     // no product found 
     $response["success"] = 0; 
     $response["message"] = "No product found"; 

     // echo no users JSON 
     echo json_encode($response); 
    } 
} else { 
    // required field is missing 
    $response["success"] = 0; 
    $response["message"] = "Required field(s) is missing"; 

    // echoing JSON response 
    echo json_encode($response); 
} 
?> 

logcat的

11-09 09:18:01.930: E/Trace(5068): error opening trace file: No such file or directory (2) 
11-09 09:18:04.410: D/gralloc_goldfish(5068): Emulator without GPU emulation detected. 
11-09 09:18:09.303: D/dalvikvm(5068): GC_CONCURRENT freed 83K, 7% free 2712K/2916K, paused 74ms+98ms, total 704ms 
11-09 09:18:11.200: I/Choreographer(5068): Skipped 88 frames! The application may be doing too much work on its main thread. 
11-09 09:18:20.180: I/Choreographer(5068): Skipped 30 frames! The application may be doing too much work on its main thread. 
11-09 09:18:23.910: D/dalvikvm(5068): GC_FOR_ALLOC freed 54K, 6% free 2872K/3048K, paused 61ms, total 82ms 
11-09 09:18:23.931: I/dalvikvm-heap(5068): Grow heap (frag case) to 3.526MB for 635812-byte allocation 
11-09 09:18:24.101: D/dalvikvm(5068): GC_FOR_ALLOC freed 2K, 5% free 3490K/3672K, paused 165ms, total 165ms 
11-09 09:18:24.341: D/dalvikvm(5068): GC_CONCURRENT freed 2K, 5% free 3501K/3672K, paused 32ms+95ms, total 242ms 
11-09 09:18:24.841: I/Choreographer(5068): Skipped 72 frames! The application may be doing too much work on its main thread. 
11-09 09:18:25.690: I/Choreographer(5068): Skipped 83 frames! The application may be doing too much work on its main thread. 
11-09 09:18:26.030: I/Choreographer(5068): Skipped 60 frames! The application may be doing too much work on its main thread. 
11-09 09:18:26.860: I/Choreographer(5068): Skipped 30 frames! The application may be doing too much work on its main thread. 
11-09 09:18:28.152: I/Choreographer(5068): Skipped 71 frames! The application may be doing too much work on its main thread. 
11-09 09:18:29.102: I/Choreographer(5068): Skipped 32 frames! The application may be doing too much work on its main thread. 
11-09 09:18:30.110: I/Choreographer(5068): Skipped 44 frames! The application may be doing too much work on its main thread. 
11-09 09:18:30.522: I/Choreographer(5068): Skipped 34 frames! The application may be doing too much work on its main thread. 
11-09 09:18:31.310: I/Choreographer(5068): Skipped 30 frames! The application may be doing too much work on its main thread. 
11-09 09:18:33.371: I/Choreographer(5068): Skipped 30 frames! The application may be doing too much work on its main thread. 
11-09 09:18:33.580: I/Choreographer(5068): Skipped 36 frames! The application may be doing too much work on its main thread. 
11-09 09:18:37.130: I/Choreographer(5068): Skipped 31 frames! The application may be doing too much work on its main thread. 
11-09 09:18:37.562: I/Choreographer(5068): Skipped 33 frames! The application may be doing too much work on its main thread. 
11-09 09:18:39.361: I/Choreographer(5068): Skipped 45 frames! The application may be doing too much work on its main thread. 
11-09 09:18:39.500: I/Choreographer(5068): Skipped 37 frames! The application may be doing too much work on its main thread. 
11-09 09:18:41.312: I/Choreographer(5068): Skipped 30 frames! The application may be doing too much work on its main thread. 
11-09 09:18:44.911: I/Choreographer(5068): Skipped 36 frames! The application may be doing too much work on its main thread. 
11-09 09:18:45.660: I/Choreographer(5068): Skipped 32 frames! The application may be doing too much work on its main thread. 
11-09 09:18:47.970: I/Choreographer(5068): Skipped 30 frames! The application may be doing too much work on its main thread. 
11-09 09:18:50.081: I/Choreographer(5068): Skipped 39 frames! The application may be doing too much work on its main thread. 
11-09 09:18:51.350: I/Choreographer(5068): Skipped 38 frames! The application may be doing too much work on its main thread. 
11-09 09:18:51.720: D/Single Product Details(5068): {"message":"No product found","success":0} 
11-09 09:19:12.970: I/Choreographer(5068): Skipped 85 frames! The application may be doing too much work on its main thread. 
11-09 09:19:13.170: I/Choreographer(5068): Skipped 43 frames! The application may be doing too much work on its main thread. 
11-09 09:19:18.532: I/Choreographer(5068): Skipped 42 frames! The application may be doing too much work on its main thread. 
11-09 09:19:18.740: I/Choreographer(5068): Skipped 47 frames! The application may be doing too much work on its main thread. 
11-09 09:19:24.440: I/Choreographer(5068): Skipped 76 frames! The application may be doing too much work on its main thread. 
11-09 09:19:24.630: I/Choreographer(5068): Skipped 32 frames! The application may be doing too much work on its main thread. 
11-09 09:19:25.001: I/Choreographer(5068): Skipped 51 frames! The application may be doing too much work on its main thread. 
11-09 09:19:25.610: I/Choreographer(5068): Skipped 146 frames! The application may be doing too much work on its main thread. 
11-09 09:19:26.520: I/Choreographer(5068): Skipped 30 frames! The application may be doing too much work on its main thread. 
11-09 09:19:27.412: I/Choreographer(5068): Skipped 31 frames! The application may be doing too much work on its main thread. 
11-09 09:19:28.701: D/Single Product Details(5068): {"message":"No product found","success":0} 
11-09 09:19:51.432: I/Choreographer(5068): Skipped 54 frames! The application may be doing too much work on its main thread. 
11-09 09:19:53.631: D/dalvikvm(5068): GC_CONCURRENT freed 166K, 8% free 3740K/4028K, paused 74ms+101ms, total 332ms 
11-09 09:19:54.360: I/Choreographer(5068): Skipped 112 frames! The application may be doing too much work on its main thread. 
11-09 09:19:54.560: I/Choreographer(5068): Skipped 33 frames! The application may be doing too much work on its main thread. 
11-09 09:19:55.591: I/Choreographer(5068): Skipped 195 frames! The application may be doing too much work on its main thread. 
11-09 09:19:55.921: I/Choreographer(5068): Skipped 80 frames! The application may be doing too much work on its main thread. 
11-09 09:19:56.150: I/Choreographer(5068): Skipped 30 frames! The application may be doing too much work on its main thread. 
11-09 09:19:56.562: I/Choreographer(5068): Skipped 46 frames! The application may be doing too much work on its main thread. 
11-09 09:19:56.831: I/Choreographer(5068): Skipped 31 frames! The application may be doing too much work on its main thread. 
11-09 09:19:56.955: I/Choreographer(5068): Skipped 31 frames! The application may be doing too much work on its main thread. 
11-09 09:19:57.471: I/Choreographer(5068): Skipped 32 frames! The application may be doing too much work on its main thread. 
11-09 09:19:57.662: I/Choreographer(5068): Skipped 36 frames! The application may be doing too much work on its main thread. 
11-09 09:19:58.100: I/Choreographer(5068): Skipped 41 frames! The application may be doing too much work on its main thread. 
11-09 09:19:58.350: I/Choreographer(5068): Skipped 36 frames! The application may be doing too much work on its main thread. 
11-09 09:19:59.010: I/Choreographer(5068): Skipped 30 frames! The application may be doing too much work on its main thread. 
11-09 09:19:59.661: D/Single Product Details(5068): {"message":"No product found","success":0} 
11-09 09:20:46.300: I/Choreographer(5068): Skipped 35 frames! The application may be doing too much work on its main thread. 
11-09 09:23:11.740: I/Choreographer(5068): Skipped 31 frames! The application may be doing too much work on its main thread. 
11-09 09:25:55.341: I/Choreographer(5068): Skipped 32 frames! The application may be doing too much work on its main thread. 
11-09 09:26:51.270: I/Choreographer(5068): Skipped 38 frames! The application may be doing too much work on its main thread. 
11-09 09:27:01.220: I/Choreographer(5068): Skipped 58 frames! The application may be doing too much work on its main thread. 
11-09 09:28:31.342: I/Choreographer(5068): Skipped 31 frames! The application may be doing too much work on its main thread. 
11-09 09:29:01.193: I/Choreographer(5068): Skipped 37 frames! The application may be doing too much work on its main thread. 
+0

做ü通過在瀏覽器中運行檢查PHP服務? – KOTIOS

+0

http://iascpl.com/app/get_name_details.php這是網址,我收到一條消息,如成功 – roshanpeter

+0

什麼是有效的用戶名和密碼? – KOTIOS

回答

0

的doInBackground()不假設處理活動中的UI,以便示出在處理doInBackground烤麪包不是一個好主意。使用onPostExecute()來處理UI EDITED!

class GetNameDetails extends AsyncTask<String, String, String> { 
     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(SignInActivity.this); 
      pDialog.setMessage("Loading the result... Please wait..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 

     /** 
     * Getting product details in background thread 
     * */ 
     protected String doInBackground(String... args) 


     { 

      final EditText et1 = (EditText) findViewById (R.id.editText401); 
      final EditText et2 = (EditText) findViewById (R.id.editText402); 




      name =et1.getText().toString(); 
      password = et2.getText().toString(); 


         try { //edited 

         // Building Parameters 
         List<NameValuePair> params = new ArrayList<NameValuePair>(); 
         params.add(new BasicNameValuePair("name", name)); 
         params.add(new BasicNameValuePair("password", password)); //check this please. 



         // getting product details by making HTTP request 
         // Note that product details url will use GET request 
         JSONObject json = jsonParser.makeHttpRequest(
           url_get_name, "GET", params); 



         // check your log for json response 
         Log.d("Single Product Details", json.toString()); 


         // json success tag 


         int success = json.getInt(TAG_SUCCESS); 

         if (success == 1) { 
          // successfully received product details 
          JSONArray productObj = json 
            .getJSONArray(TAG_PRODUCT); // JSON Array 

          // get first product object from JSON Array 
          final JSONObject product = productObj.getJSONObject(0); 

          // product with this pid found 
          // Edit Text 

          Intent i = new Intent(SignInActivity.this,HomePageActivity.class); 
              startActivity(i); 
          return "true"; // if the login was successful return true string to the onPostExecute(String) and there show the Toast. 

         }else{ 
          // product with pid not found 
         } 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 




       return null; 
      } 
         /** 
         * After completing background task Dismiss the progress dialog 
         * **/ 
         protected void onPostExecute(String file_url) 
         { 
          // dismiss the dialog once got all details 
          pDialog.dismiss(); 
          if (file_url == "true") 
           Toast.makeText(SignInActivity.this, "Login Successfull", Toast.LENGTH_LONG).show(); 
          else 
           Toast.makeText(SignInActivity.this, "Login unsuccessfull", Toast.LENGTH_LONG).show(); 
         } 



     } 
} 
+1

這是一個怎樣的答案? – Simon

+0

請在您的答案中解釋原始代碼出錯的地方。 –

+0

@烏薩馬..我們將如何測試密碼的正確與否在此answe – roshanpeter