使用volley android获得502错误的网关错误

时间:2018-01-23 07:55:26

标签: android oauth-2.0 android-volley

我正在开展一个项目。我正在使用oauth2Volley库。我有一个登录网址。首先,我向网址发出请求,以获取access_tokenrefresh_tokenexpireIn值。使用access_token我正在登录网址。我能够第一时间成功登录。但是如果令牌过期了。我收到以下错误。我做错了什么?

502 Bad Gateway error

这是我的截击请求:

StringRequest request = new StringRequest(Request.Method.GET, GAConstants.READUSERPROFILE_DEV_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                try {
                    MyApplication.logger.log("ReadUserProfileRequestUrl :" + GAConstants.READUSERPROFILE_DEV_URL + "ReadUserProfileError :" + new String(error.networkResponse.data,"UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                dialog.dismiss();
                NetworkResponse networkResponse = error.networkResponse;
                if (error instanceof TimeoutError) {
                    ConsumerAppUtils.showErrorAlertDialog(LoginActivity.this, getResources().getString(R.string.api_error));
                    Log.e("LoginActivity", "timeOuterror");
                } else if (error instanceof ServerError) {
                    if (networkResponse != null && networkResponse.statusCode == HttpStatus.SC_UNAUTHORIZED) {
                        //Token expired
                        ConsumerAppUtils.showTokenErrorAlertDialog(LoginActivity.this, getResources().getString(R.string.accesstoken_alert));
                    } else {
                        if (networkResponse != null)
                            Log.e("LoginActivity", networkResponse.statusCode + "");
                        //Other network error
//                        ConsumerAppUtils.showErrorAlertDialog(LoginActivity.this, error.getMessage());
                        if(error!=null || error.networkResponse !=null) {
                            String body = null;
                            try {
                                body = new String(error.networkResponse.data,"UTF-8");
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                            ConsumerAppUtils.showErrorAlertDialog(LoginActivity.this, body);
                        }
                    }
                    Log.e("LoginActivity", "serverError");
                } else if (error instanceof AuthFailureError) {
                    if (networkResponse != null && networkResponse.statusCode == HttpStatus.SC_UNAUTHORIZED) {
                        //Token expired
                        ConsumerAppUtils.showTokenErrorAlertDialog(LoginActivity.this, getResources().getString(R.string.accesstoken_alert));
                    } else {
                        if(error!=null || error.networkResponse !=null) {
                            String body = null;
                            try {
                                body = new String(error.networkResponse.data,"UTF-8");
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                            ConsumerAppUtils.showErrorAlertDialog(LoginActivity.this, body);
                        }
                        Log.e("LoginActivity", "authFailure");
                    }
                }
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String token = null;
                if (accessToken != null) {
                    token = accessToken;
                    Log.e("HomeScreenActivity", token);
                } else {
                    //No access token
                    ConsumerAppUtils.showTokenErrorAlertDialog(LoginActivity.this, getResources().getString(R.string.accesstoken_alert));
                }
                Map<String, String> params = new HashMap<>();
                params.put("Content-Type", "application/json");
                params.put(OAuthConstants.AUTHORIZATION,
                        OAuthConstants.BEARER + " " + token);
                return params;
            }
        };
        request.setRetryPolicy(new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        Volley.newRequestQueue(this, hurlStack).add(request);

1 个答案:

答案 0 :(得分:0)

引用维基百科502 Bad Gateway的定义:“服务器充当网关或代理,并从上游服务器收到无效响应。”还有许多其他5xx服务器错误消息,所有这些消息归结为:“服务器无法满足明显有效的请求。

然后尝试一下。 - &GT; Volley with https URLS