Search in sources :

Example 46 with JSONException

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

the class ShortcutService method dumpCheckin.

/**
     * Dumpsys for checkin.
     *
     * @param clear if true, clear the history information.  Some other system services have this
     * behavior but shortcut service doesn't for now.
     */
private void dumpCheckin(PrintWriter pw, boolean clear) {
    synchronized (mLock) {
        try {
            final JSONArray users = new JSONArray();
            for (int i = 0; i < mUsers.size(); i++) {
                users.put(mUsers.valueAt(i).dumpCheckin(clear));
            }
            final JSONObject result = new JSONObject();
            result.put(KEY_SHORTCUT, users);
            result.put(KEY_LOW_RAM, injectIsLowRamDevice());
            result.put(KEY_ICON_SIZE, mMaxIconDimension);
            pw.println(result.toString(1));
        } catch (JSONException e) {
            Slog.e(TAG, "Unable to write in json", e);
        }
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 47 with JSONException

use of org.json.JSONException in project Conversations by siacs.

the class Contact method deleteOtrFingerprint.

public boolean deleteOtrFingerprint(String fingerprint) {
    synchronized (this.keys) {
        boolean success = false;
        try {
            if (this.keys.has("otr_fingerprints")) {
                JSONArray newPrints = new JSONArray();
                JSONArray oldPrints = this.keys.getJSONArray("otr_fingerprints");
                for (int i = 0; i < oldPrints.length(); ++i) {
                    if (!oldPrints.getString(i).equals(fingerprint)) {
                        newPrints.put(oldPrints.getString(i));
                    } else {
                        success = true;
                    }
                }
                this.keys.put("otr_fingerprints", newPrints);
            }
            return success;
        } catch (JSONException e) {
            return false;
        }
    }
}
Also used : JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 48 with JSONException

use of org.json.JSONException in project core by s4.

the class GenericJsonClientStub method bytesFromEventWrapper.

@Override
public byte[] bytesFromEventWrapper(EventWrapper ew) {
    JSONObject jevent = new JSONObject();
    Object obj = ew.getEvent();
    try {
        jevent.put("stream", ew.getStreamName());
        jevent.put("class", obj.getClass().getName());
        jevent.put("object", GsonUtil.get().toJson(obj));
        return jevent.toString().getBytes(Charset.forName("UTF8"));
    } catch (JSONException e) {
        logger.error("exception while converting event wrapper to bytes.", e);
        return null;
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject)

Example 49 with JSONException

use of org.json.JSONException in project core by s4.

the class Handshake method clientConnectCreate.

private ClientConnection clientConnectCreate(byte[] v, ByteArrayIOChannel io, Socket sock, List<String> reason) throws IOException {
    try {
        JSONObject cInfo = new JSONObject(new String(v));
        String s = cInfo.optString("uuid", "");
        if (s.isEmpty()) {
            logger.error("missing client identifier during handshake.");
            reason.add("missing UUID");
            return null;
        }
        UUID u = UUID.fromString(s);
        logger.info("connecting to client " + u);
        s = cInfo.optString("readMode", "Private");
        ClientReadMode rmode = ClientReadMode.fromString(s);
        if (rmode == null) {
            logger.error(u + ": unknown readMode " + s);
            reason.add("unknown readMode " + s);
            return null;
        }
        s = cInfo.optString("writeMode", "Enabled");
        ClientWriteMode wmode = ClientWriteMode.fromString(s);
        if (wmode == null) {
            logger.error(u + ": unknown writeMode " + s);
            reason.add("unknown writeMode " + s);
            return null;
        }
        logger.info(u + " read=" + rmode + " write=" + wmode);
        if (rmode == ClientReadMode.None && wmode == ClientWriteMode.Disabled) {
            // client cannot disable read AND write...
            logger.error("client neither reads nor writes.");
            reason.add("read and write disabled");
            return null;
        }
        ClientConnection conn = new ClientConnection(clientStub, sock, u, rmode, wmode);
        if (rmode == ClientReadMode.Select) {
            JSONArray incl = cInfo.optJSONArray("readInclude");
            JSONArray excl = cInfo.optJSONArray("readExclude");
            if (incl == null && excl == null) {
                logger.error(u + ": missing stream selection information");
                reason.add("missing readInclude and readExclude");
                return null;
            }
            if (incl != null) {
                List<String> streams = new ArrayList<String>(incl.length());
                for (int i = 0; i < incl.length(); ++i) streams.add(incl.getString(i));
                conn.includeStreams(streams);
            }
            if (excl != null) {
                List<String> streams = new ArrayList<String>(excl.length());
                for (int i = 0; i < excl.length(); ++i) streams.add(excl.getString(i));
                conn.excludeStreams(streams);
            }
        }
        return conn;
    } catch (JSONException e) {
        logger.error("malformed JSON from client during handshake", e);
        reason.add("malformed JSON");
    } catch (NumberFormatException e) {
        logger.error("received malformed UUID", e);
        reason.add("malformed UUID");
    } catch (IllegalArgumentException e) {
        logger.error("received malformed UUID", e);
        reason.add("malformed UUID");
    }
    return null;
}
Also used : JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) UUID(java.util.UUID)

Example 50 with JSONException

use of org.json.JSONException in project core by s4.

the class Handshake method clientConnect.

private ClientConnection clientConnect(byte[] v, ByteArrayIOChannel io, Socket sock) throws IOException {
    List<String> reason = new ArrayList<String>(1);
    ClientConnection conn = clientConnectCreate(v, io, sock, reason);
    String message = null;
    try {
        JSONObject resp = new JSONObject();
        resp.put("status", (conn != null ? "ok" : "failed"));
        if (conn == null && !reason.isEmpty()) {
            resp.put("reason", reason.get(0));
        }
        message = resp.toString();
    } catch (JSONException e) {
        logger.error("error creating response during connect.", e);
        return null;
    }
    io.send(message.getBytes());
    return conn;
}
Also used : JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException)

Aggregations

JSONException (org.json.JSONException)1904 JSONObject (org.json.JSONObject)1422 JSONArray (org.json.JSONArray)638 IOException (java.io.IOException)338 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)77 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