I'm facing the problem that I get an empty JSON Object from the server.
How can I find out that JSON response from the server is an empty JSON Object {}
or others ? I set jObject!=null
condition for it but it doesn't work.
Here is my code:
JSONObject jObject = new JSONObject(response);
if (jObject!=null) {
Toast.makeText(getApplicationContext(), R.string.successfull_unsubscribe, Toast.LENGTH_LONG).show();
ShareData.saveData(getApplicationContext(), "simType", "null");
ShareData.saveData(getApplicationContext(), "firstLaunch", "true");
startActivity(new Intent(MainActivity2.this, WelcomeActivity.class));
finish();
}
Replace your if condition with if(.jObject.keys().hasNext())
You can try jObject.isNull("")
in your if condition
IsNull( ) method (JsonObject):
Returns a LOGICAL indicating if a property in a JsonObject contains the null value.
You can use jObject.keys().hasNext()
or jObject.length() != 0
:
JSONObject jObject = new JSONObject(response);
if (jObject != null && jObject.length() != 0) {
Toast.makeText(getApplicationContext(), R.string.successfull_unsubscribe, Toast.LENGTH_LONG).show();
ShareData.saveData(getApplicationContext(), "simType", "null");
ShareData.saveData(getApplicationContext(), "firstLaunch", "true");
startActivity(new Intent(MainActivity2.this, WelcomeActivity.class));
finish();
}
You can try length check or toString.
eg: jObject.length() == 0
eg: jObject.toString().equals("{}")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.