2016-05-25 16 views
0

私はこれを手伝ってもらえますか?私はログイン部分に悩まされています。私は正常に登録しますが、ログインに失敗することができますMySQL Androidボレーの応答からデータを取得できません

public class LoginRequestVolley extends StringRequest { 
private static final String KEY_EMAIL = "emailAddress"; 
private static final String KEY_PASSWORD = "password"; 

private Map<String, String> params; 

//FOR LOGIN VOLLEY 
public LoginRequestVolley(String emailAddress, String password, 
          Response.Listener<String> listener, Response.ErrorListener errorListener){ 
    super(Method.POST, ServerScripts.PHP_SCRIPT_PATH + ServerScripts.PHP_LOGIN, listener, errorListener); 
    params = new HashMap<>(); 
    params.put(KEY_EMAIL,emailAddress); 
    params.put(KEY_PASSWORD,password); 
}} 

PHPは Login.java

//LOGIN VOLLEY REQUEST 
public void loginUser(){ 
    final String userEmail = editTextEmailAddress.getText().toString().toLowerCase().trim(); 
    final String userPass = editTextPassword.getText().toString().trim(); 

    //RESPONSE TRUE 
    Response.Listener<String> listener = new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
       //JSON RESPONSE 
       try { 
        JSONObject jsonObject = new JSONObject(response); 
        boolean recordFound = jsonObject.getBoolean("success"); 

        if (recordFound) { 
         //GET RESPONSE FROM SERVER STORED TEMPORARILY, THEN TO SHARED PREFERENCE 
         //SAME ORDER AS WITH THE PHP CODE SEQUENCE 
         String uniqueId = jsonObject.getString("uniqueID"); 
         String userType = jsonObject.getString("userType"); 
         String firstName = jsonObject.getString("FirstName"); 
         String lastName = jsonObject.getString("LastName"); 
         String birthDate = jsonObject.getString("Birthdate"); 
         String gender = jsonObject.getString("Gender"); 
         String occupation = jsonObject.getString("Occupation"); 
         String dateJoined = jsonObject.getString("DateRegister"); 
         String orgJoined = jsonObject.getString("UserOrgJoined"); 
         String friends = jsonObject.getString("UserFriends"); 
         String volunteer = jsonObject.getString("volunteeredTotal"); 

         //STORE THE CURRENT USER LOGGED IN BEFORE GO TO MAIN- SESSION START 
         SharedPreferences userSharedPreference = LoginActivity.this.getSharedPreferences(UserSharedPreference.SHARED_PREFERENCES_HOLDER, Context.MODE_PRIVATE); 
         SharedPreferences.Editor spEditor = userSharedPreference.edit(); 
         spEditor.putBoolean(ServerScripts.LOGIN_RESPONSE, true); 

         //GET DATA FROM THE SERVER DATABASE AND SAVE TO PREFERENCE 
         spEditor.putString(UserSharedPreference.EMAIL_ADDRESS, userEmail); 
         spEditor.putString(UserSharedPreference.KEY_USERNAME_EMAIL_ADDRESS, userEmail); 
         spEditor.putString(UserSharedPreference.USER_UNIQUE_ID, uniqueId); 
         spEditor.putString(UserSharedPreference.FIRST_NAME, firstName); 
         spEditor.putString(UserSharedPreference.LAST_NAME, lastName); 
         spEditor.putString(UserSharedPreference.GENDER, gender); 
         spEditor.putString(UserSharedPreference.BIRTH_DATE, birthDate); 
         spEditor.putString(UserSharedPreference.OCCUPATION, occupation); 
         spEditor.putString(UserSharedPreference.USER_JOINED_DATE, dateJoined); 
         spEditor.putString(UserSharedPreference.USER_TYPE, userType); 
         spEditor.putString(UserSharedPreference.USER_UNIQUE_ID, uniqueId); 
         spEditor.putString(UserSharedPreference.COMMUNITY_FRIENDS, friends); 
         spEditor.putString(UserSharedPreference.ORGANIZATION_JOINED, orgJoined); 
         spEditor.putString(UserSharedPreference.VOLUNTEERED, volunteer); 


         spEditor.apply(); 
         progressDialogLogin.dismiss(); 

         Intent intentStartMainActivity = new Intent(LoginActivity.this, MainActivity.class); 
         intentStartMainActivity.putExtra("emailAddress", UserSharedPreference.EMAIL_ADDRESS); 
         intentStartMainActivity.putExtra("firstName", UserSharedPreference.FIRST_NAME); 
         intentStartMainActivity.putExtra("lastName", UserSharedPreference.LAST_NAME); 
         startActivity(intentStartMainActivity); 
         finish(); 
        } 
       } catch (JSONException e) { 
        e.getStackTrace(); 
        //Log.i("","Get"); 
       } 

     } 
    }; 
    //RESPONSE FALSE 
    Response.ErrorListener errorListener = new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      String failed = error + ""; 
      if (failed.equalsIgnoreCase("failed")) { 
       AlertDialog.Builder response = new AlertDialog.Builder(LoginActivity.this); 
       response.setTitle(R.string.error) 
         .setMessage(R.string.somethingWentWrong) 
         .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, int which) { 
           //DO NOTHING 
          } 
         }) 
         .create() 
         .show(); 
      } 
     } 
    }; 

    LoginRequestVolley loginRequestVolley = new LoginRequestVolley(userEmail,userPass,listener,errorListener); 
    RequestQueue requestQueue = Volley.newRequestQueue(LoginActivity.this); 
    requestQueue.add(loginRequestVolley); 
    progressDialogLogin = new ProgressDialog(this); 
    progressDialogLogin.setTitle("Connecting To Server"); 
    progressDialogLogin.setMessage("This may take a few seconds. Retrieving Data..."); 
    progressDialogLogin.getProgress(); 
    progressDialogLogin.show(); 
} 

