Search in sources :

Example 41 with JSONException

use of org.json.JSONException in project glimmr by brk3.

the class AddItemToGroupTask method execute.

@Override
public void execute(final ITaskQueueServiceListener listener) {
    if (BuildConfig.DEBUG) {
        Log.d(TAG, String.format("Processing photo id %s for group %s", mItemId, mGroupId));
    }
    if (mOAuth == null) {
        Log.e(TAG, "AddItemToGroupTask requires authentication");
        MAIN_THREAD.post(new Runnable() {

            @Override
            public void run() {
                listener.onFailure(mItemId, false);
            }
        });
        return;
    }
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                OAuthToken token = mOAuth.getToken();
                Flickr f = FlickrHelper.getInstance().getFlickrAuthed(token.getOauthToken(), token.getOauthTokenSecret());
                try {
                    f.getPoolsInterface().add(mItemId, mGroupId);
                    /* success */
                    postToMainThread(listener, true, false);
                } catch (FlickrException e) {
                    e.printStackTrace();
                    final String errCode = e.getErrorCode();
                    /* any of the following warrants no retry */
                    if (FLICKR_PHOTO_NOT_FOUND.equals(errCode) || FLICKR_GROUP_NOT_FOUND.equals(errCode) || FLICKR_ALREADY_IN_POOL.equals(errCode) || FLICKR_PHOTO_IN_MAX_POOLS.equals(errCode) || FLICKR_PHOTO_LIMIT_REACHED.equals(errCode) || FLICKR_PHOTO_PENDING_MOD.equals(errCode) || FLICKR_PHOTO_PENDING.equals(errCode) || FLICKR_POOL_FULL.equals(errCode)) {
                        postToMainThread(listener, false, false);
                    } else {
                        Log.e(TAG, "Unknown FlickrException code: " + errCode);
                        postToMainThread(listener, false, false);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    /* failure, queue for retry */
                    postToMainThread(listener, false, true);
                } catch (IOException e) {
                    e.printStackTrace();
                    /* failure, queue for retry */
                    postToMainThread(listener, false, true);
                } catch (Exception e) {
                    e.printStackTrace();
                    /* shouldn't get here, don't retry */
                    postToMainThread(listener, false, false);
                }
            } catch (RuntimeException e) {
                e.printStackTrace();
                /* shouldn't get here, don't retry */
                postToMainThread(listener, false, false);
            }
        }
    }).start();
}
Also used : OAuthToken(com.googlecode.flickrjandroid.oauth.OAuthToken) Flickr(com.googlecode.flickrjandroid.Flickr) FlickrException(com.googlecode.flickrjandroid.FlickrException) JSONException(org.json.JSONException) IOException(java.io.IOException) FlickrException(com.googlecode.flickrjandroid.FlickrException) IOException(java.io.IOException) JSONException(org.json.JSONException)

Example 42 with JSONException

use of org.json.JSONException in project platform_frameworks_base by android.

the class AccountSyncSettingsBackupHelper method restoreEntity.

/**
     * Restore account sync settings from the given data input stream.
     */
