Volley Library使服务器无响应

时间:2018-02-12 08:50:49

标签: android android-volley

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

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请求发送数据。

3 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分:0)

你可以通过邮递员检查你的api,可能是api中有一些问题不在你的代码中

答案 2 :(得分:0)

我认为问题出现在你的抽屉库中,这可能是有帮助的。

        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);