LoginVolley.java - 。

userLogin.php ログインを
<?php 

if($_SERVER['REQUEST_METHOD'] == 'POST'){ 
//ASSIGN TO VARIABLES - HOLDERS 
    $userEmail = $_POST['emailAddress']; 
    $userPass = $_POST['password']; 

    require('dbConnect.php'); 
     //SQLI STATEMENT - GET SPECIFIC DATA  
    $SQLi_LOGIN = "SELECT uniqueID, userType, FirstName, LastName, Gender, 
          Birthday, Occupation, EmailAddress, Password ,DateRegister, 
          UserOrgJoined, UserFriends, volunteeredTotal 
          FROM useryayong WHERE 
          EmailAddress = ? AND Password = ?"; 

    $statement = mysqli_prepare($dbConnect,$SQLi_LOGIN); 
    mysqli_stmt_bind_param($statement, "ss", $userEmail,$userPass); 
    mysqli_stmt_execute($statement) or die("Error:".mysqli_error($dbConnect));      
    mysqli_stmt_store_result($results = $statement); 
    mysqli_stmt_bind_result($statement, $colUniqueId, $colUserType, $colFirstName,$colLastName, 
          $colGender, $colBirthday,$colOccupation,$colEmail, $colPassword, 
          $colDateRegister,$colUserOrgJoined,$colUserFriends, 
          $colVolunteer); 


    //$recordSearch = mysqli_query($dbConnect, $SQLi_LOGIN) or die("Error".mysqli_error($dbConnect)); 


     $recordFound = array(); 
     $recordFound["success"] = false; 

    while(mysqli_stmt_fetch($results)){     
     $recordFound["success"] = true; 
     $recordFound = $_POST["uniqueID"] = $colUniqueId; 
     $recordFound = $_POST["userDefaultType"] = $colUserType; 
     $recordFound = $_POST["userFirstName"] = $colFirstName; 
     $recordFound = $_POST["userLastName"] = $colLastName; 
     $recordFound = $_POST["userGender"] = $colGender; 
     $recordFound = $_POST["userBirthdate"] = $colBirthday; 
     $recordFound = $_POST["userOccupation"] = $colOccupation; 
     $recordFound = $_POST["userDateRegister"] = $colDateRegister; 
     $recordFound = $_POST["UserOrgNoJoined"] = $colUserOrgJoined; 
     $recordFound = $_POST["UserNoFriends"] = $colUserFriends; 
     $recordFound = $_POST["volunteeredTotalNo"] = $colVolunteer; 
    } 
     echo json_encode($recordFound);} 

は私がデバッグモードに入って、それをトレース、私はこのエラーがPHPコードである Debug

step to

をもたらしました。レスポンス。

+1

onResponseとonErrorResponseメソッドにデバッグポイントを入れます。デバッグコード –

+0

私はいつもmysqli_fetch_array()がパラメータ1を期待しています – RoCk

+1

これをチェックしてくださいhttp://forums.phpfreaks.com/topic/129396-solved-why-warning-mysqli-fetch-array-expects-parameter-1-to-be -mysqli-result/ –

答えて