@Override
public void restoreEntity(BackupDataInputStream data) {
    byte[] dataBytes = new byte[data.size()];
    try {
        // Read the data and convert it to a String.
        data.read(dataBytes);
        String dataString = new String(dataBytes, JSON_FORMAT_ENCODING);
        // Convert data to a JSON object.
        JSONObject dataJSON = new JSONObject(dataString);
        boolean masterSyncEnabled = dataJSON.getBoolean(KEY_MASTER_SYNC_ENABLED);
        JSONArray accountJSONArray = dataJSON.getJSONArray(KEY_ACCOUNTS);
        boolean currentMasterSyncEnabled = ContentResolver.getMasterSyncAutomatically();
        if (currentMasterSyncEnabled) {
            // Disable master sync to prevent any syncs from running.
            ContentResolver.setMasterSyncAutomatically(false);
        }
        try {
            restoreFromJsonArray(accountJSONArray);
        } finally {
            // Set the master sync preference to the value from the backup set.
            ContentResolver.setMasterSyncAutomatically(masterSyncEnabled);
        }
        Log.i(TAG, "Restore successful.");
    } catch (IOException | JSONException e) {
        Log.e(TAG, "Couldn't restore account sync settings\n" + e);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException)

Example 43 with JSONException

use of org.json.JSONException in project platform_frameworks_base by android.

the class AccountSyncSettingsBackupHelper method accountAddedInternal.

/**
     * Restore SyncSettings for all existing accounts from a stashed backup-set
     */
private void accountAddedInternal() {
    String jsonString;
    try (FileInputStream fIn = new FileInputStream(new File(STASH_FILE))) {
        DataInputStream in = new DataInputStream(fIn);
        jsonString = in.readUTF();
    } catch (FileNotFoundException fnfe) {
        // This is expected to happen when there is no accounts info stashed
        if (DEBUG)
            Log.d(TAG, "unable to find the stash file", fnfe);
        return;
    } catch (IOException ioe) {
        if (DEBUG)
            Log.d(TAG, "could not read sync settings from stash file", ioe);
        return;
    }
    try {
        JSONArray unaddedAccountsJSONArray = new JSONArray(jsonString);
        restoreFromJsonArray(unaddedAccountsJSONArray);
    } catch (JSONException jse) {
        // Malformed jsonString
        Log.e(TAG, "there was an error with the stashed sync settings", jse);
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) BackupDataInputStream(android.app.backup.BackupDataInputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 44 with JSONException

use of org.json.JSONException in project platform_frameworks_base by android.

the class AndroidAppAsset method create.

/**
     * Checks that the input is a valid Android app asset.
     *
     * @param asset a JSONObject that has "namespace", "package_name", and
     *              "sha256_cert_fingerprints" fields.
     * @throws AssociationServiceException if the asset is not well formatted.
     */
public static AndroidAppAsset create(JSONObject asset) throws AssociationServiceException {
    String packageName = asset.optString(Utils.ANDROID_APP_ASSET_FIELD_PACKAGE_NAME);
    if (packageName.equals("")) {
        throw new AssociationServiceException(String.format(MISSING_FIELD_FORMAT_STRING, Utils.ANDROID_APP_ASSET_FIELD_PACKAGE_NAME));
    }
    JSONArray certArray = asset.optJSONArray(Utils.ANDROID_APP_ASSET_FIELD_CERT_FPS);
    if (certArray == null || certArray.length() == 0) {
        throw new AssociationServiceException(String.format(MISSING_APPCERTS_FORMAT_STRING, Utils.ANDROID_APP_ASSET_FIELD_CERT_FPS));
    }
    List<String> certFingerprints = new ArrayList<>(certArray.length());
    for (int i = 0; i < certArray.length(); i++) {
        try {
            certFingerprints.add(certArray.getString(i));
        } catch (JSONException e) {
            throw new AssociationServiceException(String.format(APPCERT_NOT_STRING_FORMAT_STRING, Utils.ANDROID_APP_ASSET_FIELD_CERT_FPS));
        }
    }
    return new AndroidAppAsset(packageName, certFingerprints);
}
Also used : JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException)

Example 45 with JSONException

use of org.json.JSONException in project platform_frameworks_base by android.

the class JsonParser method parse.

/**
     * Consumes and parses exactly one JSON object from the {@link JsonReader}.
     * The object's fields can only be objects, strings or arrays of strings.
     */
public static JSONObject parse(JsonReader reader) throws IOException, JSONException {
    JSONObject output = new JSONObject();
    String errorMsg = null;
    reader.beginObject();
    while (reader.hasNext()) {
        String fieldName = reader.nextName();
        if (output.has(fieldName)) {
            errorMsg = "Duplicate field name.";
            reader.skipValue();
            continue;
        }
        JsonToken token = reader.peek();
        if (token.equals(JsonToken.BEGIN_ARRAY)) {
            output.put(fieldName, new JSONArray(parseArray(reader)));
        } else if (token.equals(JsonToken.STRING)) {
            output.put(fieldName, reader.nextString());
        } else if (token.equals(JsonToken.BEGIN_OBJECT)) {
            try {
                output.put(fieldName, parse(reader));
            } catch (JSONException e) {
                errorMsg = e.getMessage();
            }
        } else {
            reader.skipValue();
            errorMsg = "Unsupported value type.";
        }
    }
    reader.endObject();
    if (errorMsg != null) {
        throw new JSONException(errorMsg);
    }
    return output;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JsonToken(android.util.JsonToken)

Aggregations

JSONException (org.json.JSONException)1899 JSONObject (org.json.JSONObject)1418 JSONArray (org.json.JSONArray)635 IOException (java.io.IOException)336 ArrayList (java.util.ArrayList)211 HashMap (java.util.HashMap)145 Test (org.junit.Test)128 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)98 File (java.io.File)76 Bundle (android.os.Bundle)75 VolleyError (com.android.volley.VolleyError)67 Intent (android.content.Intent)63 DefaultGNSTest (edu.umass.cs.gnsserver.utils.DefaultGNSTest)62 Response (com.android.volley.Response)59 URL (java.net.URL)57 Map (java.util.Map)48 TextView (android.widget.TextView)46 View (android.view.View)45 RandomString (edu.umass.cs.gnscommon.utils.RandomString)44 MalformedURLException (java.net.MalformedURLException)44