我已经使用volley创建了JSONRequest,它成功点击了服务,我检查了服务端,它接收了数据,然后发送"Success"作为回报.
问题在于,服务String
在输出中返回,而Volley JSON Data
在输出中除外.所以它执行onError
Method而不是onResponse
.
请指导我如何使其接受字符串响应,或者在您JSONObject
作为请求使用时不可能.
RequestjsonObjectRequest = new JsonObjectRequest(Request.Method.POST, "http://192.168.0.101:8888/api/services/mytest", jsonParent, new Response.Listener () { @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);
Nayan Srivas.. 11
您可以使用StringRequest而不是JSONRequest.
StringRequest stringRequest = new StringRequest(methodType, url, new Response.Listener() { @Override public void onResponse(String response) { } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }){ @Override public Map 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
.
您可以使用StringRequest而不是JSONRequest.
StringRequest stringRequest = new StringRequest(methodType, url, new Response.Listener() { @Override public void onResponse(String response) { } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }){ @Override public Map 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
.