一个接一个地执行okhttp post请求

时间:2017-05-25 08:21:45

标签: java android multithreading

我有3个递归方法,可以通过okhttp发出请求 第一个是:

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

            }
        });
    }

另外两种方法与其他类型的数据完全相同

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

我需要一个接一个地执行它们。 我试图制作方法数组

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

但okhttp会在新主题中发布所有帖子,但它不起作用。

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

        }

如何一个接一个地执行一个帖子? 问题是我想在3个方法中发送一组数据(如10-20次)(按顺序1-2-3)但是我稍后会得到3-data的数据,如果我要执行onResponse中的下一个方法我将失去第3个

1 个答案:

答案 0 :(得分:0)

使用.then连接http方法:

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

});

希望这有帮助,对我来说这是第一次噩梦!

(“。然后”等待http执行,然后继续执行内部的操作)