【发布时间】:2018-12-13 09:58:52
【问题描述】:
- com.android.volley.ServerError
-
我使用 Volley
JSONArray请求它直接接收错误响应RequestQueue queue = Volley.newRequestQueue(getBaseContext()); try { String url = getResources().getString(R.string.BASE_URL_SHARE) + "bank-nifty-top.txt"; Log.d(TAG, "URL : " + url); JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, url, null, new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { Log.d(TAG, response.toString()); try { Log.d(TAG, "data recieved Shares: " + response.toString()); if (response != null) { showMarketRates(response); Toast.makeText(getApplicationContext(), "request" + response.toString(), Toast.LENGTH_SHORT).show(); } } catch (Exception e) { Log.d(TAG, "Volley" + e.toString()); Log.d(TAG, "Unable to contact server. please try again"); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { checkInternet(); Log.d(TAG, "Error: " + error.toString()); NetworkResponse response = error.networkResponse; if ((error instanceof ServerError) && response != null) { try { String res = new String(response.data, HttpHeaderParser.parseCharset(response.headers, "utf-8")); Log.d(TAG, "Error: " + res); if (res != null) { JSONObject obj = new JSONObject(res); if (obj != null) { Log.d(TAG, "Err Desc: " + obj.getString("error_description")); return; } return; } Log.d(TAG, "Unable to contact server. please try again"); } catch (UnsupportedEncodingException e1) { Log.d(TAG, e1.toString()); } catch (JSONException e2) { Log.d(TAG, e2.toString()); } } } }) { }; jsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, 2, 2.0f)); queue.add(jsonArrayRequest); } catch (Exception e) { Log.d(TAG, e.toString()); }
【问题讨论】:
-
你能添加你给你这个错误的代码吗?你能再解释一下吗?
-
服务器响应有问题。您收到非 JSONArray 格式的响应。
-
你的服务器响应不是 JSON
-
在邮递员中显示 JSON Array ,使用 Retrofit 很好,但 Volley 显示此错误,我需要 Volley
-
解决方法是将 URL 中的 ("https://" 改为 "http://") 或 ("http://'' 改为 "https://")跨度>
标签: android api android-volley