SHARE
TWEET

Untitled

a guest May 19th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.l6z1
  2.  
  3. import android.support.v7.app.AppCompatActivity
  4. import android.os.Bundle
  5. import android.util.Log
  6. import android.view.View
  7. import android.widget.Button
  8. import android.widget.EditText
  9. import android.widget.TextView
  10. import android.widget.Toast
  11. import com.google.gson.Gson
  12. import retrofit2.Call
  13. import retrofit2.Callback
  14. import retrofit2.Response
  15. import retrofit2.Retrofit
  16. import retrofit2.converter.gson.GsonConverterFactory
  17. import retrofit2.converter.scalars.ScalarsConverterFactory
  18. import retrofit2.http.GET
  19. import retrofit2.http.Path
  20. import org.json.JSONObject
  21.  
  22.  
  23.  
  24. class MainActivity : AppCompatActivity() {
  25.  
  26.     override fun onCreate(savedInstanceState: Bundle?) {
  27.         super.onCreate(savedInstanceState)
  28.         setContentView(R.layout.activity_main)
  29.  
  30.     }
  31.  
  32.     fun getExpression() : String{
  33.         return findViewById<EditText>(R.id.editText).text.toString()
  34.     }
  35.  
  36.  
  37.     interface NewtonApi{
  38.         @GET("/{operation}/{expression}")
  39.         fun getResult(@Path("operation") operation : String, @Path("expression") expression : String) : Call<String>
  40.     }
  41.  
  42.     fun calculateResult(operation: String, expression: String){
  43.         var retrofit = Retrofit.Builder()
  44.             .baseUrl("https://newton.now.sh")
  45.             .addConverterFactory(ScalarsConverterFactory.create())
  46.             .build()
  47.  
  48.         var api = retrofit.create(NewtonApi::class.java)
  49.  
  50.         var call = api.getResult(operation, expression)
  51.  
  52.         call.enqueue(object : Callback<String> {
  53.             override fun onResponse(call: Call<String>, response: Response<String>) {
  54.                 var result = response.body().toString()
  55.                 val obj = JSONObject(result)
  56.                 Log.d("am2019", "result: ${obj.get("result")}")
  57.                 findViewById<TextView>(R.id.result).text = obj.get("result").toString()
  58.  
  59.             }
  60.  
  61.             override fun onFailure(call: Call<String>, t: Throwable) {
  62.                 Log.d("am2019", "error")
  63.                 findViewById<TextView>(R.id.result).text = "error"
  64.             }
  65.         })
  66.     }
  67.  
  68.     fun onClick(view : View){
  69.         calculateResult((view as Button).text.toString(), getExpression())
  70.         //Toast.makeText(this, (view as Button).text, Toast.LENGTH_SHORT).show()
  71.     }
  72. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top