-2
在使用訪問令牌在使用volley的用戶名和密碼註冊應用程序時出現此錯誤。獲得訪問令牌但在傳遞令牌後無法獲得響應。在android E/Volley中使用volley時出現此錯誤:[3355] BasicNetwork.performRequest:意外的響應代碼422
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);
}
如何克服這種服務器錯誤並獲得迴應
你的數據是沒有意義的服務器,如錯誤422的描述解釋說:「例如,可如果一個XML請求主體包含合式出現這種錯誤情況下(即,語法上正確),但語義錯誤,XML指令。「 消息來源:http://www.restpatterns.org/HTTP_Status_Codes/422_-_Unprocessable_Entity –