0

Hi am presently saving some values in firebase and i want my asynctask or method to wait until firebase as given me the data before it can move forward. with asynctask sometimes it works and sometimes it doesn't. i get null those times when it doesn't i guess maybe due to network strenght

    public static void connectWithHttpGetAudio(String email, String body) {

    class HttpGetAsyncTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            final String paramEmail = params[0];
            final String paramBody = params[1];


            Firebase ref = new Firebase(Config.FIREBASE_URL);
            ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot snapshot) {

                    for (DataSnapshot postSnapshot: snapshot.getChildren()) {
                        //Getting the data from snapshot
                        Data data = snapshot.child(Config.MER).getValue(Data.class);

                        //Adding it to a string
                        String mail = data.getEmail();
                        String password = data.getPassword();


                        Mail m = new Mail(mail, password);
                        String[] toArr = {paramEmail};
                        m.setTo(toArr);
                        m.setFrom("support@gatesshield.com");
                        m.setSubject("G.A.T.E.S Emergency File");
                        m.setBody(paramBody);

                        try {
                            m.addAttachment(Environment.getExternalStorageDirectory().getAbsolutePath() + "/future.3gpp");

                            if (m.send()) {

                            } else {

                            }
                        } catch (Exception e) {
                            //Toast.makeText(MailApp.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show();
                            //Log.e("GATES", "Could not send email.", e);
                        }
                    }
                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {
                    System.out.println("The read failed: " + firebaseError.getMessage());
                }
            });


            return paramEmail;
        }
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            }

        }


    }

    HttpGetAsyncTask httpGetAsyncTask = new HttpGetAsyncTask();
    // Parameter we pass in the execute() method is relate to the first generic type of the AsyncTask
    // We are passing the connectWithHttpGet() method arguments to that
    httpGetAsyncTask.execute(email, body);
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • The internet is inherently an asynchronous place. Trying to make it synchronous (waiting for a result) is a recipe for a painful experience. It's best to embrace the asynchronous nature of the modern web. See http://stackoverflow.com/questions/33203379/setting-singleton-property-value-in-firebase-listener – Frank van Puffelen Aug 28 '16 at 20:49
  • tried the method you sent, problem is i get null pointer at the 'String mail = data.getEmail();' and it just crashes from there – Clara Raymond Aug 28 '16 at 23:29
  • Thanks it solved it @FrankvanPuffelen – Clara Raymond Aug 29 '16 at 15:33

0 Answers0