X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fasync%2FSendTransactionTask.java;h=dc0c24875469a74b5d6404097a21034d1690672a;hb=bb789332571609eeb1bef6e39b7ad359227d1045;hp=cfbf5222c30a24ff41c9c1282a664da17e61a46e;hpb=4c2d1b2095d022074deed016e95cd3cca3b9f624;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java b/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java index cfbf5222..dc0c2487 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java @@ -1,5 +1,5 @@ /* - * Copyright © 2019 Damyan Ivanov. + * Copyright © 2020 Damyan Ivanov. * This file is part of MoLe. * MoLe is free software: you can distribute it and/or modify it * under the term of the GNU General Public License as published by @@ -23,13 +23,13 @@ import android.util.Log; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; -import net.ktnx.mobileledger.json.ParsedLedgerTransaction; import net.ktnx.mobileledger.model.LedgerTransaction; import net.ktnx.mobileledger.model.LedgerTransactionAccount; import net.ktnx.mobileledger.model.MobileLedgerProfile; import net.ktnx.mobileledger.utils.Globals; import net.ktnx.mobileledger.utils.Logger; import net.ktnx.mobileledger.utils.NetworkUtil; +import net.ktnx.mobileledger.utils.SimpleDate; import net.ktnx.mobileledger.utils.UrlEncodedFormData; import java.io.BufferedReader; @@ -39,8 +39,6 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.nio.charset.StandardCharsets; -import java.util.Date; -import java.util.GregorianCalendar; import java.util.List; import java.util.Locale; import java.util.Map; @@ -50,14 +48,20 @@ import java.util.regex.Pattern; import static android.os.SystemClock.sleep; import static net.ktnx.mobileledger.utils.Logger.debug; +/* TODO: get rid of the custom session/cookie and auth code? + * (the last problem with the POST was the missing content-length header) + * This will resolve itself when hledger-web 1.14+ is released with Debian/stable, + * at which point the HTML form emulation can be dropped entirely + */ + public class SendTransactionTask extends AsyncTask { private final TaskCallback taskCallback; + private final MobileLedgerProfile mProfile; + private final boolean simulate; protected String error; private String token; private String session; - private LedgerTransaction ltr; - private MobileLedgerProfile mProfile; - private boolean simulate = false; + private LedgerTransaction transaction; public SendTransactionTask(TaskCallback callback, MobileLedgerProfile profile, boolean simulate) { @@ -70,8 +74,41 @@ public class SendTransactionTask extends AsyncTask 0.3) @@ -84,17 +121,6 @@ public class SendTransactionTask extends AsyncTask= 2) - throw new IOException(String.format("aborting after %d tries", tried)); - sleep(100); - } + transaction = ledgerTransactions[0]; + + switch (mProfile.getApiVersion()) { + case auto: + Logger.debug("network", "Trying version 1.5."); + if (!send_1_15_OK()) { + Logger.debug("network", "Version 1.5 request failed. Trying with 1.14"); + if (!send_1_14_OK()) { + Logger.debug("network", + "Version 1.14 failed too. Trying HTML form emulation"); + legacySendOkWithRetry(); + } + else { + Logger.debug("network", "Version 1.14 request succeeded"); + } + } + else { + Logger.debug("network", "Version 1.15 request succeeded"); + } + break; + case html: + legacySendOkWithRetry(); + break; + case v1_14: + send_1_14_OK(); + break; + case v1_15: + send_1_15_OK(); + break; + default: + throw new IllegalStateException( + "Unexpected API version: " + mProfile.getApiVersion()); } } catch (Exception e) { @@ -250,10 +308,19 @@ public class SendTransactionTask extends AsyncTask= 2) + throw new IOException(String.format("aborting after %d tries", tried)); + sleep(100); + } + } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); taskCallback.done(error); } -} + +} \ No newline at end of file