首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Volley库使服务器没有响应

Volley库使服务器没有响应
EN

Stack Overflow用户
提问于 2018-02-12 16:50:49
回答 3查看 194关注 0票数 2

我是volley的新手,我已经在我的应用程序中添加了volley库,当我向它请求时,它将在1到2天内完成工作,在应用程序停止并给出volley服务器没有响应错误之后。我已经检查了许多关于它的链接,但问题仍然得到了解决。以下是我的截击请求代码:

代码语言:javascript
运行
复制
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请求来发送数据。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-02-13 20:49:04

我认为问题出在你的volley库的缓存中,这样做可能会有帮助。

代码语言:javascript
运行
复制
        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);
票数 0
EN

Stack Overflow用户

发布于 2018-02-12 18:17:10

您需要像这样修改请求参数key。

代码语言:javascript
运行
复制
@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
EN

Stack Overflow用户

发布于 2018-02-12 18:26:44

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48742511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档