[英]Android Volley Post Request, send JSONObject, get String Response
I'm currently using a JSONObjectRequest to get data from a server. 我目前正在使用JSONObjectRequest从服务器获取数据。 I'm sending JSON data, but I expect a string response.
我正在发送JSON数据,但是我期望一个字符串响应。 Volley returns an error (a null error of course, so I have no idea what's going on).
Volley返回一个错误(当然是空错误,所以我不知道发生了什么)。 Is this because I'm using the wrong request?
这是因为我使用了错误的请求吗? My code is:
我的代码是:
JsonObjectRequest jsonObjReq = new JsonObjectRequest(
Request.Method.POST,
postURL.toString(),
getJson(),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) { Log.d(TAG, response.toString()); }
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) { VolleyLog.d(TAG, "Error: " + error.getMessage()); Log.v(TAG, "Error: " + error.getCause()); }
}
);
My logcat contains the following data: 我的logcat包含以下数据:
07-27 11:45:57.218 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo+,hn 21(0x6670742e6d6963),sn(),family 0,flags 4
07-27 11:45:57.228 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo-,err=8
07-27 11:45:57.228 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo+,hn 21(0x6670742e6d6963),sn(),family 0,flags 1024
07-27 11:45:57.228 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo-, 1
07-27 11:45:57.228 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo_proxy+
07-27 11:45:57.228 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo_proxy-, success
07-27 11:46:02.258 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo+,hn 21(0x6670742e6d6963),sn(),family 0,flags 4
07-27 11:46:02.258 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo-,err=8
07-27 11:46:02.258 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo+,hn 21(0x6670742e6d6963),sn(),family 0,flags 1024
07-27 11:46:02.258 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo-, 1
07-27 11:46:02.258 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo_proxy+
07-27 11:46:02.288 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo_proxy-, success
07-27 11:46:12.368 11309-11309/com.microsoft.quiztest V/DeviceForensics: Error: null
07-27 11:46:12.368 11309-11309/com.microsoft.quiztest W/System.err: com.android.volley.TimeoutError
07-27 11:46:12.368 11309-11309/com.microsoft.quiztest W/System.err: at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:141)
07-27 11:46:12.368 11309-11309/com.microsoft.quiztest W/System.err: at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
First of all you need to be sure that your server is giving you a JsonObjectRequest
and not a JsonArrayRequest
. 首先,您需要确保服务器给您的是
JsonObjectRequest
而不是JsonArrayRequest
。
If is not the case maybe verify that you do not need a header, or maybe if you change the timeout duration with Request.setRetryPolicy()
. 如果不是这种情况,则可以验证您是否不需要标题,或者使用
Request.setRetryPolicy()
更改超时时间。
Hope this helps, 希望这可以帮助,
mRequestQueue = Volley.newRequestQueue(context);
final String url = "url";
final ProgressDialog pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading data...");
pDialog.show();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
url, params,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("TAGOnResponse", response.toString());
pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
String json = null;
NetworkResponse response = error.networkResponse;
if(response != null && response.data != null){
switch(response.statusCode){
case 400:
json = new String(response.data);
json = trimMessage(json, "message");
if(json != null) displayMessage(json);
Log.i("ResponseJSON", json);
break;
case 401:
json = new String(response.data);
json = trimMessage(json.toString(), "message");
if(json != null) displayMessage(json);
break;
case 500:
json = new String(response.data);
json = trimMessage(json, "message");
if(json != null) displayMessage(json);
break;
}
}
VolleyLog.d("TAGOnError " + error.getMessage(), "Error: " + error.getMessage());
pDialog.hide();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("X-Usuario-Email", email);
headers.put("X-Usuario-Token", token);
return headers;
}
};
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
mRequestQueue.add(jsonObjReq);
mRequestQueue.addRequestFinishedListener(this);
}
@Override
public void onRequestFinished(Request<Object> request) {
if (request.getUrl().contains("Something")) {
// Success asynchronous call
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.