List of usage examples for org.json JSONException getMessage
public String getMessage()
From source file:com.vinnypalumbo.vinnysmovies.FetchTrailersTask.java
/** * Take the String representing the trailer results in JSON Format and * pull out the data we need for each trailer * * Fortunately parsing is easy: constructor takes the JSON string and converts it * into an Object hierarchy for us.//w w w . ja v a 2 s . c om */ private List<Trailer> getTrailerDataFromJson(String trailerJsonStr) throws JSONException { Log.d("vinny-debug", "FetchTrailersTask - getTrailerDataFromJson"); // These are the names of the JSON objects that need to be extracted. final String TMDB_RESULTS = "results"; final String TMDB_NAME = "name"; final String TMDB_KEY = "key"; try { JSONObject trailerJson = new JSONObject(trailerJsonStr); JSONArray trailerArray = trailerJson.getJSONArray(TMDB_RESULTS); // Insert the trailer infos into an ArrayList List<Trailer> trailers = new ArrayList<Trailer>(); for (int i = 0; i < trailerArray.length(); i++) { String trailerName; String trailerKey; // Get the JSON object representing the trailer JSONObject trailer = trailerArray.getJSONObject(i); // the trailer name is in a String associated to the key "name" trailerName = trailer.getString(TMDB_NAME); // the trailer key is in a String associated to the key "key" trailerKey = trailer.getString(TMDB_KEY); trailers.add(new Trailer(trailerName, trailerKey)); } Log.d(LOG_TAG, "Fetch Trailers Completed"); return trailers; } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); e.printStackTrace(); } return null; }
From source file:com.teleca.jamendo.util.download.DownloadTask.java
@Override public Boolean doInBackground(Void... params) { // ogg support if (mJob.getFormat().equals(JamendoGet2Api.ENCODING_OGG)) { Log.i(JamendoApplication.TAG, "Getting path for ogg"); int track_id = mJob.getPlaylistEntry().getTrack().getId(); JamendoGet2Api api = new JamendoGet2ApiImpl(); try {/*from ww w.j av a 2s . c o m*/ Track track[] = api.getTracksByTracksId(new int[] { track_id }, mJob.getFormat()); if (track == null || track.length != 1) { return false; } else { mJob.getPlaylistEntry().setTrack(track[0]); } } catch (JSONException e) { return false; } catch (WSError e) { return false; } } try { return downloadFile(mJob); } catch (IOException e) { Log.e(JamendoApplication.TAG, "Download file faild reason-> " + e.getMessage()); return false; } }
From source file:com.baroq.pico.google.PlayServices.java
@Override public boolean execute(String action, JSONArray data, CallbackContext callbackContext) { try {/*from ww w . j av a 2 s . co m*/ if (!ACTION_SETUP.equals(action) && !ACTION_SIGNIN.equals(action) && (null == mHelper || !mHelper.isConnected())) { callbackContext.error("Please setup and signin to use PlayServices plugin"); return false; } if (ACTION_SETUP.equals(action)) { int l = data.length(); if (0 == l) { callbackContext.error("Expecting at least 1 parameter for action: " + action); return false; } clientTypes = data.getInt(0); String[] extraScopes = new String[l - 1]; for (int i = 1; i < l; i++) { extraScopes[i - 1] = data.getString(i); } setup(clientTypes, extraScopes, callbackContext); PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT); pluginResult.setKeepCallback(true); callbackContext.sendPluginResult(pluginResult); } else if (ACTION_SIGNIN.equals(action)) { mHelper.beginUserInitiatedSignIn(); callbackContext.success(); } else if (ACTION_SIGNOUT.equals(action)) { signout(); callbackContext.success(); } else if (ACTION_AS_MAX_KEYS.equals(action)) { PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, mHelper.getAppStateClient().getMaxNumKeys()); pluginResult.setKeepCallback(false); callbackContext.sendPluginResult(pluginResult); } else if (ACTION_AS_MAX_SIZE.equals(action)) { PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, mHelper.getAppStateClient().getMaxStateSize()); pluginResult.setKeepCallback(false); callbackContext.sendPluginResult(pluginResult); } else if (ACTION_AS_DEL.equals(action)) { int key = data.getInt(0); mHelper.getAppStateClient().deleteState(this, key); callbackContext.success(); } else if (ACTION_AS_LIST.equals(action)) { mHelper.getAppStateClient().listStates(this); callbackContext.success(); } else if (ACTION_AS_LOAD.equals(action)) { int key = data.getInt(0); mHelper.getAppStateClient().loadState(this, key); callbackContext.success(); } else if (ACTION_AS_RESOLVE.equals(action)) { int key = data.getInt(0); String resolvedVersion = data.getString(1); String value = data.getString(2); mHelper.getAppStateClient().resolveState(this, key, resolvedVersion, value.getBytes()); callbackContext.success(); } else if (ACTION_AS_UPDATE.equals(action)) { int key = data.getInt(0); String value = data.getString(1); mHelper.getAppStateClient().updateState(key, value.getBytes()); callbackContext.success(); } else if (ACTION_AS_UPDATE_NOW.equals(action)) { int key = data.getInt(0); String value = data.getString(1); mHelper.getAppStateClient().updateStateImmediate(this, key, value.getBytes()); callbackContext.success(); } else if (ACTION_GAME_SHOW_ACHIEVEMENTS.equals(action)) { cordova.startActivityForResult((CordovaPlugin) this, mHelper.getGamesClient().getAchievementsIntent(), RC_UNUSED); callbackContext.success(); } else if (ACTION_GAME_SHOW_LEADERBOARDS.equals(action)) { cordova.startActivityForResult((CordovaPlugin) this, mHelper.getGamesClient().getAllLeaderboardsIntent(), RC_UNUSED); callbackContext.success(); } else if (ACTION_GAME_SHOW_LEADERBOARD.equals(action)) { String id = data.getString(0); cordova.startActivityForResult((CordovaPlugin) this, mHelper.getGamesClient().getLeaderboardIntent(id), RC_UNUSED); callbackContext.success(); } else if (ACTION_GAME_INCR_ACHIEVEMENT.equals(action)) { String id = data.getString(0); int numSteps = data.getInt(1); mHelper.getGamesClient().incrementAchievement(id, numSteps); callbackContext.success(); } else if (ACTION_GAME_INCR_ACHIEVEMENT_NOW.equals(action)) { String id = data.getString(0); int numSteps = data.getInt(1); mHelper.getGamesClient().incrementAchievementImmediate(this, id, numSteps); callbackContext.success(); } else if (ACTION_GAME_LOAD_ACHIEVEMENTS.equals(action)) { boolean forceReload = data.getBoolean(0); mHelper.getGamesClient().loadAchievements(this, forceReload); callbackContext.success(); } else if (ACTION_GAME_LOAD_GAME.equals(action)) { mHelper.getGamesClient().loadGame(this); callbackContext.success(); } else if (ACTION_GAME_LOAD_LEADERBOARD_METADATA.equals(action)) { if (1 == data.length()) { mHelper.getGamesClient().loadLeaderboardMetadata(this, data.getBoolean(0)); } else { mHelper.getGamesClient().loadLeaderboardMetadata(this, data.getString(0), data.getBoolean(1)); } callbackContext.success(); } else if (ACTION_GAME_LOAD_MORE_SCORES.equals(action)) { if (null == scoreBuffer) { callbackContext.error("Get a leaderboard fist before calling: " + action); return false; } int maxResults = data.getInt(0); int pageDirection = data.getInt(0); mHelper.getGamesClient().loadMoreScores(this, scoreBuffer, maxResults, pageDirection); callbackContext.success(); } else if (ACTION_GAME_LOAD_PLAYER.equals(action)) { String playerId = data.getString(0); mHelper.getGamesClient().loadPlayer(this, playerId); callbackContext.success(); } else if (ACTION_GAME_LOAD_PLAYER_CENTERED_SCORES.equals(action)) { String leaderboardId = data.getString(0); int span = data.getInt(1); int leaderboardCollection = data.getInt(2); int maxResults = data.getInt(3); if (data.isNull(4)) { mHelper.getGamesClient().loadPlayerCenteredScores(this, leaderboardId, span, leaderboardCollection, maxResults); } else { boolean forceReload = data.getBoolean(4); mHelper.getGamesClient().loadPlayerCenteredScores(this, leaderboardId, span, leaderboardCollection, maxResults, forceReload); } callbackContext.success(); } else if (ACTION_GAME_LOAD_TOP_SCORES.equals(action)) { String leaderboardId = data.getString(0); int span = data.getInt(1); int leaderboardCollection = data.getInt(2); int maxResults = data.getInt(3); if (data.isNull(4)) { mHelper.getGamesClient().loadTopScores(this, leaderboardId, span, leaderboardCollection, maxResults); } else { boolean forceReload = data.getBoolean(4); mHelper.getGamesClient().loadTopScores(this, leaderboardId, span, leaderboardCollection, maxResults, forceReload); } callbackContext.success(); } else if (ACTION_GAME_REVEAL_ACHIEVEMENT.equals(action)) { String id = data.getString(0); mHelper.getGamesClient().revealAchievement(id); callbackContext.success(); } else if (ACTION_GAME_REVEAL_ACHIEVEMENT_NOW.equals(action)) { String id = data.getString(0); mHelper.getGamesClient().revealAchievementImmediate(this, id); callbackContext.success(); } else if (ACTION_GAME_SUBMIT_SCORE.equals(action)) { String leaderboardId = data.getString(0); int score = data.getInt(1); mHelper.getGamesClient().submitScore(leaderboardId, score); callbackContext.success(); } else if (ACTION_GAME_SUBMIT_SCORE_NOW.equals(action)) { String leaderboardId = data.getString(0); int score = data.getInt(1); mHelper.getGamesClient().submitScoreImmediate(this, leaderboardId, score); callbackContext.success(); } else if (ACTION_GAME_UNLOCK_ACHIEVEMENT.equals(action)) { String id = data.getString(0); mHelper.getGamesClient().unlockAchievement(id); callbackContext.success(); } else if (ACTION_GAME_UNLOCK_ACHIEVEMENT_NOW.equals(action)) { String id = data.getString(0); mHelper.getGamesClient().unlockAchievementImmediate(this, id); callbackContext.success(); } else { callbackContext.error("Unknown action: " + action); return false; } } catch (JSONException ex) { callbackContext.error(ex.getMessage()); return false; } return true; }
From source file:com.baroq.pico.google.PlayServices.java
@Override public void onSignInSucceeded() { JSONObject json = new JSONObject(); try {// w w w. ja v a2 s. co m json.put("type", GMS_SIGNIN); json.put("signin", true); } catch (JSONException ex) { Log.e(TAG, "signin succeeded exception: " + ex.getMessage()); return; } Log.d(TAG, "signin succeeded"); PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, json); pluginResult.setKeepCallback(true); connectionCB.sendPluginResult(pluginResult); }
From source file:com.baroq.pico.google.PlayServices.java
@Override public void onSignInFailed(String reason) { JSONObject json = new JSONObject(); try {//www . j a v a 2 s . c o m json.put("type", GMS_SIGNIN); json.put("signin", false); json.put("message", reason); } catch (JSONException ex) { Log.e(TAG, "signin failed exception: " + ex.getMessage()); return; } Log.d(TAG, "signin failed"); PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, json); pluginResult.setKeepCallback(true); connectionCB.sendPluginResult(pluginResult); }
From source file:com.baroq.pico.google.PlayServices.java
@Override public void onStateLoaded(int statusCode, int stateKey, byte[] localData) { JSONObject json = new JSONObject(); try {// w w w.ja va 2 s . c o m json.put("type", STATE_LOADED); json.put("status", statusCode); switch (statusCode) { case AppStateClient.STATUS_OK: // Data was successfully loaded from the cloud: merge with local data. json.put("stateKey", stateKey); json.put("data", new String(localData)); break; case AppStateClient.STATUS_STATE_KEY_NOT_FOUND: // key not found means there is no saved data. To us, this is the same as // having empty data, so we treat this as a success. break; case AppStateClient.STATUS_STATE_KEY_LIMIT_EXCEEDED: // if the application already has data present in the maximum number of state keys. break; case AppStateClient.STATUS_NETWORK_ERROR_NO_DATA: // can't reach cloud, and we have no local state. Warn user that // they may not see their existing progress, but any new progress won't be lost. break; case AppStateClient.STATUS_NETWORK_ERROR_STALE_DATA: // can't reach cloud, but we have locally cached data. break; case AppStateClient.STATUS_CLIENT_RECONNECT_REQUIRED: // need to reconnect AppStateClient mHelper.reconnectClients(clientTypes); break; case AppStateClient.STATUS_INTERNAL_ERROR: // if an unexpected error occurred in the service. break; default: // error break; } } catch (JSONException ex) { Log.e(TAG, "STATE_LOADED [" + statusCode + "] [" + stateKey + "] exception: " + ex.getMessage()); return; } PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, json); pluginResult.setKeepCallback(true); connectionCB.sendPluginResult(pluginResult); }
From source file:com.baroq.pico.google.PlayServices.java
@Override public void onStateConflict(int stateKey, String resolvedVersion, byte[] localData, byte[] serverData) { // Need to resolve conflict between the two states. // We do that by taking the union of the two sets of cleared levels, // which means preserving the maximum star rating of each cleared // level:// ww w .j av a 2 s . c om JSONObject json = new JSONObject(); try { json.put("type", STATE_CONFLICTED); json.put("stateKey", stateKey); json.put("version", resolvedVersion); json.put("localData", new JSONObject(new String(localData))); json.put("serverData", new JSONObject(new String(serverData))); } catch (JSONException ex) { Log.e(TAG, "STATE_CONFLICTED [" + stateKey + "] [" + resolvedVersion + "] exception: " + ex.getMessage()); return; } PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, json); pluginResult.setKeepCallback(true); connectionCB.sendPluginResult(pluginResult); }
From source file:com.baroq.pico.google.PlayServices.java
@Override public void onStateListLoaded(int statusCode, AppStateBuffer buffer) { JSONObject json = new JSONObject(); try {/*from www .j av a 2 s .c o m*/ json.put("type", STATE_LIST_LOADED); json.put("status", statusCode); switch (statusCode) { case AppStateClient.STATUS_OK: JSONArray jsonStates = new JSONArray(); json.put("states", jsonStates); AppState state; JSONObject jsonState; for (int i = 0, l = buffer.getCount(); i < l; i++) { state = buffer.get(i); jsonState = new JSONObject(); if (state.hasConflict()) { jsonState.put("type", STATE_CONFLICTED); jsonState.put("stateKey", state.getKey()); jsonState.put("version", state.getConflictVersion()); jsonState.put("localVersion", state.getLocalVersion()); jsonState.put("localData", new JSONObject(new String(state.getLocalData()))); jsonState.put("serverData", new JSONObject(new String(state.getConflictData()))); } else { jsonState.put("type", STATE_LOADED); jsonState.put("status", statusCode); jsonState.put("stateKey", state.getKey()); jsonState.put("data", new JSONObject(new String(state.getLocalData()))); } jsonStates.put(jsonState); } // Data was successfully loaded from the cloud: merge with local data. break; case AppStateClient.STATUS_NETWORK_ERROR_NO_DATA: // can't reach cloud, and we have no local state. Warn user that // they may not see their existing progress, but any new progress won't be lost. break; case AppStateClient.STATUS_NETWORK_ERROR_STALE_DATA: // can't reach cloud, but we have locally cached data. break; case AppStateClient.STATUS_CLIENT_RECONNECT_REQUIRED: // need to reconnect AppStateClient mHelper.reconnectClients(clientTypes); break; case AppStateClient.STATUS_INTERNAL_ERROR: // if an unexpected error occurred in the service. break; default: // error break; } } catch (JSONException ex) { Log.e(TAG, "STATE_LIST_LOADED [" + statusCode + "][" + buffer.getCount() + "] exception: " + ex.getMessage()); return; } buffer.close(); PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, json); pluginResult.setKeepCallback(true); connectionCB.sendPluginResult(pluginResult); }
From source file:com.baroq.pico.google.PlayServices.java
@Override public void onStateDeleted(int statusCode, int stateKey) { JSONObject json = new JSONObject(); try {/*from w w w. ja v a2s. c om*/ json.put("type", STATE_CONFLICTED); json.put("statusCode", statusCode); json.put("stateKey", stateKey); switch (statusCode) { case AppStateClient.STATUS_OK: // if data was successfully deleted from the server. break; case AppStateClient.STATUS_INTERNAL_ERROR: // if an unexpected error occurred in the service break; case AppStateClient.STATUS_NETWORK_ERROR_OPERATION_FAILED: // if the device was unable to communicate with the network. In this case, the operation is not retried automatically. break; case AppStateClient.STATUS_CLIENT_RECONNECT_REQUIRED: // need to reconnect AppStateClient mHelper.reconnectClients(clientTypes); break; default: // error break; } } catch (JSONException ex) { Log.e(TAG, "STATE_DELETED [" + statusCode + "] [" + stateKey + "] exception: " + ex.getMessage()); return; } PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, json); pluginResult.setKeepCallback(true); connectionCB.sendPluginResult(pluginResult); }
From source file:com.baroq.pico.google.PlayServices.java
@Override public void onGamesLoaded(int statusCode, GameBuffer buffer) { JSONObject json = new JSONObject(); try {/*from w w w .j a va 2 s . c o m*/ json.put("type", GAMES_LOADED); json.put("statusCode", statusCode); switch (statusCode) { case GamesClient.STATUS_OK: // if data was successfully loaded and is up-to-date. JSONArray games = new JSONArray(); JSONObject game; for (int i = 0, l = buffer.getCount(); i < l; i++) { Game g = buffer.get(i); game = new JSONObject(); game.put("achievementTotalCount", g.getAchievementTotalCount()); game.put("applicationId", g.getApplicationId()); game.put("description", g.getDescription()); game.put("developerName", g.getDeveloperName()); game.put("displayName", g.getDisplayName()); Uri uri = g.getFeaturedImageUri(); if (null != uri) game.put("featuredImageUri", uri.getScheme() + ':' + uri.getSchemeSpecificPart()); uri = g.getHiResImageUri(); if (null != uri) game.put("hiResImageUri", uri.getScheme() + ':' + uri.getSchemeSpecificPart()); uri = g.getIconImageUri(); if (null != uri) game.put("iconImageUri", uri.getScheme() + ':' + uri.getSchemeSpecificPart()); game.put("leaderboardCount", g.getLeaderboardCount()); game.put("primaryCategory", g.getPrimaryCategory()); game.put("secondaryCategory", g.getSecondaryCategory()); games.put(game); } json.put("list", games); break; case GamesClient.STATUS_INTERNAL_ERROR: // if an unexpected error occurred in the service break; case GamesClient.STATUS_NETWORK_ERROR_STALE_DATA: // if the device was unable to communicate with the network. In this case, the operation is not retried automatically. break; case GamesClient.STATUS_CLIENT_RECONNECT_REQUIRED: // need to reconnect GamesClient mHelper.reconnectClients(clientTypes); break; case GamesClient.STATUS_LICENSE_CHECK_FAILED: // The game is not licensed to the user. Further calls will return the same code. break; default: // error break; } } catch (JSONException ex) { Log.e(TAG, "GAMES_LOADED [" + statusCode + "] exception: " + ex.getMessage()); return; } buffer.close(); PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, json); pluginResult.setKeepCallback(true); connectionCB.sendPluginResult(pluginResult); }