我开始制作一个应用程序,并首先将其连接到模拟 API。现在我想将其连接到在我的 PC 上运行的 API。
For starters, I'm trying to implement the login access. Since my API's base URL was http://localhost:5000/energy/api/ http://localhost:5000/energy/api/ I changed it to http://<< MyIPAddress >>:5000/energy/api/ (don't know if this is right yet). I'm testing it on my actual phone.
But with my code username and password are passed on as parameters, while I'd like to login with basic auth (this is how I do it in Postman).
For example this is how I want the authentication to be done:
But with my code I get this:
这是我的请求管理器:
object RequestManager {
val interceptor = HttpLoggingInterceptor()
val client = OkHttpClient.Builder().addInterceptor(interceptor).build()
init {
interceptor.level = HttpLoggingInterceptor.Level.BODY
}
val retrofit = Retrofit.Builder()
.baseUrl("http://<<MYIP>>:5000/energy/api/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
val service = retrofit.create(Api::class.java)
我的 MainActivity.kt:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
login_button.setOnClickListener {
buttonClicked()
}
if (getTokenFromPrefs()?.length!! > 0) {
openSearchActivity()
}
}
private fun buttonClicked() {
val username = username_edittext.text.toString()
val password = password_edittext.text.toString()
Log.d("eeee","button clicked " + username_edittext.text.toString() + " password "+password_edittext.text.toString() )
val call = RequestManager.service.login(username, password)
call.enqueue(object : Callback<LoginResponse> {
override fun onResponse(call: Call<LoginResponse>, response: Response<LoginResponse>) {
if (response.isSuccessful ){
openSearchActivity()
saveTokenToPrefs(response.toString())
}else {
}
}
override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
}
})
}
val TOKENPREFSKEY = "tokenprefskey"
private fun saveTokenToPrefs(token: String) {
val pref = applicationContext.getSharedPreferences("CGEEnergy", 0)
val editor = pref.edit()
editor.putString(TOKENPREFSKEY, token)
editor.commit()
}
private fun getTokenFromPrefs(): String? {
val pref = applicationContext.getSharedPreferences("CGEEnergy", 0)
return pref.getString(TOKENPREFSKEY, "")
}
private fun openSearchActivity() {
val intent = Intent(this, SearchActivity::class.java)
startActivity(intent)
if (getTokenFromPrefs()?.length!! == 0) {
openMainActivity()
}
finish()
}
private fun openMainActivity() {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
}
}
我的 API.kt 代码:(由用户编辑和更正你好世界 https://stackoverflow.com/users/5127363/hello-world )
interface Api {
@POST("Login")
@FormUrlEncoded
fun login(@Field("username") username: String, @Field("password") password: String): Call<LoginResponse>
}
我的 LoginResponse.java 代码:
public class LoginResponse {
@SerializedName("token")
@Expose
private String token;
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
有人可以帮助我进行基本身份验证吗?
我的基本网址也有问题吗? (添加IP而不是本地主机)。
任何提示将不胜感激。
附言。对于http安全问题,我已经添加了
android:usesCleartextTraffic="true"
in my AndroidManifest.xml