首页 > 解决方案 > 注册 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)

    }`

标签: androidkotlinandroid-volley

解决方案


我认为这个错误是因为响应结果不是有效的 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()
                }
            }
    }
    ......

希望这有帮助


推荐阅读