Skip to content

Instantly share code, notes, and snippets.

@Ankit-Slnk
Created March 15, 2023 11:57
Show Gist options
  • Save Ankit-Slnk/509777e7f6c40000bddefe154f8dd1c5 to your computer and use it in GitHub Desktop.
Save Ankit-Slnk/509777e7f6c40000bddefe154f8dd1c5 to your computer and use it in GitHub Desktop.
Laravel default error format show in android
public static void showError(Context context, ResponseBody responseBody) {
JSONObject jObjError = null;
try {
jObjError = new JSONObject(responseBody.string());
} catch (Exception e) {
}
try {
JSONObject data = jObjError.getJSONObject("errors");
Iterator<String> errorKeys = data.keys();
while (errorKeys.hasNext()) {
String key = errorKeys.next();
try {
if (data.get(key) instanceof JSONArray) {
JSONArray array = data.getJSONArray(key);
int size = array.length();
if (size > 0) {
Toast.makeText(context, array.getString(0), Toast.LENGTH_SHORT).show();
}
} else {
System.out.println(key + ":" + data.getString(key));
}
} catch (Throwable e) {
try {
System.out.println(key + ":" + data.getString(key));
} catch (Exception ee) {
}
}
}
} catch (Exception e) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment