2017-05-25 86 views
0

我有3种递归方法,这使得通过okhttp POST请求,第一个是:执行okhttp POST请求此起彼伏

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

      } 
     }); 
    } 

和其他2的方法,这使得一些其他类型的数据

的一模一样
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个metods(序列1-2-3)中发送一个数据包(比如10-20次),但是我后来得到了3 -dd的数据,如果我将执行在onResponse下一个方法我会失去3挡

+0

当你从第一次回应,当你得到第二反应开始3开始第2位。 –

+0

好主意,但(通过用户界面逻辑)的第一次和第二次执行一次,3 - RD的数据得到一些以后,它不会被发送,如果我发送1和2 –

+0

没有得到*但什么* ? –

回答

0

使用。然后到Concat的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,然后用它有什么内部收益)

+0

很多thx,我的解决方案有些不同,但具有相同的原理) –

+0

别客气!谢谢你的积极:) –