我有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擋
當你從第一次迴應,當你得到第二反應開始3開始第2位。 –
好主意,但(通過用戶界面邏輯)的第一次和第二次執行一次,3 - RD的數據得到一些以後,它不會被髮送,如果我發送1和2 –
沒有得到*但什麼* ? –