0

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

enter image description hereI do not know what to do. I am new to android development and i am using mongodb as my database. How can i solve this time out exception to get my code working. I tried out searched for this issue but did not find a simple and good descriptive solution. Help will be appreciated. Thanks in advance.

manlio
  • 18,345
  • 14
  • 76
  • 126

1 Answers1

0

The error is time out error. If you look into the detail error log.

You get exception unknown host name ds015730.mlab.com:15730

Please the timeout error is due to the wrong hostname. Please check your host name.

Time Out error is occur when user request to the server but server is not responding within time then that request throws time out error.

In your case issue is due to the wrong hostname.

Maheshwar Ligade
  • 6,709
  • 4
  • 42
  • 59
  • I am using mlab.com for mongodb i have checked that and the database has been working fine but after adding the CheckEmailDuplicate() method it is causing this error. – Yousaf Iqbal Apr 07 '16 at 18:07