我开始制作一个应用程序,并首先将其连接到模拟API。现在我想将其连接到在我的PC上运行的API。
首先,我正在尝试实现登录访问。由于我的API的基本URL为http://localhost:5000/energy/api/,我将其更改为http://<< MyIPAddress >>:5000/energy/api/(还不知道这是否正确)。我正在我的实际手机上进行测试。 但是,我的代码中传递了用户名和密码作为参数,而我想使用基本身份验证登录(这是我在Postman中的做法)。 例如,这就是我想要进行身份验证的方式:
但是,我的代码却得到了这个结果:
这是我的请求管理器:
在我的AndroidManifest.xml文件中。
首先,我正在尝试实现登录访问。由于我的API的基本URL为http://localhost:5000/energy/api/,我将其更改为http://<< MyIPAddress >>:5000/energy/api/(还不知道这是否正确)。我正在我的实际手机上进行测试。 但是,我的代码中传递了用户名和密码作为参数,而我想使用基本身份验证登录(这是我在Postman中的做法)。 例如,这就是我想要进行身份验证的方式:
![enter image description here](https://istack.dev59.com/WewXB.webp)
![enter image description here](https://istack.dev59.com/zmCGd.webp)
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 代码:(由用户 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;
}
}
有人可以帮我处理基本身份验证吗? 我的基础URL是否也有问题?(添加了IP而不是localhost)。 任何提示都将不胜感激。
PS. 关于HTTP安全问题,我已经添加了。
android:usesCleartextTraffic="true"
在我的AndroidManifest.xml文件中。
Basic Base64.encode(<username>:<password>)
例如Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l
- P Fuster