根据我的知识,服务器只能以XML或JSON格式响应,这是真的吗?如果是,那么Volley的StringRequest()
是如何工作的?
XML和JSON是格式化字符串。http
请求将始终返回一个字符串。如果您正在设计请求系统,您应该知道该字符串(特别是响应的主体(中所期望的格式,并且由您对其进行解码
val stringRequest = object : StringRequest(
Request.Method.POST, Config."Url",
Response.Listener<String> { response ->
try {
val obj = JSONObject(response)
if (!obj.getBoolean("error")) {
val userssListArray = obj.getJSONArray("arrayanme")
for (i in 0 until userssListArray.length()) {
var usersObj = userssListArray.getJSONObject(i)
}
} else {
if (obj.getString("message") == "Your login expired please re login") {
val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
finish()
}
}
} catch (e: JSONException) {
e.printStackTrace()
}
},
Response.ErrorListener {
Toast.makeText(this, "Network Error Try Again...", Toast.LENGTH_LONG).show()
}) {
@Throws(AuthFailureError::class)
override fun getParams(): Map<String, String> {
val params = HashMap<String, String>()
return params
}
}
stringRequest.retryPolicy = DefaultRetryPolicy(
15000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)
//adding request to queue
VolleySingleton.instance?.addToRequestQueue(stringRequest)
这就是截击字符串请求的工作原理