当前位置: 动力学知识库 > 问答 > 编程问答 >

Register Error in android hive login/registration tutorial

问题描述:

I have read many post about this issue, my app works fine, and when user register the data post into database server but not showing any toast message that this is success and dont take me to main activity

I am using xampp as my server and here is the java, and manifest code.

package info.androidhive.loginandregistration.activity;

import android.app.Activity;

import android.app.ProgressDialog;

import android.content.Intent;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

import com.android.volley.Request.Method;

import com.android.volley.Response;

import com.android.volley.VolleyError;

import com.android.volley.toolbox.StringRequest;

import org.json.JSONException;

import org.json.JSONObject;

import java.util.HashMap;

import java.util.Map;

import info.androidhive.loginandregistration.R;

import info.androidhive.loginandregistration.app.AppConfig;

import info.androidhive.loginandregistration.app.AppController;

import info.androidhive.loginandregistration.helper.SQLiteHandler;

import info.androidhive.loginandregistration.helper.SessionManager;

public class RegisterActivity extends Activity {

private static final String TAG = RegisterActivity.class.getSimpleName();

private Button btnRegister;

private Button btnLinkToLogin;

private EditText inputFullName;

private EditText inputEmail;

private EditText inputPassword;

private ProgressDialog pDialog;

private SessionManager session;

private SQLiteHandler db;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_register);

inputFullName = (EditText) findViewById(R.id.name);

inputEmail = (EditText) findViewById(R.id.email);

inputPassword = (EditText) findViewById(R.id.password);

btnRegister = (Button) findViewById(R.id.btnRegister);

btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);

// Progress dialog

pDialog = new ProgressDialog(this);

pDialog.setCancelable(false);

// Session manager

session = new SessionManager(getApplicationContext());

// SQLite database handler

db = new SQLiteHandler(getApplicationContext());

// Check if user is already logged in or not

if (session.isLoggedIn()) {

// User is already logged in. Take him to main activity

Intent intent = new Intent(RegisterActivity.this,

MainActivity.class);

startActivity(intent);

finish();

}

// Register Button Click event

btnRegister.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {

String name = inputFullName.getText().toString().trim();

String email = inputEmail.getText().toString().trim();

String password = inputPassword.getText().toString().trim();

if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty()) {

registerUser(name, email, password);

} else {

Toast.makeText(getApplicationContext(),

"Please enter your details!", Toast.LENGTH_LONG)

.show();

}

}

});

// Link to Login Screen

btnLinkToLogin.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {

Intent i = new Intent(getApplicationContext(),

LoginActivity.class);

startActivity(i);

finish();

}

});

}

/**

* Function to store user in MySQL database will post params(tag, name,

* email, password) to register url

* */

private void registerUser(final String name, final String email,

final String password) {

// Tag used to cancel the request

String tag_string_req = "req_register";

pDialog.setMessage("Registering ...");

showDialog();

StringRequest strReq = new StringRequest(Method.POST,

AppConfig.URL_REGISTER, new Response.Listener<String>() {

@Override

public void onResponse(String response) {

Log.d(TAG, "Register Response: " + response.toString());

hideDialog();

try {

JSONObject jObj = new JSONObject(response);

boolean error = jObj.getBoolean("error");

if (!error) {

// User successfully stored in MySQL

// Now store the user in sqlite

String uid = jObj.getString("apiKey");

String name = jObj.getString("name");

String email = jObj.getString("email");

String created_at = jObj.getString("createdAt");

// Inserting row in users table

db.addUser(name, email, uid, created_at);

Toast.makeText(getApplicationContext(), "User successfully registered. Try login now!", Toast.LENGTH_LONG).show();

// Launch login activity

Intent intent = new Intent(

RegisterActivity.this,

LoginActivity.class);

startActivity(intent);

finish();

} else {

// Error occurred in registration. Get the error

// message

String errorMsg = jObj.getString("message");

Toast.makeText(getApplicationContext(),

errorMsg, Toast.LENGTH_LONG).show();

}

} catch (JSONException e) {

e.printStackTrace();

}

}

}, new Response.ErrorListener() {

@Override

public void onErrorResponse(VolleyError error) {

Log.e(TAG, "Registration Error: " + error.getMessage());

Toast.makeText(getApplicationContext(),

error.getMessage(), Toast.LENGTH_LONG).show();

hideDialog();

}

}) {

@Override

protected Map<String, String> getParams() {

// Posting params to register url

Map<String, String> params = new HashMap<String, String>();

params.put("name", name);

params.put("email", email);

params.put("password", password);

return params;

}

};

// Adding request to request queue

AppController.getInstance().addToRequestQueue(strReq, tag_string_req);

}

private void showDialog() {

if (!pDialog.isShowing())

pDialog.show();

}

private void hideDialog() {

if (pDialog.isShowing())

pDialog.dismiss();

}

}

网友答案:

I too had faced difficulties in following Android Hive tutorials in the beginning. You should gain some knowledge about Databases and Storage Options in android using the official docs.

To start off with basic tutorials, you can follow

  • this for a Login Screen Tutorial and

  • this one for Registration, Login tutorial with SQLite Database.

The tutorials are very informative and easy to understand. After grasping these, you can proceed ahead with advanced tutorials.

网友答案:

I've already found the error after searching; this register activity store data to sqlite which is I think that's not necessary.

Because in my code here :

try {
        JSONObject jObj = new JSONObject(response);
        boolean error = jObj.getBoolean("error");
        if (!error) {
        // User successfully stored in MySQL
        // Now store the user in sqlite
        String uid = jObj.getString("apiKey");
        String name = jObj.getString("name");
        String email = jObj.getString("email");
        String created_at = jObj.getString("createdAt");

it need apiKey json object to store data in mysql but my api doesn't provide api key to register; here's the code:

$app->post('/register', function() use ($app) {
            // check for required params
            verifyRequiredParams(array('name', 'email', 'password'));

            $response = array();

            // reading post params
            $name = $app->request->post('name');
            $email = $app->request->post('email');
            $password = $app->request->post('password');

            // validating email address
            validateEmail($email);

            $db = new DbHandler();
            $res = $db->createUser($name, $email, $password);

            if ($res == USER_CREATED_SUCCESSFULLY) {
                $response["error"] = false;
                $response["message"] = "You are successfully registered";

            } else if ($res == USER_CREATE_FAILED) {
                $response["error"] = true;
                $response["message"] = "Oops! An error occurred while registereing";
            } else if ($res == USER_ALREADY_EXISTED) {
                $response["error"] = true;
                $response["message"] = "Sorry, this email already existed";
            }
            // echo json response
            echoRespnse(201, $response);
        });

So the solution we can either delete the api key if we insist to store data to sqlite or we don't have to store any data to sqlite, we just store it to our mysql.

分享给朋友:
您可能感兴趣的文章:
随机阅读: