fix json_decode problems

This commit is contained in:
Mike Schwörer 2018-11-12 14:24:11 +01:00
parent 3d29fecaec
commit 36b9263730
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
3 changed files with 60 additions and 37 deletions

View File

@ -4,7 +4,9 @@ import android.util.Log;
import android.view.View; import android.view.View;
import com.blackforestbytes.simplecloudnotifier.SCNApp; import com.blackforestbytes.simplecloudnotifier.SCNApp;
import com.blackforestbytes.simplecloudnotifier.lib.string.Str;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONTokener; import org.json.JSONTokener;
@ -57,18 +59,18 @@ public class ServerCommunication
JSONObject json = (JSONObject) new JSONTokener(r).nextValue(); JSONObject json = (JSONObject) new JSONTokener(r).nextValue();
if (!json.getBoolean("success")) if (!json_bool(json, "success"))
{ {
SCNApp.showToast(json.getString("message"), 4000); SCNApp.showToast(json_str(json, "message"), 4000);
return; return;
} }
SCNSettings.inst().user_id = json.getInt("user_id"); SCNSettings.inst().user_id = json_int(json, "user_id");
SCNSettings.inst().user_key = json.getString("user_key"); SCNSettings.inst().user_key = json_str(json, "user_key");
SCNSettings.inst().fcm_token_server = token; SCNSettings.inst().fcm_token_server = token;
SCNSettings.inst().quota_curr = json.getInt("quota"); SCNSettings.inst().quota_curr = json_int(json, "quota");
SCNSettings.inst().quota_max = json.getInt("quota_max"); SCNSettings.inst().quota_max = json_int(json, "quota_max");
SCNSettings.inst().promode_server = json.getBoolean("is_pro"); SCNSettings.inst().promode_server = json_bool(json, "is_pro");
SCNSettings.inst().save(); SCNSettings.inst().save();
SCNApp.refreshAccountTab(); SCNApp.refreshAccountTab();
@ -123,18 +125,18 @@ public class ServerCommunication
JSONObject json = (JSONObject) new JSONTokener(r).nextValue(); JSONObject json = (JSONObject) new JSONTokener(r).nextValue();
if (!json.getBoolean("success")) if (!json_bool(json, "success"))
{ {
SCNApp.showToast(json.getString("message"), 4000); SCNApp.showToast(json_str(json, "message"), 4000);
return; return;
} }
SCNSettings.inst().user_id = json.getInt("user_id"); SCNSettings.inst().user_id = json_int(json, "user_id");
SCNSettings.inst().user_key = json.getString("user_key"); SCNSettings.inst().user_key = json_str(json, "user_key");
SCNSettings.inst().fcm_token_server = token; SCNSettings.inst().fcm_token_server = token;
SCNSettings.inst().quota_curr = json.getInt("quota"); SCNSettings.inst().quota_curr = json_int(json, "quota");
SCNSettings.inst().quota_max = json.getInt("quota_max"); SCNSettings.inst().quota_max = json_int(json, "quota_max");
SCNSettings.inst().promode_server = json.getBoolean("is_pro"); SCNSettings.inst().promode_server = json_bool(json, "is_pro");
SCNSettings.inst().save(); SCNSettings.inst().save();
SCNApp.refreshAccountTab(); SCNApp.refreshAccountTab();
@ -185,16 +187,16 @@ public class ServerCommunication
JSONObject json = (JSONObject) new JSONTokener(r).nextValue(); JSONObject json = (JSONObject) new JSONTokener(r).nextValue();
if (!json.getBoolean("success")) { if (!json_bool(json, "success")) {
SCNApp.showToast(json.getString("message"), 4000); SCNApp.showToast(json_str(json, "message"), 4000);
return; return;
} }
SCNSettings.inst().user_id = json.getInt("user_id"); SCNSettings.inst().user_id = json_int(json, "user_id");
SCNSettings.inst().user_key = json.getString("user_key"); SCNSettings.inst().user_key = json_str(json, "user_key");
SCNSettings.inst().quota_curr = json.getInt("quota"); SCNSettings.inst().quota_curr = json_int(json, "quota");
SCNSettings.inst().quota_max = json.getInt("quota_max"); SCNSettings.inst().quota_max = json_int(json, "quota_max");
SCNSettings.inst().promode_server = json.getBoolean("is_pro"); SCNSettings.inst().promode_server = json_bool(json, "is_pro");
SCNSettings.inst().save(); SCNSettings.inst().save();
SCNApp.refreshAccountTab(); SCNApp.refreshAccountTab();
@ -246,9 +248,9 @@ public class ServerCommunication
JSONObject json = (JSONObject) new JSONTokener(r).nextValue(); JSONObject json = (JSONObject) new JSONTokener(r).nextValue();
if (!json.getBoolean("success")) if (!json_bool(json, "success"))
{ {
SCNApp.showToast(json.getString("message"), 4000); SCNApp.showToast(json_str(json, "message"), 4000);
int errid = json.optInt("errid", 0); int errid = json.optInt("errid", 0);
@ -270,10 +272,10 @@ public class ServerCommunication
return; return;
} }
SCNSettings.inst().user_id = json.getInt("user_id"); SCNSettings.inst().user_id = json_int(json, "user_id");
SCNSettings.inst().quota_curr = json.getInt("quota"); SCNSettings.inst().quota_curr = json_int(json, "quota");
SCNSettings.inst().quota_max = json.getInt("quota_max"); SCNSettings.inst().quota_max = json_int(json, "quota_max");
SCNSettings.inst().promode_server = json.getBoolean("is_pro"); SCNSettings.inst().promode_server = json_bool(json, "is_pro");
SCNSettings.inst().save(); SCNSettings.inst().save();
SCNApp.refreshAccountTab(); SCNApp.refreshAccountTab();
@ -324,16 +326,16 @@ public class ServerCommunication
JSONObject json = (JSONObject) new JSONTokener(r).nextValue(); JSONObject json = (JSONObject) new JSONTokener(r).nextValue();
if (!json.getBoolean("success")) { if (!json_bool(json, "success")) {
SCNApp.showToast(json.getString("message"), 4000); SCNApp.showToast(json_str(json, "message"), 4000);
return; return;
} }
SCNSettings.inst().user_id = json.getInt("user_id"); SCNSettings.inst().user_id = json_int(json, "user_id");
SCNSettings.inst().user_key = json.getString("user_key"); SCNSettings.inst().user_key = json_str(json, "user_key");
SCNSettings.inst().quota_curr = json.getInt("quota"); SCNSettings.inst().quota_curr = json_int(json, "quota");
SCNSettings.inst().quota_max = json.getInt("quota_max"); SCNSettings.inst().quota_max = json_int(json, "quota_max");
SCNSettings.inst().promode_server = json.getBoolean("is_pro"); SCNSettings.inst().promode_server = json_bool(json, "is_pro");
SCNSettings.inst().save(); SCNSettings.inst().save();
SCNApp.refreshAccountTab(); SCNApp.refreshAccountTab();
@ -353,4 +355,24 @@ public class ServerCommunication
} }
} }
private static boolean json_bool(JSONObject o, String key) throws JSONException
{
Object v = o.get(key);
if (v instanceof Integer) return ((int)v) != 0;
if (v instanceof Boolean) return ((boolean)v);
if (v instanceof String) return !Str.equals(((String)v), "0") && !Str.equals(((String)v), "false");
return o.getBoolean(key);
}
private static int json_int(JSONObject o, String key) throws JSONException
{
return o.getInt(key);
}
private static String json_str(JSONObject o, String key) throws JSONException
{
return o.getString(key);
}
} }

View File

@ -18,9 +18,9 @@
<string name="str_common_settings">Common Settings</string> <string name="str_common_settings">Common Settings</string>
<string name="str_enabled">Enabled</string> <string name="str_enabled">Enabled</string>
<string name="str_localcachesize">Remember the last x notifications locally</string> <string name="str_localcachesize">Remember the last x notifications locally</string>
<string name="str_header_prio0">Notifications (low priority)</string> <string name="str_header_prio0">Notifications (priority 0)</string>
<string name="str_header_prio1">Notifications (normal priority)</string> <string name="str_header_prio1">Notifications (priority 1)</string>
<string name="str_header_prio2">Notifications (high priority)</string> <string name="str_header_prio2">Notifications (priority 2)</string>
<string name="str_msg_enablesound">Enable notification sound</string> <string name="str_msg_enablesound">Enable notification sound</string>
<string name="str_notificationsound">Notification sound</string> <string name="str_notificationsound">Notification sound</string>
<string name="str_repeatnotificationsound">Repeat notification sound</string> <string name="str_repeatnotificationsound">Repeat notification sound</string>

View File

@ -201,6 +201,7 @@ function api_return($http_code, $message)
/** /**
* @param String $str * @param String $str
* @param String[] $path * @param String[] $path
* @return mixed|null
*/ */
function try_json($str, $path) function try_json($str, $path)
{ {