I want to get data from php file after putting the value m (String) in JSONObject
and give result in TextView
ie mResultTextView
. However, this is giving me the following error.
com.android.volley.parseerror org.json.jsonexception value yes of type java.lang.String cannot be converted to JSONObject
var m = barcode.displayValue.toString()
val jsonobj = JSONObject()
jsonobj.put("email", m)
val url = "http://192.168.1.106/verf1.php"
val que = Volley.newRequestQueue(this@MainActivity)
val req = JsonObjectRequest(Request.Method.POST, url, jsonobj, Response.Listener {
response -> mResultTextView.text = response.toString()
},Response.ErrorListener {
response -> mResultTextView.text = response.toString()
})
que.add(req)
You need to set the content-type
to application/json
in the header for your POST request. For adding a custom header along with your POST request you might consider taking a look at this tutorial .
I am copying the code example from the tutorial from the link above.
class ServiceVolley : ServiceInterface {
val TAG = ServiceVolley::class.java.simpleName
val basePath = "https://your/backend/api/"
override fun post(path: String, params: JSONObject, completionHandler: (response: JSONObject?) -> Unit) {
val jsonObjReq = object : JsonObjectRequest(Method.POST, basePath + path, params,
Response.Listener<JSONObject> { response ->
Log.d(TAG, "/post request OK! Response: $response")
completionHandler(response)
},
Response.ErrorListener { error ->
VolleyLog.e(TAG, "/post request fail! Error: ${error.message}")
completionHandler(null)
}) {
@Throws(AuthFailureError::class)
override fun getHeaders(): Map<String, String> {
val headers = HashMap<String, String>()
headers.put("Content-Type", "application/json")
return headers
}
}
BackendVolley.instance?.addToRequestQueue(jsonObjReq, TAG)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.