Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

Fix JSON escaping

Acho Arnolda8faae917ed3
diff --git a/android/app/build.gradle b/android/app/build.gradle
index d3aaab53..5a1d4fde 100644
--- a/android/app/build.gradle
+++ b/android/app/build.gradle
@@ -52,6 +52,7 @@ dependencies {
implementation 'com.beust:klaxon:5.5'
implementation 'androidx.work:work-runtime-ktx:2.7.1'
implementation 'androidx.appcompat:appcompat:1.4.2'
+ implementation 'org.apache.commons:commons-text:1.9'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
diff --git a/android/app/src/main/java/com/httpsms/HttpSmsApiService.kt b/android/app/src/main/java/com/httpsms/HttpSmsApiService.kt
index 67a5df29..17c70208 100644
--- a/android/app/src/main/java/com/httpsms/HttpSmsApiService.kt
+++ b/android/app/src/main/java/com/httpsms/HttpSmsApiService.kt
@@ -4,6 +4,7 @@ import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
+import org.apache.commons.text.StringEscapeUtils
import timber.log.Timber
import java.net.URI
import java.time.ZonedDateTime
@@ -63,9 +64,10 @@ class HttpSmsApiService(private val apiKey: String) {
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'000000'ZZZZZ")
val timestampString = formatter.format(timestamp).replace("+", "Z")
+
val body = """
{
- "content": "$content",
+ "content": "${StringEscapeUtils.escapeJson(content)}",
"from": "$from",
"timestamp": "$timestampString",
"to": "$to"
@@ -80,7 +82,7 @@ class HttpSmsApiService(private val apiKey: String) {
val response = client.newCall(request).execute()
if (!response.isSuccessful) {
- Timber.e("error response [${response.body?.string()}] with code [${response.code}] while receiving message [${body}]}]")
+ Timber.e("error response [${response.body?.string()}] with code [${response.code}] while receiving message [${body}]")
return
}