android - 注册 VOLLEY 和 KOTLIN 问题,有人可以告诉我什么是问题,我得到以下错误:
问题描述
我面临以下代码错误可能有人帮助我我正在尝试通过此代码注册到应用程序
注册 VOLLEY 和 KOTLIN 问题,有人可以告诉我什么是问题,我得到以下错误:
我正在添加此以获取更多详细信息,以便发布我的问题
W/System.err: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:111)
`override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
loading = findViewById<ProgressBar>(R.id.loading)
name = findViewById<EditText>(R.id.name)
email = findViewById<EditText>(R.id.email)
password = findViewById<EditText>(R.id.password)
c_password = findViewById<EditText>(R.id.password)
btn_regist = findViewById<Button>(R.id.btn_regist)
btn_regist.setOnClickListener() { v -> regist() }
}
private fun regist(){
loading.visibility = View.VISIBLE
btn_regist.visibility = View.GONE
val name:String = this.name.text.trim().toString()
val email:String = this.email.text.trim().toString()
val password:String = this.password.text.trim().toString()
val stringRequest = object : StringRequest(com.android.volley.Request.Method.POST, URL_REGIST,
com.android.volley.Response.Listener<String>() { response ->
try {
val jsonObject = JSONObject(response)
val success : String = jsonObject.getString("success")
if (success.equals("1")){
Toast.makeText(this, "Register Success ! Eyyvalll !", Toast.LENGTH_SHORT).show()
}
}
catch (e: JSONException){
e.printStackTrace()
Toast.makeText(this, "Register Error ! goh !!!! :/" + e.toString() , Toast.LENGTH_SHORT).show()
loading.visibility = View.GONE
btn_regist.visibility = View.VISIBLE
}
}, com.android.volley.Response.ErrorListener {
error -> error.printStackTrace()
Toast.makeText(this, "Register Error ! SHIT :/" + error.toString() , Toast.LENGTH_SHORT).show()
loading.visibility = View.GONE
btn_regist.visibility = View.VISIBLE
}) {
// @Throws(AuthFailureError::class)
override fun getParams(): Map<String, String> {
val params = HashMap<String, String>()
params["name"] = name
params["email"] = email
params["password"] = password
return params
}
}
val requestQueue = Volley.newRequestQueue(this)
requestQueue.add(stringRequest)
}`
解决方案
我认为这个错误是因为响应结果不是有效的 json 格式
com.android.volley.Response.Listener<String>() { response ->
try {
//at this point,you can debug result with console log your response
//because if response have a php error then json format not valid
Log.d("RESPONSE"," $response")
val jsonObject = JSONObject(response)
//at this you can check if json has have key name success
if (jsonObject.has("success")){
val success : String = jsonObject.getString("success")
if (success.equals("1")){
Toast.makeText(this, "Register Success ! Eyyvalll !", Toast.LENGTH_SHORT).show()
}
}
}
......
希望这有帮助
推荐阅读
- php - 根据另一个表的结果导入 mysql
- r - 如何解释没有显着系数的 VAR 模型?
- javascript - 在本地安装 ESLint 不起作用
- android - 使用自定义视图和 CursorAdapter 在 ListView 中单击的位置
- python - Discord.py 模块 Python 3.6.4 踢功能
- c# - 如何在IF c#中评估单个字符串
- css - 如何创建两个孩子来填充父母的身高?
- javascript - 在 reactjs 渲染函数内部检测到的问题的最佳处理
- jquery - jQuery css高度增加但不减少,Foundation,Flex
- javascript - 正则表达式在括号内捕获时忽略括号