0

I have 3 recursion methods which make post requests by okhttp The first one is:

 private void sendStatPhoto() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (!mSharedPreferences.getString("Statistics_photo", "").equals("")) {
                    mStatisticsPhoto = mSharedPreferences.getString("Statistics_photo", "").split(";");
                    System.out.println(mStatisticsPhoto[0]);
                    mPhoto = DBReader.read(mSQLiteDatabase,
                            "photo_statistics_" + mStatisticsPhoto[0],
                            "names");

                    mDictionaryForRequest = new Hashtable();
                    mDictionaryForRequest.put("login_admin", QuestionnaireActivity.this.getString(R.string.login_admin));

                    OkHttpClient client = new OkHttpClient();
                    client.newCall(new DoRequest(QuestionnaireActivity.this).Post(mDictionaryForRequest, QuestionnaireActivity.this.getString(R.string.url), mPhoto))
                            .enqueue(new Callback() {
                                         @Override
                                         public void onFailure(Call call, IOException e) {
                                             e.printStackTrace();
                                             System.out.println("Ошибка");
                                             runOnUiThread(new Runnable() {
                                                 @Override
                                                 public void run() {
                                                     Toast.makeText(QuestionnaireActivity.this, "Ошибка отправки!", Toast.LENGTH_SHORT).show();
                                                     Log.d("TAG", "3 error");
                                                 }
                                             });
                                         }

                                         @Override
                                         public void onResponse(Call call, final Response response) throws IOException {

                                             String responseCallback = response.body().string();

                                             if (responseCallback.substring(1, responseCallback.length() - 1).equals("1")) {

                                                 mSQLiteDatabase.execSQL("DROP TABLE if exists " + "photo_statistics_" + mStatisticsPhoto[0]);

                                                 SharedPreferences.Editor editor = mSharedPreferences.edit()
                                                         .putString("Statistics_photo", mSharedPreferences.getString("Statistics_photo", "").replace(mStatisticsPhoto[0] + ";", "")); //temp-оставшиеся анкеты.
                                                 editor.apply();
                                                 new File(getFilesDir(), "files/" + mPhoto + ".jpg").delete();

                                                 System.out.println("Deleted");
                                                 Log.d("TAG", "3 good");
                                                 sendStatPhoto();
                                             }
                                         }
                                     }
                            );
                }

            }
        });
    }

And 2 other methods which make exactly the same with some other kind of data

 private void sendAudio() {...}
 private void send() {...}

I need to execute them one after another. I tried to make the array of methods

 Runnable[] methods = new Runnable[]{
                new Runnable() {
                    @Override
                    public void run() {
                        Log.d("TAG", "1");
                        send();
                    }
                },
                new Runnable() {
                    @Override
                    public void run() {
                        Log.d("TAG", "2");
                         sendAudio();
                    }
                },
                new Runnable() {
                    @Override
                    public void run() {
                        Log.d("TAG", "3");
                        sendStatPhoto();
                    }
                }
        };

But okhttp makes all posts in new Threads and it does not work.

if (Internet.hasConnection(this)) {
            Log.d("TAG", "start");
            ExecutorService service = Executors.newSingleThreadExecutor();
            for (Runnable r : methods)
                service.submit(r);
            service.shutdown();
            Log.d("TAG", "finish");

        }

How to execute one post after another? The problem is that i want to send a pack of data(like 10-20 times) in 3 metods(in sequence 1-2-3) but i get the data for 3-rd some later and if i ll put the execution of the next method in onResponse i ll lose the 3-rd

Dmitry Ushkevich
  • 392
  • 2
  • 14

1 Answers1

0

use .then to concat http methods:

$http.post(UserPass, {username : vm.data.username, password : vm.data.password 
}).then(function (res){
 var serverresponse = res.data;
}).then(function (){
// another http request here and so on
 $http.post({}).then(function (){
//etc...
});

});

;)

Hope this helps, for me this was a nightmare the first time!

(".then" waits for the http to execute and then proceeds with what it has inside)

Foo Bar
  • 165
  • 2
  • 14