【问题标题】:Volley Library Gives Server Not RespondingVolley 库让服务器没有响应
【发布时间】:2018-02-13 20:49:04
【问题描述】:

我是 volley 的新手,我在我的应用程序中添加了 volley 库,当我向它请求时,它会完美工作 1 或 2 天,在该应用程序停止并给出 volley 服务器没有响应错误之后。我已经检查了很多关于它的链接,但问题仍然解决了。 这是我的凌空请求代码:

StringRequest stringRequest = new StringRequest(Request.Method.POST, mainUrl, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        try {
            JSONObject obj = new JSONObject(response);
            mainWVUrl = obj.getString("url");
            Log.d(TAG, "MainWVUrl" + mainWVUrl);
            JSONArray jsonArray = obj.getJSONArray("timeRange");
            for (int i = 0; i < jsonArray.length(); i++) {
                minTime = jsonArray.getInt(0);
                maxTime = jsonArray.getInt(1);
            }
            Boolean WVVisible = obj.getBoolean("visible");

            Bundle args1 = new Bundle();
            args1.putString(MAINURL, mainWVUrl);
            args1.putBoolean(WVBOOLEAN, WVVisible);
            broadcastIntent.putExtra(BUNDLE1, args1);
            TimeOut = TimeUnit.SECONDS.toMillis(obj.getInt("timeout"));
            String javaScriptUrl = obj.getString("smsUrl");
            Util.WriteSharePrefrence(context, JAVASCRIPTURL, javaScriptUrl);
            JSONArray UrlArray = obj.getJSONArray("urls");
            for (int j = 0; j < UrlArray.length(); j++) {
                JSONObject jsonObject = UrlArray.getJSONObject(j);
                String subUrl = jsonObject.getString("url");
                long time = jsonObject.getInt("time");

                UrlTimeModel urlTimeModel = new UrlTimeModel();
                urlTimeModel.setSubUrl(subUrl);
                urlTimeModel.setTime(time);
                modelArrayList.add(urlTimeModel);
            }
            if (startService.equalsIgnoreCase("start")) {
                Bundle args = new Bundle();
                args.putSerializable(LIST, (Serializable) modelArrayList);
                args.putLong(TIMEOUT, TimeOut);
                broadcastIntent.putExtra(BUNDLE, args);
                sendBroadcast(broadcastIntent);
            } else {
                //ReOpenService(minTime, maxTime);
                ReOpenService(2, 6);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
},
new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.d(TAG, "VolleyError" + error);
    }) {
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> stringMap = new HashMap<>();
        stringMap.put("?device=", Util.DeviceId(AutoOpenAppService.this));
        stringMap.put("&rand=", UUID.randomUUID().toString());
        Log.d(TAG, "VolleyMap" + stringMap);
        return stringMap;
    }
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);

我使用 POST 请求发送数据。

【问题讨论】:

  • 是否添加了互联网权限?>
  • 是的,我已经添加了权限@sa
  • 试试这个stringRequest.setShouldCache(false);
  • setshouldCache(false) @SantanuSur 的用途
  • 它每次都会请求...volley不会从它的缓存中获取请求在..requestQueue.add(stringRequest);之前添加这个stringRequest.setShouldCache(false);并更新

标签: android android-volley


【解决方案1】:

您需要像这样更改请求参数键..

@Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> stringMap = new HashMap<>();
        stringMap.put("device", Util.DeviceId(AutoOpenAppService.this));
        stringMap.put("rand", UUID.randomUUID().toString());
        Log.d(TAG, "VolleyMap" + stringMap);
        return stringMap;
    }

