为什么 Volley 响应被接收为“[”而不是 JSON
Why Volley response is received as "[" and not JSON
我正在了解 Volley,我不知道为什么 GET 方法的响应会以单个字符的形式出现 -> [.
我正在使用此方法获取 JSON 响应:
public void getJsonMethod() {
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(context);
// String url = "https://www.w3schools.com/js/myTutorials.txt";
String url = "http://www.google.com"; // with this url I am getting response
// Request a string response from the provided URL.
final StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println("Response is: " + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("Response is not good" + error.getMessage());
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}
当我使用 this link I do get a response but when I try to use some link that contains nothing but JSON like this one 时,我的回答是“[”.
我从 Activity 调用这个方法是这样的:
GetJsonClass getJson = new GetJsonClass(this);
getJson.getJsonMethod();
关于我在这里做错了什么有什么想法吗?
答案+代码
如果有人开始使用 Volley,也许这对他有帮助:
正如 David Lacroix 在他的回答中所说,我打电话给 stringRequest
而不是 JsonArrayRequest
。
应该是这样的:
public void getJsonMethod() {
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(context);
String url = "your url";
JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
System.out.println("this is response good" + response);
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("this is response bad" + error);
}
});
queue.add(jsonObjectRequest);
}
myTutorials.txt 正在提供状态代码 304
(也没有正确的后缀和 MIME 类型):
304 Not Modified. If the client has performed a conditional GET request and access is allowed, but the document has not been modified, the server SHOULD respond with this status code. The 304 response MUST NOT contain a message-body, and thus is always terminated by the first empty line after the header fields.
换句话说,浏览器可能显示的内容不一定与服务器发送的内容相同。例如。 GSON
会接受 JSON
仅在启用宽松选项的情况下,因为数组没有名称。
参见 RFC 2616。
见https://developer.android.com/training/volley/request
StringRequest. Specify a URL and receive a raw string in response. See Setting Up a Request Queue for an example.
JsonObjectRequest and JsonArrayRequest (both subclasses of JsonRequest). Specify a URL and get a JSON object or array (respectively) in response.
您应该使用 JsonArrayRequest
我正在了解 Volley,我不知道为什么 GET 方法的响应会以单个字符的形式出现 -> [.
我正在使用此方法获取 JSON 响应:
public void getJsonMethod() {
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(context);
// String url = "https://www.w3schools.com/js/myTutorials.txt";
String url = "http://www.google.com"; // with this url I am getting response
// Request a string response from the provided URL.
final StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println("Response is: " + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("Response is not good" + error.getMessage());
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}
当我使用 this link I do get a response but when I try to use some link that contains nothing but JSON like this one 时,我的回答是“[”.
我从 Activity 调用这个方法是这样的:
GetJsonClass getJson = new GetJsonClass(this);
getJson.getJsonMethod();
关于我在这里做错了什么有什么想法吗?
答案+代码
如果有人开始使用 Volley,也许这对他有帮助:
正如 David Lacroix 在他的回答中所说,我打电话给 stringRequest
而不是 JsonArrayRequest
。
应该是这样的:
public void getJsonMethod() {
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(context);
String url = "your url";
JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
System.out.println("this is response good" + response);
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("this is response bad" + error);
}
});
queue.add(jsonObjectRequest);
}
myTutorials.txt 正在提供状态代码 304
(也没有正确的后缀和 MIME 类型):
304 Not Modified. If the client has performed a conditional GET request and access is allowed, but the document has not been modified, the server SHOULD respond with this status code. The 304 response MUST NOT contain a message-body, and thus is always terminated by the first empty line after the header fields.
换句话说,浏览器可能显示的内容不一定与服务器发送的内容相同。例如。 GSON
会接受 JSON
仅在启用宽松选项的情况下,因为数组没有名称。
参见 RFC 2616。
见https://developer.android.com/training/volley/request
StringRequest. Specify a URL and receive a raw string in response. See Setting Up a Request Queue for an example. JsonObjectRequest and JsonArrayRequest (both subclasses of JsonRequest). Specify a URL and get a JSON object or array (respectively) in response.
您应该使用 JsonArrayRequest