我的应用程序中有两个POST方法。一个是LoginActivity,我在其中放置了CookieHandler(代码:CookieHandler.setDefault(CookieManager())
)。在这个活动之后,我得到了一些GET方法并且cookie正在运行,但是当我想使用另一个POST方法时,我得到了AuthFailurEerror
。这是我的代码:
LoginActivity:
// this line is in onCreate method
CookieHandler.setDefault(CookieManager())
private fun login2() {
val req = object : StringRequest(Request.Method.POST,
LOGIN_API_URL,
Response.Listener { response ->
Toast.makeText(this, response, Toast.LENGTH_LONG).show()
val user = Intent(this, UserActivity::class.java)
startActivity(user)
}, Response.ErrorListener { e ->
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show()
}) {
public override fun getParams(): Map<String, String> {
val params = HashMap<String, String>()
params.put("username", username.text.toString())
params.put("password", passwd.text.toString())
return params
}
override fun getBodyContentType(): String {
return "application/x-www-form-urlencoded";
}
}
req.retryPolicy = DefaultRetryPolicy(60000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)
volleyRequest!!.add(req)
}
SecondActivity
private fun aktualizacja2() {
val req = object : StringRequest(Request.Method.POST,
UPDATE_URL,
Response.Listener { response ->
Toast.makeText(this, response.toString(), Toast.LENGTH_LONG).show()
}, Response.ErrorListener { e ->
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show()
}) {
public override fun getParams(): Map<String, String> {
val params = HashMap<String, String>()
params.put("librus_user", usernameEdit.text.toString())
params.put("librus_pswd", passwordEdit.text.toString())
return params
}
override fun getBodyContentType(): String {
return "application/x-www-form-urlencoded";
}
}
}
非常感谢您的帮助!