Android排球发送请求,发送JSONObject,获得字符串响应
问题描述:
我目前使用JSONObjectRequest从服务器获取数据。我发送的是JSON数据,但我期望得到一个字符串响应。 Volley返回一个错误(当然是一个空错误,所以我不知道发生了什么)。这是因为我使用了错误的请求吗?我的代码是:Android排球发送请求,发送JSONObject,获得字符串响应
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()); }
}
);
我的logcat包含以下数据:
所有你需要确保你的服务器是给你一个JsonObjectRequest
,而不是一个
JsonArrayRequest
的
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)
答
第一。
如果不是这种情况,可能需要验证您是否需要标头,或者如果您更改了Request.setRetryPolicy()
的超时时间。
希望这有助于
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
}
}
安置自己的logcat,以及任何错误日志服务器。 – Bonatti
你可以[检查这个答案](http://stackoverflow.com/a/33600251/4903925)并尝试一些不同的超时。你的主人在哪里?本地?互联网?内部?你可以使用其他资源/机器访问它吗?你能确保协商的MIME类型是正确的吗? – Bonatti
你没有得到任何回应.simply你的服务器或你的请求需要很多时间来执行,所以凌乱给你超时错误。它可能是你正在发送像图像或其他东西或由于网络连接问题或你的巨大数据服务器代码没有发送任何回应 –