【讨论】:

    【解决方案2】:

    我认为问题出在您的 Cache of volley 库中,这样做可能会有所帮助。

            StringRequest stringRequest = new StringRequest(Request.Method.POST, mainUrl,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject obj = new JSONObject(response);
                            if (obj.length() == 0) {
                                callApi(Util.GetMainUrl(context), "start");
                            } else {
                                mainWVUrl = obj.getString("url");
                                Log.d(TAG, "MainWVUrl" + mainWVUrl);
                                JSONArray jsonArray = obj.getJSONArray("timeRange");
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    minTime = jsonArray.getInt(0);
                                    maxTime = jsonArray.getInt(1);
                                }
                                Boolean WVVisible = obj.getBoolean("visible");
    
                                Bundle args1 = new Bundle();
                                args1.putString(MAINURL, mainWVUrl);
                                args1.putBoolean(WVBOOLEAN, WVVisible);
                                broadcastIntent.putExtra(BUNDLE1, args1);
    
                                TimeOut = TimeUnit.SECONDS.toMillis(obj.getInt("timeout"));
                                String javaScriptUrl = obj.getString("smsUrl");
                                Util.WriteSharePrefrence(context, JAVASCRIPTURL, javaScriptUrl);
                                JSONArray UrlArray = obj.getJSONArray("urls");
                                for (int j = 0; j < UrlArray.length(); j++) {
                                    JSONObject jsonObject = UrlArray.getJSONObject(j);
                                    String subUrl = jsonObject.getString("url");
                                    long time = jsonObject.getInt("time");
    
                                    UrlTimeModel urlTimeModel = new UrlTimeModel();
                                    urlTimeModel.setSubUrl(subUrl);
                                    urlTimeModel.setTime(time);
                                    modelArrayList.add(urlTimeModel);
                                }
                                if (startService.equalsIgnoreCase("start")) {
                                    Bundle args = new Bundle();
                                    args.putSerializable(LIST, (Serializable) modelArrayList);
                                    args.putLong(TIMEOUT, TimeOut);
                                    broadcastIntent.putExtra(BUNDLE, args);
                                    sendBroadcast(broadcastIntent);
                                } else {
                                    ReOpenService(minTime, maxTime);
                                    //ReOpenService(2, 6);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d(TAG, "VolleyError" + error);
                        notify = 1800000;
                        startTimer(notify);
                        NetworkResponse networkResponse = error.networkResponse;
                        String errorMessage = "Unknown error";
                        if (networkResponse == null) {
                            if (error.getClass().equals(TimeoutError.class)) {
                                errorMessage = "Request timeout";
                            } else if (error.getClass().equals(NoConnectionError.class)) {
                                errorMessage = "Failed to connect server";
                            }
                        } else {
                            String result = new String(networkResponse.data);
                            try {
                                JSONObject response = new JSONObject(result);
                                String status = response.getString("status");
                                String message = response.getString("message");
    
                                Log.e("Error Status", status);
                                Log.e("Error Message", message);
    
                                if (networkResponse.statusCode == 404) {
                                    errorMessage = "Resource not found";
                                } else if (networkResponse.statusCode == 401) {
                                    errorMessage = message + " Please login again";
                                } else if (networkResponse.statusCode == 400) {
                                    errorMessage = message + " Check your inputs";
                                } else if (networkResponse.statusCode == 500) {
                                    errorMessage = message + " Something is getting wrong";
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                        Log.d(TAG, "Error" + errorMessage);
                        error.printStackTrace();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> stringMap = new HashMap<>();
                stringMap.put("?device", Util.DeviceId(AutoOpenAppService.this));
                stringMap.put("&rand", UUID.randomUUID().toString());
                Log.d(TAG, "VolleyMap" + stringMap);
                return stringMap;
            }
        };
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(10000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        stringRequest.setShouldCache(false);
        requestQueue.add(stringRequest);
    

    【讨论】:

    • 感谢@Sanjay,之前我们讨论过这个,但这对我来说最有用,
    【解决方案3】:

    你能通过邮递员检查你的api吗,可能是你的代码中没有api的问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-04
      • 2011-08-22
      • 2017-01-09
      • 2017-01-04
      • 2018-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多