Created
March 15, 2023 11:57
-
-
Save Ankit-Slnk/509777e7f6c40000bddefe154f8dd1c5 to your computer and use it in GitHub Desktop.
Laravel default error format show in android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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