我已经开发了REST网络服务,并被android客户端使用。 该网络服务工作正常,但有时android volley遇到了com.android.volley.ServerError。如果我用邮递员检查网址,一切正常。该问题仅由少数几个用户引起,我无法重现该错误。
这是我的排球要求:
public void requestURL(final RestCallback callback) {
Log.d("JSON", url);
JsonArrayRequest jsonObjectRequest = new JsonArrayRequest
(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
callback.onSuccess(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("JSON", "onErrorResponse: " + url);
String errorText = null;
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
Log.d("Error", "Timeouterror");
errorText = context.getString(R.string.error_timeout);
} else if (error instanceof ServerError) {
Log.d("Error", "Servererror");
errorText = context.getString(R.string.error_server);
} else if (error instanceof NetworkError) {
Log.d("Error", "Servererror");
errorText = context.getString(R.string.error_network);
} else if (error instanceof ParseError) {
Log.d("Error", "Parseerror");
errorText = context.getString(R.string.error_parse);
}
Crashlytics.logException(new Throwable("Errornachricht: " + error.getMessage() + " Error: " + error.toString() + "\n URL: " + url + "\n Cause: " +error.getCause() + " Stacktrace: " + error.getStackTrace()));
callback.onError(errorText);
}
}) {
@Override
public Map<String, String> getHeaders() {
return Security.getAuth();
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return super.getParams();
}
};
RequestQueue requestQueue = Volley.newRequestQueue(context);
//sets time for retry connection
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy( 0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
jsonObjectRequest.setShouldCache(false);
//Adding request to the queue
requestQueue.add(jsonObjectRequest);
}
还有名为bei getHeaders的方法getAuth:
public static Map<String, String> getAuth() {
String credentials = "username" + ":" + "password";
String base64EncodedCredentials = Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
HashMap<String, String> headers = new HashMap<>();
headers.put("Accept","application/json");
headers.put("Content-Type","application/json");
headers.put("Authorization", "Basic " + base64EncodedCredentials);
return headers;
}
错误日志:
有人知道为什么我会收到此错误吗?
答案 0 :(得分:0)
我认为有时API响应不是有效的JsonArray,在那时它会引发服务器错误。 API响应是一个“空”或一个明智的jsonObject