我是 android studio 和整個 API JSON 協議的新手。我正在嘗試使登錄服務正常作業。它從 BASE_URL 中顯示的后端服務器獲取成功登錄的資訊,成功登錄會回傳狀態和令牌。所以我有以下 LoginActivity 代碼:
package com.example.mydoctor
import android.accounts.AccountManager
import android.content.Intent
import android.content.Context
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import android.widget.Toast
import com.example.mydoctor.api.ApiInterface
import com.example.mydoctor.models.LoginRequest
import com.example.mydoctor.models.LoginResponse
import kotlinx.android.synthetic.main.activity_login.*
import org.json.JSONObject
import retrofit2.*
import retrofit2.converter.gson.GsonConverterFactory
const val BASE_URL = "https://docappmy.herokuapp.com/mydoctor/user/"
class LoginActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
val clickRegister = findViewById<TextView>(R.id.clickRegister)
clickRegister.setOnClickListener {
val intent = Intent(this, RegisterActivity::class.java);
startActivity(intent)
}
// val loginbtm = findViewById<TextView>(R.id.loginbtm)
// loginbtm.setOnClickListener {
// val intent = Intent(this, MainActivity::class.java);
// startActivity(intent)
//
// }
val loginbutton = loginbtm
var amka = ""
var password = ""
loginbutton.setOnClickListener {
amka = amka_edit_text.text.toString().trim()
password = password_edit_text.text.toString().trim()
Log.d("values","The AMKA is: $amka, The password is: $password")
val retrofitBuilder = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.build()
.create(ApiInterface::class.java)
val retrofitData = retrofitBuilder.loginUser(LoginRequest(amka,password))
retrofitData.enqueue(object:Callback<LoginResponse>{
override fun onResponse(call: Call<LoginResponse>, response: Response<LoginResponse>) {
val responseData = response.body()
Log.d("SuccessLogin","The response is: $responseData")
// val intent = Intent(this@LoginActivity,MainActivity::class.java)
// startActivity(intent)
}
override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
Toast.makeText(this@LoginActivity,"FailureLogin",Toast.LENGTH_LONG).show()
Log.d("loginerror","loginerror: ${t.localizedMessage} - ${t.stackTrace} - ${t.message}")
}
})
}
這個登錄請求:
package com.example.mydoctor.models
data class LoginRequest(
val amka: String, val password: String
)
這個登錄回應:
包 com.example.mydoctor.models
import com.google.gson.annotations.SerializedName
data class LoginResponse(
@SerializedName("status")
val status: String,
@SerializedName("Token")
val token: String
)
而這個 ApiInterface :
package com.example.mydoctor.api
import com.example.mydoctor.models.LoginRequest
import com.example.mydoctor.models.LoginResponse
import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.POST
interface ApiInterface {
@POST("login")
fun loginUser(
@Body loginRequest: LoginRequest
): Call<LoginResponse>
}
所以我想檢索“狀態”和“令牌”的值,然后讓登錄按鈕在成功登錄后將我帶到我的主要活動(正確的 AMKA(與健康保險相關的 11 位數字)和密碼。我怎樣才能檢索它們并進行正確的登錄?我應該使用 SharedPreferences 嗎?如果使用,如何使用?我真的很困惑。感謝任何幫助。
編輯:
我開始添加以下函式,但隨后我需要輸入令牌的存盤位置,但不知道如何表達:
private fun saveData() {
if(amka_edit_text.text.isEmpty()){
amka_edit_text.error = "Please enter an AMKA"
return
}
if(password_edit_text.text.isEmpty()){
password_edit_text.error = "Please enter a password"
}
val mypref = getSharedPreferences("mypref", MODE_PRIVATE)
val editor = mypref.edit()
editor.putString("token",)
uj5u.com熱心網友回復:
您可以使用共享首選項。
1. 將資料寫入共享首選項
以下代碼塊將令牌值保存在共享首選項中
將以下代碼放在您的 onResponse 回呼中:
val sharedPref = getSharedPreferences(Context.MODE_PRIVATE)
val editor = sharedPref.edit()
editor.putString("TOKEN", responseData.token)
editor.apply()
2. 從共享偏好中讀取資料
val sharedPref = getSharedPreferences(Context.MODE_PRIVATE)
val token = sharedPref.getString("TOKEN", null)
您可以在需要的地方使用此令牌值。
注意:共享首選項將資料存盤在帶有鍵值的 XML 檔案中。在上面的示例中,“TOKEN”字串值成為您的密鑰。如果使用此鍵找不到值,它將回傳 null。這樣就可以檢查是否有保存的token。
如果您想了解更多詳細資訊,可以查看此檔案:https : //developer.android.com/training/data-storage/shared-preferences
我還建議您查看 Encrypted Shared Preferences,這是一種更安全的敏感資料存盤方式:https : //developer.android.com/topic/security/data
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/372862.html
上一篇:僅在滑鼠移動時進行角度系結
