0

I have the Android project. I use Gradle and Kotlin.
I need to realize the SMS sending mechanism with Twilio.

I used this manual: How to Send an SMS from Android

So, I have created the Backend.
Here is the build.gradle for Backend:

group 'com.my.app'
version '1.0-SNAPSHOT'

buildscript {
    repositories { jcenter() }
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3'
    }
}

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'com.github.johnrengelman.shadow'

sourceCompatibility = 1.8
mainClassName = 'SMSBackend'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation group: 'junit', name: 'junit', version: '4.12'
    implementation 'com.sparkjava:spark-core:2.6.0'
    implementation group: 'com.twilio.sdk', name: 'twilio', version: '7.9.0'
    implementation 'org.slf4j:slf4j-simple:1.6.1'
}

And here is the main activity - SMSBackend.java

import com.twilio.http.TwilioRestClient;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.rest.api.v2010.account.MessageCreator;
import com.twilio.type.PhoneNumber;
import static spark.Spark.*;

import static spark.Spark.get;
import static spark.Spark.post;

public class SMSBackend {
    public static void main(String[] args) {
        get("/", (req, res) -> "Hello, World!");

        TwilioRestClient client = new TwilioRestClient.Builder(System.getenv("AC749....d462247b104a"), System.getenv("096e4788......ed95cc36c")).build();

    post("/sms", (req, res) -> {
        String body = req.queryParams("Body");
        String to = req.queryParams("To");
        String from = "+15037381694";

        Message message = new MessageCreator(
                new PhoneNumber(to),
                new PhoneNumber(from),
                body).create(client);

        return message.getSid();
    });
    }
}

After that, I use ngrok in order this app becomes available externally.

I type the command:

ngrok http 4567

And have an output as the link below:

enter image description here

And now I go to my Android application. I need to enter my telephone number, click on the button and after that receive the SMS.
For this, I put the link from ngrok into my activity.

My MyActivity.kt

package com.my.app

import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast

import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

class MyActivity : AppCompatActivity() {

private val mClient = OkHttpClient()
private val mContext = getApplicationContext()
private val message = resources.getString(R.string.textSMS)
private val userPhoneNumber = findViewById(R.id.editText) as EditText
private val buttonSend = findViewById(R.id.button) as Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_my)

        buttonSend.setOnClickListener {
            post(("https://2b722554.ngrok.io/sms"), object : Callback {
                        override fun onFailure(call: Call, e: IOException) {
                            e.printStackTrace()
                        }

                        @Throws(IOException::class)
                        override fun onResponse(call: Call, response: Response) {
                            runOnUiThread(object : Runnable {
                                public override fun run() {
                                    userPhoneNumber.setText("")
                                    message.setText("")
                                    Toast.makeText(applicationContext, "SMS Sent!", Toast.LENGTH_SHORT).show()
                                }
                            })
                        }

            })
    }

}

@Throws(IOException::class)
    internal fun post(url: String, callback: Callback): Call {
        val formBody = FormBody.Builder()
                .add("To", userPhoneNumber.getText().toString())
                .add("Body", message.getText().toString())
                .build()
             val request = Request.Builder()
                .url(url)
                .post(formBody)
                .build()
    val response = mClient.newCall(request)
    response.enqueue(callback)
    return response
}

So, when I run my application on the emulator or real device I can enter the number but the button is not working.
But I can send and receive the SMS from Twilio site, so, my Twilio number is Ok.

What am I missing?

UPD. I have changed all my kotline code. Now my application doesn't run on the emulator or real device

NaSt
  • 301
  • 1
  • 5
  • 16

1 Answers1

0

Looking at the click listener you add to the button:

buttonSend.setOnClickListener {
  fun onClick(v: View) {
    ...
  }
}

this will only create a local function called onClick but it won't execute any code inside the click listener. To get this to work, you need to remove all local function declarations and execute the request right in the click listener body:

buttonSend.setOnClickListener {
  post(getString("https://07eca790.ngrok.io/sms") ...
}

Here's a reference: Local Functions

Egor
  • 39,695
  • 10
  • 113
  • 130
  • Thank you! I'm trying. I have deleted local functions and have updated the code in the post. But now the words `post`, `catch` and `e` is red and have the issue: "Unresolved reference". What does it mean? – NaSt Dec 20 '17 at 14:51
  • Well, those are compiler errors. Re-check that your code is correct and the functions you're calling exist. – Egor Dec 21 '17 at 07:27
  • Well, I have cleaned my code. I have only one compilation error now: `post(mContext.getString("https://54c5ffb1.ngrok.io/sms")` I can see that the link is underlined with comment: "Type mismatch: inferred type is String but Int was expected". – NaSt Dec 21 '17 at 13:00
  • You don't need the `mContext.getString` part, just pass the URL to the `post()` as is – Egor Dec 21 '17 at 13:27
  • Now I use `post(("https://2b722554.ngrok.io/sms")`. This error has disappeared but now my application doesn't run on the emulator or real device. I have put all my new code in the post, can you please take a look at it? – NaSt Dec 21 '17 at 14:01
  • "my application doesn't run on the emulator or real device" is not very descriptive, are you seeing any errors in the logcat? Does the build succeed at all? – Egor Dec 21 '17 at 14:53
  • A don't see any errors, I can see that the app starting and crashing at the same time. The screen of my phone just flashing and show me the desktop. But the build in the Android Studio is successful. – NaSt Dec 21 '17 at 15:20
  • The fact that the app is crashing suggests that there should be error output in the logcat, this should give you a clue of what's wrong. Overall, it seems like you don't have a lot of experience in Android development, and I'd suggest you work through a couple of tutorials and maybe take a beginner's course on Android before trying to develop your own apps. – Egor Dec 21 '17 at 16:30