0
//LOGIN VOLLEY REQUEST 
public void loginUser(){ 
final String userEmail = editTextEmailAddress.getText().toString().toLowerCase().trim(); 
final String userPass = editTextPassword.getText().toString().trim(); 

//RESPONSE TRUE 
Response.Listener<String> listener = new Response.Listener<String>() { 
    @Override 
    public void onResponse(String response) { 
      //JSON RESPONSE 
      try { 
       JSONArray array = response.getJSONArray("your_array"); 
       JSONObject jsonObject = array.getJSONObject(0); 

       if (recordFound) { 
        //GET RESPONSE FROM SERVER STORED TEMPORARILY, THEN TO SHARED PREFERENCE 
        //SAME ORDER AS WITH THE PHP CODE SEQUENCE 
        String uniqueId = jsonObject.getString("uniqueID"); 
        String userType = jsonObject.getString("userType"); 
        String firstName = jsonObject.getString("FirstName"); 
        String lastName = jsonObject.getString("LastName"); 
        String birthDate = jsonObject.getString("Birthdate"); 
        String gender = jsonObject.getString("Gender"); 
        String occupation = jsonObject.getString("Occupation"); 
        String dateJoined = jsonObject.getString("DateRegister"); 
        String orgJoined = jsonObject.getString("UserOrgJoined"); 
        String friends = jsonObject.getString("UserFriends"); 
        String volunteer = jsonObject.getString("volunteeredTotal"); 

        //STORE THE CURRENT USER LOGGED IN BEFORE GO TO MAIN- SESSION START 
        SharedPreferences userSharedPreference = LoginActivity.this.getSharedPreferences(UserSharedPreference.SHARED_PREFERENCES_HOLDER, Context.MODE_PRIVATE); 
        SharedPreferences.Editor spEditor = userSharedPreference.edit(); 
        spEditor.putBoolean(ServerScripts.LOGIN_RESPONSE, true); 

        //GET DATA FROM THE SERVER DATABASE AND SAVE TO PREFERENCE 
        spEditor.putString(UserSharedPreference.EMAIL_ADDRESS, userEmail); 
        spEditor.putString(UserSharedPreference.KEY_USERNAME_EMAIL_ADDRESS, userEmail); 
        spEditor.putString(UserSharedPreference.USER_UNIQUE_ID, uniqueId); 
        spEditor.putString(UserSharedPreference.FIRST_NAME, firstName); 
        spEditor.putString(UserSharedPreference.LAST_NAME, lastName); 
        spEditor.putString(UserSharedPreference.GENDER, gender); 
        spEditor.putString(UserSharedPreference.BIRTH_DATE, birthDate); 
        spEditor.putString(UserSharedPreference.OCCUPATION, occupation); 
        spEditor.putString(UserSharedPreference.USER_JOINED_DATE, dateJoined); 
        spEditor.putString(UserSharedPreference.USER_TYPE, userType); 
        spEditor.putString(UserSharedPreference.USER_UNIQUE_ID, uniqueId); 
        spEditor.putString(UserSharedPreference.COMMUNITY_FRIENDS, friends); 
        spEditor.putString(UserSharedPreference.ORGANIZATION_JOINED, orgJoined); 
        spEditor.putString(UserSharedPreference.VOLUNTEERED, volunteer); 


        spEditor.apply(); 
        progressDialogLogin.dismiss(); 

        Intent intentStartMainActivity = new Intent(LoginActivity.this, MainActivity.class); 
        intentStartMainActivity.putExtra("emailAddress", UserSharedPreference.EMAIL_ADDRESS); 
        intentStartMainActivity.putExtra("firstName", UserSharedPreference.FIRST_NAME); 
        intentStartMainActivity.putExtra("lastName", UserSharedPreference.LAST_NAME); 
        startActivity(intentStartMainActivity); 
        finish(); 
       } 
      } catch (JSONException e) { 
       e.getStackTrace(); 
       //Log.i("","Get"); 
      } 

    } 
}; 
//RESPONSE FALSE 
Response.ErrorListener errorListener = new Response.ErrorListener() { 
    @Override 
    public void onErrorResponse(VolleyError error) { 
     String failed = error + ""; 
     if (failed.equalsIgnoreCase("failed")) { 
      AlertDialog.Builder response = new AlertDialog.Builder(LoginActivity.this); 
      response.setTitle(R.string.error) 
        .setMessage(R.string.somethingWentWrong) 
        .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          //DO NOTHING 
         } 
        }) 
        .create() 
        .show(); 
     } 
    } 
}; 

LoginRequestVolley loginRequestVolley = new LoginRequestVolley(userEmail,userPass,listener,errorListener); 
RequestQueue requestQueue = Volley.newRequestQueue(LoginActivity.this); 
requestQueue.add(loginRequestVolley); 
progressDialogLogin = new ProgressDialog(this); 
progressDialogLogin.setTitle("Connecting To Server"); 
progressDialogLogin.setMessage("This may take a few seconds. Retrieving Data..."); 
progressDialogLogin.getProgress(); 
progressDialogLogin.show(); 
} 
関連する問題