使用访问令牌使用volley在用户名和密码的应用中注册时出现此错误。获得访问令牌但在传递令牌后无法获得响应。
private void getRegisterToken() {
final String grant_type = ApiConstants.CLIENT_GRANT_TYPE;
final String client_id = ApiConstants.CLIENT_ID;
final String client_secret = ApiConstants.CLIENT_SECRET;
StringRequest stringRequest1 = new StringRequest(Request.Method.POST, API_BASE_URL_TOKEN,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
VolleyLog.e("Response:%n %s", response);
if (pDialog != null)
pDialog.dismiss();
try {
if (UtilValidate.isNotNull(response)) {
JSONObject responseJSONObject = new JSONObject(response);
if (responseJSONObject.has(ApiConstants.ACCESS_TOKEN)) {
String access_token = Utils.getJSONString(responseJSONObject, ApiConstants.ACCESS_TOKEN);
registerDetails(access_token);
// getUserDetails(access_token);
}
}
} catch (JSONException ignored) {
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
NetworkResponse networkResponse = error.networkResponse;
if (UtilValidate.isNotNull(networkResponse)) {
if (pDialog != null)
pDialog.dismiss();
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
finish();
} else {
if (pDialog != null)
pDialog.dismiss();
Snackbar.make(login_linear_register, getString(R.string.chk_credentials), Snackbar.LENGTH_LONG).show();
}
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
/* params.put(KEY_USERNAME, "");
params.put(KEY_EMAIL, inputusername);
params.put(KEY_PASSWORD, inputpassword);
*/
params.put(KEY_GRANT_TYPE, grant_type);
params.put(KEY_CLIENT_ID, client_id);
params.put(KEY_CLIENT_SECRET, client_secret);
return params;
}
};
TwistedAppController.getInstance().addToRequestQueue(stringRequest1);
}
//Get Details of logged user
private void registerDetails(final String access_token) {
// final String grant_type = ApiConstants.CLIENT_GRANT_TYPE;
// final String client_id = ApiConstants.CLIENT_ID;
// final String client_secret = ApiConstants.CLIENT_SECRET;
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_USER,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
VolleyLog.e("Response:%n %s", response);
if (pDialog != null)
pDialog.dismiss();
try {
if (UtilValidate.isNotNull(response)) {
if (pDialog != null)
pDialog.dismiss();
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
} catch (Exception ignored) {
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
NetworkResponse networkResponse = error.networkResponse;
if (UtilValidate.isNotNull(networkResponse)) {
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
finish();
} else {
if (pDialog != null)
pDialog.dismiss();
Snackbar.make(login_linear_register, getString(R.string.chk_credentials), Snackbar.LENGTH_LONG).show();
}
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put(KEY_USERNAME, "");
params.put(KEY_EMAIL, username.getText().toString().trim());
params.put(KEY_PASSWORD, password.getText().toString().trim());
// params.put(KEY_GRANT_TYPE, grant_type);
// params.put(KEY_CLIENT_ID, client_id);
// params.put(KEY_CLIENT_SECRET, client_secret);
return params;
}
@Override
public Map getHeaders() throws AuthFailureError {
Map headers = new HashMap();
headers.put("Accept", "application/json");
headers.put("Authorization", "Bearer" + " " + access_token);
return headers;
}
};
TwistedAppController.getInstance().addToRequestQueue(stringRequest, tag_json_obj);
}
如何克服此服务器错误并获得响应
答案 0 :(得分:0)
得到了同样的错误。经过长时间的搜索我发现了这个 在代码中添加setRetryPolicy
RequestQueue requestQueue = Volley.newRequestQueue(this);
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(jsonObjReq);