无法从MySQL Android Volley响应中获取我的数据

时间:2016-05-25 04:48:08

标签: php android mysql android-volley

你可以帮我解决这个问题吗,我一直坚持使用登录部分。我可以成功注册但无法登录。

Login.java

 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/tx 
h

ttp://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

<context:property-placeholder location="classpath:properties/database.properties" />

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="packagesToScan" value="com.thebrandsapp" />
        <property name="hibernateProperties">
            <props> 
                <prop key="hibernate.connection.driver_class">${jdbc.driverClassName}</prop>
                <prop key="hibernate.connection.url">${jdbc.url}</prop>
                <prop key="hibernate.connection.username">${jdbc.username}</prop>
                <prop key="hibernate.connection.password">${jdbc.password}</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.connection.autocommit">false</prop>        
                <prop key="hibernate.cp30.min_size">1</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.c3p0.testConnectionOnCheckout">true</prop>
                <prop key="hibernate.c3p0.idle_test_period">0</prop>
                <prop key="hibernate.c3p0.preferredTestQuery">SELECT 1</prop>
                <prop key="hibernate.c3p0.max_size">100</prop>
        <prop key="hibernate.c3p0.min_size">5</prop>
        <prop key="hibernate.c3p0.max_size">100</prop>
        <prop key="hibernate.c3p0.timeout">2000</prop>
        <prop key="hibernate.c3p0.acquire_increment">3</prop>

    <prop key="hibernate.c3p0.max_statements">0</prop>
        <prop key="hibernate.connection.release_mode">after_transaction</prop>
                <prop key="hibernate.jdbc.use_get_generated_keys">true</prop>
<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
            </props>
        </property>
    </bean>

<!-- transaction management stuff…. -->
<bean id="transactionManager"
    class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>


<tx:annotation-driven transaction-manager="transactionManager"
    proxy-target-class="false" />

</beans>

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

PHP - 登录 userLogin.php

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);
}}

我进入调试模式并跟踪它,我得到了这个结果 Debug

step to

错误在php代码中。响应。

1 个答案:

答案 0 :(得分: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();
}