我一直读到数据来自 json 格式的服务器,当我们想发送一些我们以 json 格式发送到服务器的数据时,数据以 json 格式传输,那么字符串请求来自哪里?我不知道我们是否也可以以字符串格式发布和获取数据,使用字符串和 json 请求有什么区别和用例?
谢谢!
最佳答案
StringRequest class will be used to fetch any kind of string data. The response can be 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) {
}
});
If you are expecting json object in the response, you should use 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) {
}
});
关于android - volley中的JSONRequest和StringRequest有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45543544/