3. Retrofit

Kotlin

build.gradle

dependencies {
    implementation("com.squareup.retrofit2:retrofit:2.9.0")
    implementation("com.squareup.retrofit2:converter-gson:2.9.0")
    implementation("com.squareup.okhttp3:logging-intercepter:4.9.0")
    implementation("android.lifecycle:lifecycle-runtime-ktx:2.3.0")
}

retrofit(非同期通信をするためのライブラリ)をimplementします

com.example.myapp/model/CustomData.kt

data class Customer(
    val name: String,
    val phone: Int,
    val pass: Int
)

data classを定義します

com.example.myapp/network/ApiService.kt

interface ApiService {
    @POST("wp-json/custom/v1/save_customer")
    suspend fun saveCustomer(@Body customer: Customer): Response<Unit> 
}

com.example.myapp/network/RetrofitClient.kt

object RetrofitClient {
    val instance: ApiService by lazy {
        val retrofit = Retrofit.Builder()
            .baseUrl("https://your-website.com")
            .addConverterFactory(GsonConverterFactory.create())
            .build()
        retrofit.create(ApiService::class.java)
    }
}

MainActivity.kt

class MainActivity : AppCompatActivity() {
    private lateinit var etName: EditText
    private lateinit var etPhone: EditText
    private lateinit var etPass: EditText

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

        etName = findViewById(R.id.etName)
        etPhone = findViewById(R.id.etPhone)
        etPass = findViewById(R.id.etPass)

        val btSubmit = findViewById<Button>(R.id.btSubmit)
        val listener = btSubmitClickListener()
        btSubmit.setOnClickListener(listener)
    }

    private inner class btSubmitClickListener: View.OnClickListener {
        override fun onClick(view: View) {
            val name = etName.text.toString()
            val phone = etPhone.text.toString()
            val pass = etPass.text.toString()

            if ( name.isNotEmpty() && phone.isNotEmpty() && pass.isNotEmpty() ) {
                val customer = Customer(name, phone, pass)
                submitCustomerData(customer)
            } else {
                Toast.makeText(this, "Please fill in all fields", Toast.LENGTH_LONG).show()
            }
        }
    }

    private fun submitCustomerData(customer: Customer) {
        val tvError = findViewById<TextView>(R.id.tvError)
        CoroutineScope(Dispatchers.IO).launch {
            try {
                val response = RetrofitClient.instance.saveCustomer(customer)
                if (response.isSuccessful) {
                    runOnUiThread {
                        tvError.text = "SUCCESS TO ADD CUSTOMER"
                    }
                } else {
                    runOnUiThread {
                        tvError.text = "FAILED TO ADD CUSTOMER"
                    }
                }
            } catch (e: Exception) {
                runOnUiThread {
                    tvError.text = "ERROR: ${e.message}"
                }
            }
        }
    }
}
BACK