如何从Volley中的JSONObject获取字符串响应 [英] How to get String response from JSONObject in Volley
问题描述
我已经通过齐射创建了JSONRequest,它成功命中了服务,检查了服务端,它接收了数据,并发送成功"作为回报.
I have created JSONRequest by using volley, It successfully hits the service, I checked the service end, It receives the data, and send "Success" in return.
问题在于,Service在输出中返回String
,而Volley在输出中除了某些JSON Data
.因此,它执行onError
方法而不是onResponse
.
The problem is that, Service returns String
in output, and Volley excepts some JSON Data
in output. So it executes the onError
Method, instead of onResponse
.
请指导我如何使它接受字符串响应,或者当您将JSONObject
作为请求使用时,这是不可能的.
Kindly guide me how to make it accept string response, or is it not possible when you are using JSONObject
as request.
Request<JSONObject> jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, "http://192.168.0.101:8888/api/services/mytest",
jsonParent, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("Success", response.toString());
deleteFile();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error", error.toString());
deleteFile();
}
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(jsonObjectRequest);
推荐答案
您可以使用StringRequest代替JSONRequest.
You can use StringRequest instead JSONRequest.
StringRequest stringRequest = new StringRequest(methodType, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers == null ? super.getHeaders() : headers;
}
@Override
public byte[] getBody() throws AuthFailureError {
return "Your JSON body".toString().getBytes();
}
};
getheaders
方法是根据需要添加自定义标头,而getBody
提供request body
.
getheaders
method is to add custom headers if you want and getBody
to supply request body
.
这篇关于如何从Volley中的JSONObject获取字符串响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!