package com.donateblood.blooddonation;
/**
* Created by YouCaf Iqbal on 4/5/2016.
*/
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import butterknife.ButterKnife;
import butterknife.InjectView;
public class SignupActivity extends AppCompatActivity {
private Spinner mySpinner; private String bloodgroup,name,password,number,location,email;
@InjectView(R.id.input_name) EditText _nameText;
@InjectView(R.id.input_email) EditText _emailText;
@InjectView(R.id.input_password) EditText _passwordText;
@InjectView(R.id.btn_signup) Button _signupButton;
@InjectView(R.id.link_login) TextView _loginLink;
@InjectView(R.id.input_location) TextView _locText;
@InjectView(R.id.input_number) TextView _numText;
DB db; boolean duplicate=false;
DBCursor cursor;
DBCollection collection;
Database dbobj = new Database();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
Spinner spinner =(Spinner) findViewById(R.id.spinner);
String[] list = getResources().getStringArray(R.array.blood_type);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.spinner_layout,R.id.txt,list);
spinner.setAdapter(adapter);
ButterKnife.inject(this);
_signupButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signup();
}
});
_loginLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Finish the registration screen and return to the Login activity
finish();
}
});
}
public void signup() {
if (validate()==false) {
onSignupFailed();
return;
}
if(CheckEmailDuplicate()){
Toast.makeText(getBaseContext(), "Account with this Email already exists", Toast.LENGTH_LONG).show();
return;
}
_signupButton.setEnabled(false);
dbAsync signupThread = new dbAsync();
signupThread.execute();
}
public void onSignupSuccess() {
_signupButton.setEnabled(true);
setResult(RESULT_OK, null);
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(intent);
finish();
}
public void onSignupFailed() {
Toast.makeText(getBaseContext(), "Sign Up failed", Toast.LENGTH_LONG).show();
_signupButton.setEnabled(true);
}
public boolean validate() {
boolean valid = true;
GetUserDetails();
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);
}
if (location.isEmpty()) {
_locText.setError("Enter location");
valid = false;
} else {
_locText.setError(null);
}
if (number.isEmpty()) {
_numText.setError("Enter valid number");
valid = false;
} else {
_numText.setError(null);
}
return valid;
}
public class dbAsync extends AsyncTask<Void,Void,Void> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(SignupActivity.this);
pDialog.setMessage("Creating Account");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Void doInBackground(Void... voids) {
GetUserDetails();
dbobj.insertUser(name,email,password,location,number,bloodgroup);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
pDialog.dismiss();
Toast.makeText(getBaseContext(), "Created Successfully", Toast.LENGTH_LONG).show();
onSignupSuccess();
}
}
public void GetUserDetails(){
mySpinner=(Spinner) findViewById(R.id.spinner);
bloodgroup = mySpinner.getSelectedItem().toString();
name = _nameText.getText().toString();
email = _emailText.getText().toString();
password = _passwordText.getText().toString();
location = _locText.getText().toString();
number = _numText.getText().toString();
}
public boolean CheckEmailDuplicate(){
email = _emailText.getText().toString();
db = dbobj.getconnection();
collection= db.getCollection("UserDetails");
BasicDBObject query = new BasicDBObject();
query.put("email", email);
cursor = collection.find(query);
if(cursor.hasNext()){
duplicate=true;
}
return duplicate;
}
}
我不知道该怎么做。我是android开发新手,我使用mongodb作为我的数据库。我怎样才能解决这个超时问题以使我的代码正常工作。我尝试搜索此问题,但没有找到一个简单而好的描述性解决方案。帮助将不胜感激。提前谢谢。
答案 0 :(得分:0)
错误是超时错误。如果您查看详细错误日志。
您获得异常未知主机名ds015730.mlab.com:15730
请timeout error
错误hostname
。请检查您的主机名。
当用户向服务器请求服务器时,会发生超时错误 在时间内没有响应,那个请求会导致超时错误。
在您的情况下,问题是由于错误的主机名。