Мне нужно обновить данные с помощью запроса PATCH, используя библиотеку Volley. Я применяю запрос GET для получения значений формы в каждой форме. Я пытаюсь реализовать метод PATCH для обновления значений формы, но он выдает эту ошибку " E / Volley: [99785] BasicNetwork.performRequest: Неожиданный код ответа 422 для http://api-eriqab.cocalms.com/api/v1/updateClass/33" и " E / VOLLEY: null ". Как реализовать запрос PATCH и устранить эту ошибку, используя библиотеку Volley.
Требование API
Метод API
private void UpdateFormData() {
classEdit.setPlace(place.getText().toString());
classEdit.setDate(choosedate.getText().toString());
classEdit.setTime(chooseTime.getText().toString());
classEdit.setTitle(title.getText().toString());
classEdit.setItem(item.getText().toString());
classEdit.setOutcome(outcome.getText().toString());
Log.i("UPDATE", "location: " + classEdit.getLocation());
Log.i("UPDATE", "place: " + classEdit.getPlace());
Log.i("UPDATE", "date: " + classEdit.getDate());
Log.i("UPDATE", "time: " + classEdit.getTime());
Log.i("UPDATE", "subject_id: " + classEdit.getSubject_id());
Log.i("UPDATE", "title: " + classEdit.getTitle());
Log.i("UPDATE", "item: " + classEdit.getItem());
Log.i("UPDATE", "outcome: " + classEdit.getOutcome());
String URL = RestAPI.UpdateClass + classId;
StringRequest request = new StringRequest(Request.Method.PATCH, URL, detailUpdateListener, errorUpdateListener) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json; charset=utf-8");
params.put("Authorization", "Bearer " + storeToken);
return params;
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("location", classEdit.getLocation());
params.put("place", classEdit.getPlace());
params.put("date", classEdit.getDate());
params.put("time", classEdit.getTime());
params.put("subject_id", String.valueOf(classEdit.getSubject_id()));
params.put("title", classEdit.getSubject_name());
params.put("item", classEdit.getTitle());
params.put("outcome", classEdit.getOutcome());
return params;
}
};
VolleySingleton.getInstance(this).addToRequestQueue(request);
}
public Response.Listener<String> detailUpdateListener = response ->
{
Log.i("VOLLEY", "response" + response);
try {
JSONObject responseObj = new JSONObject(response);
String array = responseObj.optString("data");
String msg = responseObj.optString("message");
if(msg.equalsIgnoreCase("success")) {
Toast.makeText(getApplicationContext(), "Successfully Submitted ", Toast.LENGTH_LONG).show();
Intent intent = new Intent(ClassEdit.this, ClassRatingEdit.class);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "Failed to Submit", Toast.LENGTH_LONG).show();
}
} catch (JSONException ex) {
ex.printStackTrace();
}
};
public Response.ErrorListener errorUpdateListener = error -> {
Log.e("VOLLEY", "" + error.getMessage());
Toast.makeText(this, String.valueOf(error.getMessage()), Toast.LENGTH_LONG).show();
};
Код счетчика для местоположения
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,R.layout.support_simple_spinner_dropdown_item, locationList);
adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
location.setAdapter(adapter);
location.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String locate = ClassEdit.this.location.getItemAtPosition(ClassEdit.this.location.getSelectedItemPosition()).toString();
classEdit.setLocation(locate);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Код счетчика для субъекта
LoadSubjectData();
subject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String subject = ClassEdit.this.subject.getItemAtPosition(ClassEdit.this.subject.getSelectedItemPosition()).toString();
subjectModel = subjects.get(position);
classEdit.setSubject_id(subjectModel.getId());
Toast.makeText(ClassEdit.this, "Selected item is "+ subjectModel.getId(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Вызов метода
sahkan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UpdateFormData();
}
});