changed from uploadFull and uploadDelt to uploadDelta only(works better).

This commit is contained in:
oxidiert
2025-08-14 07:23:27 +02:00
parent 17b455a60a
commit a0c906b754
3 changed files with 36 additions and 58 deletions

View File

@ -21,16 +21,6 @@ object DatabaseDownloader {
fun downloadAndReplaceDatabase(context: Context) {
CoroutineScope(Dispatchers.IO).launch {
try {
// Alte DB sofort löschen, bevor irgendwas heruntergeladen wird
val dbFile = context.getDatabasePath(DB_NAME)
if (dbFile.exists()) {
if (dbFile.delete()) {
Log.d("DOWNLOAD", "Bestehende lokale DB gelöscht.")
} else {
Log.e("DOWNLOAD", "Konnte bestehende DB nicht löschen.")
}
}
Log.d("DOWNLOAD", "Download gestartet: $SERVER_DOWNLOAD_URL")
val request = Request.Builder()
@ -51,7 +41,10 @@ object DatabaseDownloader {
}
Log.d("DOWNLOAD", "Datei gespeichert: ${downloadedFile.absolutePath}")
val dbFile = context.getDatabasePath(DB_NAME)
if (dbFile.exists()) dbFile.delete()
downloadedFile.copyTo(dbFile, overwrite = true)
Log.d("DOWNLOAD", "Neue DB erfolgreich eingesetzt")
} catch (e: Exception) {

View File

@ -9,8 +9,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import android.database.Cursor
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.asRequestBody
import org.json.JSONArray
import org.json.JSONObject
import java.io.File
@ -20,7 +18,6 @@ import kotlin.system.exitProcess
object DatabaseUploader {
private const val DB_NAME = "questionnaire_database"
private const val SERVER_FULL_URL = "http://49.13.157.44/uploadFull.php"
private const val SERVER_DELTA_URL = "http://49.13.157.44/uploadDelta.php"
private const val SERVER_CHECK_URL = "http://49.13.157.44/checkDatabaseExists.php"
private const val API_TOKEN = "MEIN_SUPER_GEHEIMES_TOKEN_12345"
@ -61,8 +58,8 @@ object DatabaseUploader {
Log.d("UPLOAD", "Server-Datenbank vorhanden → Delta-Upload")
uploadDelta(dbFile)
} else {
Log.d("UPLOAD", "Keine Server-Datenbank → Full-Upload")
uploadFull(dbFile)
Log.d("UPLOAD", "Keine Server-Datenbank → Delta-Upload")
uploadDelta(dbFile)
}
} catch (e: Exception) {
@ -97,24 +94,6 @@ object DatabaseUploader {
}
}
private fun uploadFull(file: File) {
val requestBody = MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart(
"file", file.name,
file.asRequestBody("application/octet-stream".toMediaTypeOrNull())
)
.addFormDataPart("token", API_TOKEN)
.build()
val request = Request.Builder()
.url(SERVER_FULL_URL)
.post(requestBody)
.build()
client.newCall(request).enqueue(serverCallback(file))
}
private fun uploadDelta(file: File) {
try {
val db = SQLiteDatabase.openDatabase(file.absolutePath, null, SQLiteDatabase.OPEN_READONLY)
@ -124,8 +103,12 @@ object DatabaseUploader {
put("questionnaires", queryToJsonArray(db, "SELECT id FROM questionnaires"))
put("questions", queryToJsonArray(db, "SELECT questionId, questionnaireId, question FROM questions"))
put("answers", queryToJsonArray(db, "SELECT clientCode, questionId, answerValue FROM answers"))
put("completed_questionnaires",
queryToJsonArray(db, "SELECT clientCode, questionnaireId, timestamp, isDone, sumPoints FROM completed_questionnaires")
put(
"completed_questionnaires",
queryToJsonArray(
db,
"SELECT clientCode, questionnaireId, timestamp, isDone, sumPoints FROM completed_questionnaires"
)
)
}
@ -142,7 +125,30 @@ object DatabaseUploader {
.post(requestBody)
.build()
client.newCall(request).enqueue(serverCallback(file))
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
Log.e("UPLOAD", "Delta-Upload fehlgeschlagen: ${e.message}")
}
override fun onResponse(call: Call, response: Response) {
val body = try {
response.body?.string() ?: "Keine Response"
} catch (e: Exception) {
"Fehler beim Lesen der Response: ${e.message}"
}
if (response.isSuccessful) {
Log.d("UPLOAD", "Delta-Upload erfolgreich: $body")
if (file.delete()) {
Log.d("UPLOAD", "Lokale DB gelöscht.")
exitProcess(0)
} else {
Log.e("UPLOAD", "Löschen der lokalen DB fehlgeschlagen.")
}
} else {
Log.e("UPLOAD", "Delta-Upload fehlgeschlagen: ${response.code} $body")
}
}
})
} catch (e: Exception) {
Log.e("UPLOAD", "Fehler beim Delta-Upload", e)
@ -177,25 +183,4 @@ object DatabaseUploader {
}
return jsonArray
}
private fun serverCallback(file: File) = object : Callback {
override fun onFailure(call: Call, e: IOException) {
Log.e("UPLOAD", "Upload fehlgeschlagen: ${e.message}")
}
override fun onResponse(call: Call, response: Response) {
val body = try { response.body?.string() ?: "Keine Response" } catch (e: Exception) { "Fehler beim Lesen der Response: ${e.message}" }
if (response.isSuccessful) {
Log.d("UPLOAD", "Upload erfolgreich: $body")
if (file.delete()) {
Log.d("UPLOAD", "Lokale DB gelöscht.")
exitProcess(0)
} else {
Log.e("UPLOAD", "Löschen der lokalen DB fehlgeschlagen.")
}
} else {
Log.e("UPLOAD", "Upload fehlgeschlagen: ${response.code} $body")
}
}
}
}

View File

@ -554,7 +554,7 @@ class HandlerOpeningScreen(private val activity: MainActivity) {
GlobalValues.LAST_CLIENT_CODE = clientCode
Toast.makeText(activity, "Datenbank wird verschlüsselt...", Toast.LENGTH_SHORT).show()
Toast.makeText(activity, "Datenbank wird hochgeladen...", Toast.LENGTH_SHORT).show()
DatabaseUploader.uploadDatabase(activity)
}
}