Ошибка сервера залпа - PullRequest
       9

Ошибка сервера залпа

0 голосов
/ 13 декабря 2018
  1. com.android.volley.ServerError
  2. Я использовал 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());
        }
    

1 Ответ

0 голосов
/ 13 декабря 2018

Поскольку вы получаете ответ HTML или произошла ошибка, а не массив JSON, необходимо проверить серверную часть.

...