mirror of
https://github.com/markusfisch/BinaryEye.git
synced 2024-09-20 12:02:17 +02:00
Show an error when background request fails
The app shouldn't be silent if a background request fails.
This commit is contained in:
parent
1ac578845e
commit
941d875b3e
@ -685,7 +685,7 @@ fun showResult(
|
||||
}
|
||||
val scan = Scan(result)
|
||||
prefs.sendScanUrl?.let {
|
||||
scan.send(it)
|
||||
scan.send(activity.applicationContext, it)
|
||||
}
|
||||
if (prefs.useHistory) {
|
||||
scan.id = db.insertScan(scan)
|
||||
|
@ -1,24 +1,42 @@
|
||||
package de.markusfisch.android.binaryeye.net
|
||||
|
||||
import android.content.Context
|
||||
import de.markusfisch.android.binaryeye.R
|
||||
import de.markusfisch.android.binaryeye.database.Scan
|
||||
import de.markusfisch.android.binaryeye.widget.toast
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.IOException
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
import java.net.URLEncoder
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
fun Scan.send(url: String) {
|
||||
thread {
|
||||
var con: HttpURLConnection? = null
|
||||
try {
|
||||
con = URL(
|
||||
url + URLEncoder.encode(content, "utf-8")
|
||||
).openConnection() as HttpURLConnection
|
||||
con.responseCode
|
||||
} catch (e: IOException) {
|
||||
// can't do anything about it
|
||||
} finally {
|
||||
con?.disconnect()
|
||||
fun Scan.send(context: Context, url: String) {
|
||||
CoroutineScope(Dispatchers.IO).launch(Dispatchers.IO) {
|
||||
val urlAndContent = url + URLEncoder.encode(content, "utf-8")
|
||||
val code = request(urlAndContent)
|
||||
withContext(Dispatchers.Main) {
|
||||
if (code < 200 || code > 299) {
|
||||
context.toast(R.string.background_request_failed)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun request(
|
||||
url: String,
|
||||
writer: ((HttpURLConnection) -> Any)? = null
|
||||
): Int {
|
||||
var con: HttpURLConnection? = null
|
||||
return try {
|
||||
con = URL(url).openConnection() as HttpURLConnection
|
||||
writer?.invoke(con)
|
||||
con.responseCode
|
||||
} catch (e: IOException) {
|
||||
-1
|
||||
} finally {
|
||||
con?.disconnect()
|
||||
}
|
||||
}
|
||||
|
@ -114,4 +114,5 @@
|
||||
<string name="shortcut_decode">Barcode lesen</string>
|
||||
<string name="shortcut_encode">Barcode erstellen</string>
|
||||
<string name="shortcut_preferences">Einstellungen</string>
|
||||
<string name="background_request_failed">Background request failed</string>
|
||||
</resources>
|
||||
|
@ -114,4 +114,5 @@
|
||||
<string name="shortcut_decode">Escanear un código de barras</string>
|
||||
<string name="shortcut_encode">Crear un código de barras</string>
|
||||
<string name="shortcut_preferences">Ajustes</string>
|
||||
<string name="background_request_failed">Background request failed</string>
|
||||
</resources>
|
||||
|
@ -114,4 +114,5 @@
|
||||
<string name="shortcut_decode">Scan a barcode</string>
|
||||
<string name="shortcut_encode">Create a barcode</string>
|
||||
<string name="shortcut_preferences">Settings</string>
|
||||
<string name="background_request_failed">Background request failed</string>
|
||||
</resources>
|
||||
|
@ -114,4 +114,5 @@
|
||||
<string name="shortcut_decode">Scan a barcode</string>
|
||||
<string name="shortcut_encode">Create a barcode</string>
|
||||
<string name="shortcut_preferences">Settings</string>
|
||||
<string name="background_request_failed">Background request failed</string>
|
||||
</resources>
|
||||
|
@ -113,4 +113,5 @@
|
||||
<string name="shortcut_decode">Pindai barcode</string>
|
||||
<string name="shortcut_encode">Buat barcode</string>
|
||||
<string name="shortcut_preferences">Pengaturan</string>
|
||||
<string name="background_request_failed">Background request failed</string>
|
||||
</resources>
|
||||
|
@ -114,4 +114,5 @@
|
||||
<string name="shortcut_decode">Scansiona un codice a barre</string>
|
||||
<string name="shortcut_encode">Crea un codice a barre</string>
|
||||
<string name="shortcut_preferences">Impostazioni</string>
|
||||
<string name="background_request_failed">Background request failed</string>
|
||||
</resources>
|
||||
|
@ -114,4 +114,5 @@
|
||||
<string name="shortcut_decode">Scan a barcode</string>
|
||||
<string name="shortcut_encode">Create a barcode</string>
|
||||
<string name="shortcut_preferences">Settings</string>
|
||||
<string name="background_request_failed">Background request failed</string>
|
||||
</resources>
|
||||
|
@ -110,4 +110,5 @@
|
||||
<string name="auto_rotate_summary">Automatically rotate the camera frame to recognize vertical 1D barcodes. Depending on the device, this may affect performance.</string>
|
||||
<string name="save_as_file_name">Jak zapisać plik w pobranych?</string>
|
||||
<string name="issue_number">Numer wydania</string>
|
||||
<string name="background_request_failed">Background request failed</string>
|
||||
</resources>
|
||||
|
@ -114,4 +114,5 @@
|
||||
<string name="shortcut_decode">Scan a barcode</string>
|
||||
<string name="shortcut_encode">Create a barcode</string>
|
||||
<string name="shortcut_preferences">Settings</string>
|
||||
<string name="background_request_failed">Background request failed</string>
|
||||
</resources>
|
||||
|
@ -116,4 +116,5 @@
|
||||
<string name="shortcut_decode">Сканировать штрих-код</string>
|
||||
<string name="shortcut_encode">Создать штрих-код</string>
|
||||
<string name="shortcut_preferences">Настройки</string>
|
||||
<string name="background_request_failed">Background request failed</string>
|
||||
</resources>
|
||||
|
@ -113,4 +113,5 @@
|
||||
<string name="shortcut_decode">Scan a barcode</string>
|
||||
<string name="shortcut_encode">Create a barcode</string>
|
||||
<string name="shortcut_preferences">Settings</string>
|
||||
<string name="background_request_failed">Background request failed</string>
|
||||
</resources>
|
||||
|
@ -113,4 +113,5 @@
|
||||
<string name="shortcut_decode">Scan a barcode</string>
|
||||
<string name="shortcut_encode">Create a barcode</string>
|
||||
<string name="shortcut_preferences">Settings</string>
|
||||
<string name="background_request_failed">Background request failed</string>
|
||||
</resources>
|
||||
|
@ -114,4 +114,5 @@
|
||||
<string name="shortcut_decode">Scan a barcode</string>
|
||||
<string name="shortcut_encode">Create a barcode</string>
|
||||
<string name="shortcut_preferences">Settings</string>
|
||||
<string name="background_request_failed">Background request failed</string>
|
||||
</resources>
|
||||
|
Loading…
Reference in New Issue
Block a user