Example usage for org.json JSONException printStackTrace

List of usage examples for org.json JSONException printStackTrace

Introduction

In this page you can find the example usage for org.json JSONException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:de.dakror.virtualhub.data.Catalog.java

public JSONObject getJSONObject() {
    try {/*from   ww w . j a v  a 2 s  . c om*/
        JSONObject o = new JSONObject();
        o.put("name", name);
        JSONArray sources = new JSONArray();
        for (File f : this.sources)
            sources.put(f.getPath().replace("\\", "/"));
        o.put("sources", sources);

        o.put("tags", tags.toArray(new String[] {}));

        return o;
    } catch (JSONException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:io.s4.client.Driver.java

/**
 * Initialize the driver.//from   w ww. j a va 2 s .c  om
 * 
 * Handshake with adapter to receive a unique id, and verify that the driver
 * is compatible with the protocol used by the adapter. This does not
 * actually establish a connection for sending and receiving events.
 * 
 * @see #connect(ReadMode, WriteMode)
 * 
 * @return true if and only if the adapter issued a valid ID to this client,
 *         and the protocol is found to be compatible.
 * @throws IOException
 *             if the underlying TCP/IP socket throws an exception.
 */
public boolean init() throws IOException {
    if (state.isInitialized())
        return true;

    try {
        sock = new Socket(hostname, port);

        ByteArrayIOChannel io = new ByteArrayIOChannel(sock);

        io.send(emptyBytes);

        byte[] b = io.recv();

        if (b == null || b.length == 0) {
            if (debug) {
                System.err.println("Empty response during initialization.");
            }
            return false;
        }

        JSONObject json = new JSONObject(new String(b));

        this.uuid = json.getString("uuid");

        JSONObject proto = json.getJSONObject("protocol");

        if (!isCompatible(proto)) {
            if (debug) {
                System.err.println("Driver not compatible with adapter protocol: " + proto);
            }
            return false;
        }

        state = State.Initialized;

        return true;

    } catch (JSONException e) {
        if (debug) {
            System.err.println("malformed JSON in initialization response. " + e);
        }
        e.printStackTrace();
        return false;

    } finally {
        sock.close();
    }
}

From source file:io.s4.client.Driver.java

/**
 * Establish a connection to the adapter. Upon success, this enables the
 * client to send and receive events. The client must first be initialized.
 * Otherwise, this operation will fail.//from  ww  w .  j  av  a 2  s .c o m
 * 
 * @see #init()
 * 
 * @return true if and only if a connection was successfully established.
 * @throws IOException
 *             if the underlying TCP/IP socket throws an exception.
 */
public boolean connect() throws IOException {
    if (!state.isInitialized()) {
        // must first be initialized
        if (debug) {
            System.err.println("Not initialized.");
        }
        return false;
    } else if (state.isConnected()) {
        // nothing to do if already connected.
        return true;
    }

    String message = null;

    try {
        // constructing connect message
        JSONObject json = new JSONObject();

        json.put("uuid", uuid);
        json.put("readMode", readMode.toString());
        json.put("writeMode", writeMode.toString());

        if (readInclude != null) {
            // stream inclusion
            json.put("readInclude", new JSONArray(readInclude));
        }

        if (readExclude != null) {
            // stream exclusion
            json.put("readExclude", new JSONArray(readExclude));
        }

        message = json.toString();

    } catch (JSONException e) {
        if (debug) {
            System.err.println("error constructing connect message: " + e);
        }
        return false;
    }

    try {
        // send the message
        this.sock = new Socket(hostname, port);
        this.io = new ByteArrayIOChannel(sock);

        io.send(message.getBytes());

        // get a response
        byte[] b = io.recv();

        if (b == null || b.length == 0) {
            if (debug) {
                System.err.println("empty response from adapter during connect.");
            }
            return false;
        }

        String response = new String(b);

        JSONObject json = new JSONObject(response);
        String s = json.optString("status", "unknown");

        // does it look OK?
        if (s.equalsIgnoreCase("ok")) {
            // done connecting
            state = State.Connected;
            return true;
        } else if (s.equalsIgnoreCase("failed")) {
            // server has failed the connect attempt
            if (debug) {
                System.err.println("connect failed by adapter. reason: " + json.optString("reason", "unknown"));
            }
            return false;
        } else {
            // unknown response.
            if (debug) {
                System.err.println("connect failed by adapter. unrecongnized response: " + response);
            }
            return false;
        }

    } catch (Exception e) {
        // clean up after error...
        if (debug) {
            System.err.println("error during connect: " + e);
            e.printStackTrace();
        }

        if (this.sock.isConnected()) {
            this.sock.close();

        }

        return false;
    }
}

From source file:census.couchdroid.CouchViewResults.java

/**
 * Retrieves a list of documents that matched this View.
 * These documents only contain the data that the View has returned (not the full document).
 * <p>//from  ww  w  . j av a 2 s  .c o m
 * You can load the remaining information from Document.reload();
 * 
 * @return
*/
public List<CouchDocument> getResults() {
    JSONArray ar = null;
    try {
        android.util.Log.e("Census", getJSONObject().toString());
        ar = getJSONObject().getJSONArray("rows");
    } catch (JSONException e) {
        return null;
    }
    List<CouchDocument> docs = new ArrayList<CouchDocument>(ar.length());
    for (int i = 0; i < ar.length(); i++) {
        try {
            if (ar.get(i) != null && !ar.getString(i).equals("null")) {
                CouchDocument d = new CouchDocument(ar.getJSONObject(i));
                d.setDatabase(database);
                docs.add(d);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return docs;

}

From source file:com.sina.weibo.sdk_lib.openapi.models.Favorite.java

public static Favorite parse(String jsonString) {
    try {//from  www . j  a  v  a  2  s.c o m
        JSONObject object = new JSONObject(jsonString);
        return Favorite.parse(object);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.vinexs.tool.XML.java

private static Object getChildJSONObject(org.w3c.dom.Element tag) {
    int i, k;//from w ww. j a v  a 2  s .co  m

    //get attributes && child nodes
    NamedNodeMap attributes = tag.getAttributes();
    NodeList childNodes = tag.getChildNodes();
    int numAttr = attributes.getLength();
    int numChild = childNodes.getLength();

    //get element nodes
    Boolean hasTagChild = false;
    Map<String, ArrayList<Object>> childMap = new HashMap<>();
    for (i = 0; i < numChild; i++) {
        Node node = childNodes.item(i);
        //not process non-element node
        if (node.getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) {
            continue;
        }
        hasTagChild = true;
        org.w3c.dom.Element childTag = (org.w3c.dom.Element) node;
        String tagName = childTag.getTagName();
        if (!childMap.containsKey(tagName)) {
            childMap.put(tagName, new ArrayList<>());
        }
        childMap.get(tagName).add(getChildJSONObject(childTag));
    }
    if (numAttr == 0 && !hasTagChild) {
        // Return String
        return stringToValue(tag.getTextContent());
    } else {
        // Return JSONObject
        JSONObject data = new JSONObject();
        if (numAttr > 0) {
            for (i = 0; i < numAttr; i++) {
                Node attr = attributes.item(i);
                try {
                    data.put(attr.getNodeName(), stringToValue(attr.getNodeValue()));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
        if (hasTagChild) {
            for (Map.Entry<String, ArrayList<Object>> tagMap : childMap.entrySet()) {
                ArrayList<Object> tagList = tagMap.getValue();
                if (tagList.size() == 1) {
                    try {
                        data.put(tagMap.getKey(), tagList.get(0));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    JSONArray array = new JSONArray();
                    for (k = 0; k < tagList.size(); k++) {
                        array.put(tagList.get(k));
                    }
                    try {
                        data.put(tagMap.getKey(), array);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            try {
                data.put("content", stringToValue(tag.getTextContent()));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return data;
    }
}

From source file:nl.hnogames.domoticzapi.Parsers.NotificationsParser.java

@Override
public void parseResult(String result) {
    try {/*  w ww. j  a va  2 s .com*/
        JSONArray jsonArray = new JSONArray(result);
        ArrayList<NotificationInfo> mNotificationInfo = new ArrayList<>();

        if (jsonArray.length() > 0) {
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject row = jsonArray.getJSONObject(i);
                mNotificationInfo.add(new NotificationInfo(row));
            }
        }
        notificationsReceiver.onReceiveNotifications(mNotificationInfo);

    } catch (JSONException e) {
        Log.e(TAG, "JSON exception");
        e.printStackTrace();
        notificationsReceiver.onError(e);
    }
}

From source file:org.lcog.cyclelane.TripUploader.java

boolean uploadOneTrip(long currentTripId) {
    boolean result = false;

    List<NameValuePair> nameValuePairs;
    try {/*from  w  w  w .  j a  va2 s .com*/
        nameValuePairs = getPostData(currentTripId);
    } catch (JSONException e) {
        e.printStackTrace();
        return result;
    }
    Log.v("PostData", nameValuePairs.toString());

    HttpClient client = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost(postUrl);

    try {
        postRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = client.execute(postRequest);
        String responseString = convertStreamToString(response.getEntity().getContent());
        Log.v("httpResponse", responseString);
        JSONObject responseData = new JSONObject(responseString);
        if (responseData.getString("status").equals("success")) {
            mDb.open();
            mDb.updateTripStatus(currentTripId, TripData.STATUS_SENT);
            mDb.close();
            result = true;
        }
    } catch (IllegalStateException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } catch (JSONException e) {
        e.printStackTrace();
        return false;
    }
    return result;
}

From source file:com.polyvi.xface.view.XWebViewClient.java

/**
 * ?webViewurl?/*from w w  w  . j a  va  2 s  . c o m*/
 *
 * @param url
 */
private void handleExecUrl(String url) {
    try {
        int idx1 = XFACE_EXEC_URL_PREFIX.length();
        int idx2 = url.indexOf('#', idx1 + 1);
        int idx3 = url.indexOf('#', idx2 + 1);
        int idx4 = url.indexOf('#', idx3 + 1);
        if (idx1 == -1 || idx2 == -1 || idx3 == -1 || idx4 == -1) {
            XLog.e(CLASS_NAME, "Could not decode URL command: " + url);
            return;
        }
        String service = url.substring(idx1, idx2);
        String action = url.substring(idx2 + 1, idx3);
        String callbackId = url.substring(idx3 + 1, idx4);
        String jsonArgs = url.substring(idx4 + 1);
        mWebAppView.getOwnerApp().getJSNativeBridge().exec(service, action, callbackId, jsonArgs);
    } catch (JSONException e) {
        XLog.e(CLASS_NAME, "handleExecUrl: JSONException");
        e.printStackTrace();
        return;
    }
}

From source file:cn.code.notes.gtask.remote.GTaskManager.java

private void initGTaskList() throws NetworkFailureException {
    if (mCancelled)
        return;/*from  ww  w . j ava  2 s  .  co m*/
    GTaskClient client = GTaskClient.getInstance();
    try {
        JSONArray jsTaskLists = client.getTaskLists();

        // init meta list first
        mMetaList = null;
        for (int i = 0; i < jsTaskLists.length(); i++) {
            JSONObject object = jsTaskLists.getJSONObject(i);
            String gid = object.getString(GTaskStringUtils.GTASK_JSON_ID);
            String name = object.getString(GTaskStringUtils.GTASK_JSON_NAME);

            if (name.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META)) {
                mMetaList = new TaskList();
                mMetaList.setContentByRemoteJSON(object);

                // load meta data
                JSONArray jsMetas = client.getTaskList(gid);
                for (int j = 0; j < jsMetas.length(); j++) {
                    object = (JSONObject) jsMetas.getJSONObject(j);
                    MetaData metaData = new MetaData();
                    metaData.setContentByRemoteJSON(object);
                    if (metaData.isWorthSaving()) {
                        mMetaList.addChildTask(metaData);
                        if (metaData.getGid() != null) {
                            mMetaHashMap.put(metaData.getRelatedGid(), metaData);
                        }
                    }
                }
            }
        }

        // create meta list if not existed
        if (mMetaList == null) {
            mMetaList = new TaskList();
            mMetaList.setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META);
            GTaskClient.getInstance().createTaskList(mMetaList);
        }

        // init task list
        for (int i = 0; i < jsTaskLists.length(); i++) {
            JSONObject object = jsTaskLists.getJSONObject(i);
            String gid = object.getString(GTaskStringUtils.GTASK_JSON_ID);
            String name = object.getString(GTaskStringUtils.GTASK_JSON_NAME);

            if (name.startsWith(GTaskStringUtils.MIUI_FOLDER_PREFFIX)
                    && !name.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META)) {
                TaskList tasklist = new TaskList();
                tasklist.setContentByRemoteJSON(object);
                mGTaskListHashMap.put(gid, tasklist);
                mGTaskHashMap.put(gid, tasklist);

                // load tasks
                JSONArray jsTasks = client.getTaskList(gid);
                for (int j = 0; j < jsTasks.length(); j++) {
                    object = (JSONObject) jsTasks.getJSONObject(j);
                    gid = object.getString(GTaskStringUtils.GTASK_JSON_ID);
                    Task task = new Task();
                    task.setContentByRemoteJSON(object);
                    if (task.isWorthSaving()) {
                        task.setMetaInfo(mMetaHashMap.get(gid));
                        tasklist.addChildTask(task);
                        mGTaskHashMap.put(gid, task);
                    }
                }
            }
        }
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("initGTaskList: handing JSONObject failed");
    }
}