Guest User

Untitled

a guest
Jan 11th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. class ClientJsonModel(var client: ClientModel, var error: String)
  2.  
  3. class ClientModel (
  4. var userId: Int = 0,
  5. var name: String = "",
  6. var bankAccount: String = "",
  7. var agency: String = "",
  8. var balance: Double = 0.0
  9. )
  10.  
  11. fun reqGET(reqUrl: String): String? {
  12. var response: String? = null
  13. lateinit var conn: HttpURLConnection
  14.  
  15. try {
  16. val url = URL(reqUrl)
  17. conn = url.openConnection() as HttpURLConnection
  18. conn.requestMethod = "GET"
  19.  
  20. // lê o response
  21. val ins = BufferedInputStream(conn.inputStream)
  22. response = streamToString(ins)
  23.  
  24. conn.connect()
  25. conn.inputStream.close()
  26. } catch (e: MalformedURLException) {
  27. Log.e("LOG", "MalformedURLException: " + e.message)
  28. } catch (e: ProtocolException) {
  29. Log.e("LOG", "ProtocolException: " + e.message)
  30. } catch (e: IOException) {
  31. Log.e("LOG", "IOException: " + e.message)
  32. } catch (e: Exception) {
  33. Log.e("LOG", "Exception: " + e.message)
  34. } finally {
  35. conn.disconnect()
  36. }
  37.  
  38. return response
  39. }
  40.  
  41. fun reqPOST(reqUrl: String, params: Map<String, String>): String? {
  42. var response: String? = ""
  43. lateinit var conn: HttpURLConnection
  44.  
  45. try {
  46. val url = URL(reqUrl)
  47. conn = url.openConnection() as HttpURLConnection
  48. conn.requestMethod = "POST"
  49.  
  50. conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
  51. conn.doOutput = true
  52. conn.doInput = true
  53. conn.connectTimeout = 30 * 1000
  54.  
  55. var parsedParams = ""
  56. var isFirst = true
  57.  
  58. params.keys.forEach { it ->
  59. if (!isFirst) {
  60. parsedParams += "&"
  61. isFirst = false
  62. }
  63.  
  64. parsedParams += URLEncoder.encode(it + "=" + params[it], "UTF-8")
  65. }
  66.  
  67. conn.connect()
  68.  
  69. val bw = BufferedWriter(OutputStreamWriter(conn.outputStream, "UTF-8"))
  70. bw.write(parsedParams)
  71. bw.flush()
  72. conn.outputStream.close()
  73. bw.close()
  74.  
  75. response = streamToString(BufferedInputStream(conn.inputStream))
  76. } catch (e: MalformedURLException) {
  77. Log.e("LOG", "MalformedURLException: " + e.message)
  78. } catch (e: ProtocolException) {
  79. Log.e("LOG", "ProtocolException: " + e.message)
  80. } catch (e: IOException) {
  81. Log.e("LOG", "IOException: " + e.message)
  82. } catch (e: Exception) {
  83. Log.e("LOG", "Exception: " + e.message)
  84. } finally {
  85. conn.disconnect()
  86. }
  87.  
  88. return response
  89. }
  90.  
  91. class LoginPresenter : LoginContract.Presenter<ClientJsonModel> {
  92. override fun showDataFromServer(data: (ClientJsonModel) -> Unit) {
  93.  
  94. }
  95.  
  96. fun makeLogin(user: String, password: String) {
  97. MakeLogin(user, password).execute()
  98. }
  99.  
  100. class MakeLogin(var userP: String, var passwordP: String) : AsyncTask<Void, Void, String>() {
  101. lateinit var callback: (ClientJsonModel) -> Unit
  102. var dataBody : MutableMap<String, String> = mutableMapOf()
  103.  
  104. override fun doInBackground(vararg params: Void?): String {
  105. dataBody["user"] = userP
  106. dataBody["password"] = passwordP
  107.  
  108. val jsonString = Helper.reqPOST("https://ban-tst-xxhh.herokuapp.com/api/login", dataBody)
  109.  
  110. try {
  111. val jo = JSONObject(jsonString)
  112.  
  113. val bill = jo.getJSONObject("userAccount")
  114. val id = bill.getInt("userId")
  115. val name = bill.getString("name")
  116. val bank = bill.getString("bankAccount")
  117. val agency = bill.getString("agency")
  118. val balance = bill.getDouble("balance")
  119.  
  120. val error = jo.getJSONObject("error")
  121.  
  122. callback.invoke(
  123. ClientJsonModel(
  124. ClientModel(
  125. id,
  126. name.toString(),
  127. bank.toString(),
  128. agency.toString(),
  129. balance
  130. ), error.getString("")
  131. )
  132. )
  133.  
  134. } catch (e: Exception) {
  135. e.printStackTrace()
  136. Log.e("LOG", "LoginPresenter -> doInBackground: Error -> ${e.message} | Causa: ${e.cause.toString()}")
  137. }
  138. return ""
  139. }
  140.  
  141. }
  142. }
  143.  
  144. class LoginActivity : CoreActivity(), LoginContract.View {
  145. var presenter : LoginPresenter? = null
  146.  
  147. override fun onCreate(savedInstanceState: Bundle?) {
  148. super.onCreate(savedInstanceState)
  149. setContentView(R.layout.activity_login)
  150.  
  151. setUp()
  152. }
  153.  
  154. fun onLogin(v: View) {
  155. presenter?.makeLogin(edt_login_user.text.toString(), edt_login_password.text.toString())
  156. startActivity(Intent(this, AccountActivity::class.java))
  157. }
  158.  
  159. override fun setUp() {
  160. presenter = LoginPresenter()
  161. }
  162. }
Add Comment
Please, Sign In to add comment