Make方法等待firebase数据

时间:2016-08-28 20:46:18

标签: android firebase firebase-realtime-database

您好我正在firebase中保存一些值,我希望我的asynctask或方法等到firebase给我数据然后才能继续前进。有了asynctask有时它可以工作,有时它不工作。我认为可能是由于网络强度而导致的那些时候我无效

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

0 个答案:

没有答案