[英]Android: User registration with couchbase
我一直在使用此示例作为指导,以允许用户注册我的应用程序,但它不起作用。 没有语法错误,但是由于某些原因我的应用程序无法与服务器通信。 以下是我正在使用的SignupActivity.java:
public class SignupActivity extends AppCompatActivity implements View.OnTouchListener {
public static final String DB_NAME = "myapp";
private Database mDatabase = null;
@Bind(R.id.input_name) EditText _nameText;
@Bind(R.id.input_firstname) EditText _firstnameText;
@Bind(R.id.input_lastname) EditText _lastnameText;
@Bind(R.id.input_confirmpassword) EditText _confirmpasswordText;
@Bind(R.id.input_email) EditText _emailText;
@Bind(R.id.input_password) EditText _passwordText;
@Bind(R.id.btn_signup) Button _signupButton;
@Bind(R.id.link_login) TextView _loginLink;
@Bind(R.id.ScrollViewSignup) ScrollView _scrollLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
ButterKnife.bind(this);
_signupButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyUtilities.hideSoftKey(SignupActivity.this,v);
signup();
}
});
_loginLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Finish the registration screen and return to the Login activity
finish();
}
});
_scrollLayout.setOnTouchListener(this);
// Create a manager
Manager manager = null;
try {
manager = new Manager(new AndroidContext(getApplicationContext()), Manager.DEFAULT_OPTIONS);
} catch (IOException e) {
e.printStackTrace();
}
// Create or open the database named app
Database database = null;
try {
database = manager.getDatabase(DB_NAME);
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
// The properties that will be saved on the document
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("name", _nameText);
properties.put("password", _passwordText);
properties.put("firstname", _firstnameText);
properties.put("surname", _lastnameText);
properties.put("email", _emailText);
// Create a new document
Document document = database.createDocument();
// Save the document to the database
try {
document.putProperties(properties);
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
// Log the document ID (generated by the database)
// and properties
Log.d(DB_NAME, String.format("Document ID :: %s", document.getId()));
Log.d(DB_NAME, String.format("Learning %s with %s %s %s %s", (String) document.getProperty("name"), (String) document.getProperty("password"),(String) document.getProperty("firstname"),
(String) document.getProperty("surname"), (String) document.getProperty("email") ));
// Create replicators to push & pull changes to & from Sync Gateway.
URL url = null;
try {
url = new URL("http://serverurl:8080/REST-Model-context-root/resources/register");
} catch (MalformedURLException e) {
e.printStackTrace();
}
Replication push = database.createPushReplication(url);
push.setContinuous(true);
// Start replicators
push.start();
}
public void signup() {
if (!validate()) {
onSignupFailed();
return;
}
_signupButton.setEnabled(false);
final ProgressDialog progressDialog = new ProgressDialog(SignupActivity.this,
R.style.AppTheme_Dark_Dialog);
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Creating Account...");
progressDialog.show();
String name = _nameText.getText().toString();
String email = _emailText.getText().toString();
String password = _passwordText.getText().toString();
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
// On complete call either onSignupSuccess or onSignupFailed
// depending on success
onSignupSuccess();
progressDialog.dismiss();
}
}, 3000);
}
public void onSignupSuccess() {
_signupButton.setEnabled(true);
setResult(RESULT_OK, null);
finish();
}
public void onSignupFailed() {
Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show();
_signupButton.setEnabled(true);
}
public boolean validate() {
boolean valid = true;
String name = _nameText.getText().toString();
String email = _emailText.getText().toString();
String password = _passwordText.getText().toString();
if (name.isEmpty() || name.length() < 3) {
_nameText.setError("at least 3 characters");
valid = false;
} else {
_nameText.setError(null);
}
if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
_emailText.setError("enter a valid email address");
valid = false;
} else {
_emailText.setError(null);
}
if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
_passwordText.setError("between 4 and 10 alphanumeric characters");
valid = false;
} else {
_passwordText.setError(null);
}
return valid;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
MyUtilities.hideSoftKey(SignupActivity.this,v);
return true;
}
}
任何想法将不胜感激!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.