我一直在阅读json格式的服务器数据,当我们想发送一些数据发送到服务器以json格式,所以数据以json格式传输,然后字符串请求来自?我不知道我们是否可以发布并获取字符串格式的数据,以及使用字符串和json请求的区别和用例是什么?JSONRequest和StringRequest之间的区别是什么
谢谢!
我一直在阅读json格式的服务器数据,当我们想发送一些数据发送到服务器以json格式,所以数据以json格式传输,然后字符串请求来自?我不知道我们是否可以发布并获取字符串格式的数据,以及使用字符串和json请求的区别和用例是什么?JSONRequest和StringRequest之间的区别是什么
谢谢!
StringRequest类将用于提取任何类型的字符串数据。 响应可以是json,xml,html,text。
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
如果您在响应预期JSON对象,你应该使用 JsonObjectRequest。
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
URL, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
这是关于请求的返回类型,StringRequest
处理一个String
像error = false
和JSONObjectRequest
响应处理一个JSONObject
响应像{"error" : false}
,如何知道它的JSONObject
?使用括号({
)。
https://stackoverflow.com/questions/32420158/jsonrequest-vs-stringrequest-in-android-volley –
遗憾的是它并没有帮助 – blackHawk
JSONRequest返回结果为一个JSONObject。 StringRequest返回结果作为一个字符串(一个字符串的JSON结构) –