How can I catch the error 500 and display a notification (Toast).

API

@POST("events/") Call<Event> createEvent(@Body Event event); 

Restclient

 public void createEvent(final String eventId) { Realm realm = Realm.getDefaultInstance(); Event event = realm.where(Event.class).equalTo("id", eventId).findFirst(); event = realm.copyFromRealm(event); realm.close(); if (event != null) { ArrayList<String> interest_ids = new ArrayList<>(); for (Interest interest: event.getInterests() ) { interest_ids.add(interest.getId()); } event.setInterestIds(interest_ids); happApi.createEvent(event).enqueue(new Callback<Event>() { @Override public void onResponse(Call<Event> call, Response<Event> response) { Log.d("HAPP_API", String.valueOf(response.code())); Log.d("HAPP_API", response.message()); if (response.isSuccessful()) { Event event = response.body(); Realm realm = Realm.getDefaultInstance(); realm.beginTransaction(); realm.copyToRealmOrUpdate(event); realm.commitTransaction(); realm.close(); Intent intent = new Intent(BroadcastIntents.EVENTEDIT_REQUEST_OK); intent.putExtra("event_id", event.getId()); LocalBroadcastManager.getInstance(App.getContext()).sendBroadcast(intent); } else { Intent intent = new Intent(BroadcastIntents.EVENTEDIT_REQUEST_FAIL); intent.putExtra("CODE", response.code()); intent.putExtra("MESSAGE", response.message()); LocalBroadcastManager.getInstance(App.getContext()).sendBroadcast(intent); } Realm realm = Realm.getDefaultInstance(); realm.beginTransaction(); Event oldEvent = realm.where(Event.class).equalTo("id", eventId).findFirst(); oldEvent.deleteFromRealm(); realm.commitTransaction(); realm.close(); } @Override public void onFailure(Call<Event> call, Throwable t) { Realm realm = Realm.getDefaultInstance(); realm.beginTransaction(); Event oldEvent = realm.where(Event.class).equalTo("id", eventId).findFirst(); oldEvent.deleteFromRealm(); realm.commitTransaction(); realm.close(); Intent intent = new Intent(BroadcastIntents.EVENTEDIT_REQUEST_FAIL); intent.putExtra("MESSAGE", t.getLocalizedMessage()); LocalBroadcastManager.getInstance(App.getContext()).sendBroadcast(intent); } }); } } 

Apiservice

 public static void createEvent(String eventId) { Intent intent = new Intent(App.getContext(), APIService.class); intent.setAction(ACTION_PATCH_EVENTCREATE); intent.putExtra(EXTRA_EE_ID, eventId); App.getContext().startService(intent); } 
  • if(errorCode == 500){Toast.makeText(context, "шота сламалась, туту ашыпка 500!!!111адын").show()} - Vladyslav Matviienko
  • one
    What exactly did you fail while you tried to solve this problem yourself? - Vladyslav Matviienko
  • @metalurgus I did not understand how to correct this problem correctly. Did not find sources. I decided to ask here, suddenly they would give a sensible solution here. And why do my questions always minus? - Satanist Devilov
  • 3
    Because you don’t even bother to describe what "error 500" is. Is this a Bluetooth connectivity error, or does this code have an OutOfMemory error? It turns out that your question is of extremely low quality, which is reflected in its rating. - Vladyslav Matviienko
  • Did you really show the code? Ok, now let's try to figure out what all the same fails. Do you know how to determine which code came back? - Vladyslav Matviienko

2 answers 2

 if(response.code() == 500) { Toast.makeText(context, "шота сламалась, туту ашыпка 500!!!111адын", Toast.LENGTH_LONG).show(); } 

If onResponse() executed not in the UI stream, then it is necessary to wrap the toast display, for example, in the Handler

     URL obj = new URL(url); HttpURLConnection connection = null; connection = (HttpURLConnection) obj.openConnection(); if (connection.getResponseCode() == 500){ //тут кидаете тост } 
    • But I have a function where the request is made APIService.createEvent(event.getId()); How can I check it? - Satanist Devilov
    • This line does not tell me anything! - Kirill Stoianov
    • You would still add some code to the question. - Kirill Stoianov
    • Well, right now, add. - Satanist Devilov
    • one
      I find it difficult to answer, because I am not familiar with the implementation of the classes that you use for queries. But I think you should pay attention to the line Log.d ("HAPP_API", String.valueOf (response.code ())); What does she give you back? - Kirill Stoianov