Example usage for org.json JSONException getLocalizedMessage

List of usage examples for org.json JSONException getLocalizedMessage

Introduction

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

Prototype

public String getLocalizedMessage() 

Source Link

Document

Creates a localized description of this throwable.

Usage

From source file:com.nextgis.firereporter.GetFiresService.java

protected void LoadScanexData() {
    mmoSubscriptions = new HashMap<Long, ScanexSubscriptionItem>();
    File file = new File(getExternalFilesDir(null), SCANEX_FILE);
    String sData = readFromFile(file);
    try {/*  w  ww  . j a  v a 2s.  co  m*/
        JSONObject oJSONRoot = new JSONObject(sData);
        JSONArray jsonArray = oJSONRoot.getJSONArray("subscriptions");

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);

            ScanexSubscriptionItem Item = new ScanexSubscriptionItem(this, jsonObject);
            if (Item.GetId() != -1) {
                mmoSubscriptions.put(Item.GetId(), Item);
            }
        }

        mbHasChanges = true;

    } catch (JSONException e) {
        SendError(e.getLocalizedMessage());
    }
}

From source file:com.nextgis.firereporter.GetFiresService.java

protected void StoreScanexData() {
    if (mbHasChanges == false)
        return;// www  .ja  va2 s .c o m
    try {
        JSONObject oJSONRoot = new JSONObject();
        JSONArray oJSONSubscriptions = new JSONArray();
        oJSONRoot.put("subscriptions", oJSONSubscriptions);
        for (ScanexSubscriptionItem Item : mmoSubscriptions.values()) {
            oJSONSubscriptions.put(Item.getAsJSON());
        }

        File file = new File(getExternalFilesDir(null), SCANEX_FILE);
        writeToFile(file, oJSONRoot.toString());
        mbHasChanges = false;

    } catch (JSONException e) {
        SendError(e.getLocalizedMessage());
    }
}

From source file:com.spoiledmilk.ibikecph.IssuesActivity.java

public void onButtonSendClick(View v) {
    if (currentOption != null) {
        new Thread(new Runnable() {
            @Override/*from  ww w.j  a  va  2s .  c  o m*/
            public void run() {
                JsonNode response = null;
                JSONObject jsonPost = new JSONObject();
                String auth_token = IbikeApplication.getAuthToken();
                try {
                    jsonPost.put("auth_token", auth_token);
                    JSONObject jsonIssue = new JSONObject();
                    jsonIssue.put("route_segment", spinner.getSelectedItem().toString());
                    jsonIssue.put("error_type", currentOption.getText().toString());
                    String comment = "";
                    comment += IbikeApplication.getString("report_from") + "\n";
                    comment += startName + "\n" + startLoc + "\n\n";
                    comment += IbikeApplication.getString("report_to") + "\n";
                    comment += endName + "\n" + endLoc + "\n\n";
                    comment += IbikeApplication.getString("report_reason") + "\n";
                    comment += currentOption.getText().toString() + "\n\n";
                    comment += (currentComment == null ? "" : currentComment.getText().toString()) + "\n\n";
                    comment += spinner.getSelectedItem().toString() + "\n\n";
                    comment += IbikeApplication.getString("report_tbt_instructions") + "\n";
                    for (String turn : turns) {
                        comment += turn + "\n";
                    }
                    jsonIssue.put("comment", comment);
                    jsonPost.put("issue", jsonIssue);
                    response = HttpUtils.postToServer(Config.serverUrl + "/issues", jsonPost);
                    IbikeApplication.getTracker().sendEvent("Report", "Completed", "", (long) 0);
                } catch (JSONException e) {
                    LOG.e(e.getLocalizedMessage());
                } finally {
                    final JsonNode responseTemp = response;
                    IssuesActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            String message = IbikeApplication.getString("Error");
                            if (responseTemp != null && responseTemp.has("info")) {
                                message = responseTemp.get("info").asText();
                                LOG.d("issues response message = " + message);
                            }
                            AlertDialog.Builder builder = new AlertDialog.Builder(IssuesActivity.this);
                            builder.setMessage(message);
                            builder.setPositiveButton("OK", new OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                    finish();
                                    overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
                                }
                            });
                            builder.show();
                        }
                    });

                }
            }
        }).start();

    }
}

From source file:to.networld.fbtosemweb.ws.SIOCWallExporter.java

@Override
public void doGet(HttpServletRequest _request, HttpServletResponse _response)
        throws ServletException, IOException {
    String access_token = SessionHandler.getAccessToken(_request);
    if (access_token == null) {
        access_token = (String) _request.getParameter("access_token");
        if (access_token == null) {
            _response.sendRedirect(".");
            return;
        } else {//from  w  w w. j a v a 2 s .  c om
            access_token = "access_token=" + access_token;
        }
    }

    PrintWriter out = _response.getWriter();

    try {
        FacebookAgentHandler currentAgentHandler = new FacebookAgentHandler(access_token);
        FacebookToSIOC siocFile = new FacebookToSIOC(currentAgentHandler.getFacebookWallFeed());
        _response.setCharacterEncoding("UTF-8");
        _response.setContentType("application/rdf+xml; charset=UTF-8");
        _response.setHeader("Content-Disposition", "attachment; filename=sioc_wall.rdf");
        out.println(siocFile);
    } catch (JSONException e) {
        out.println(e.getLocalizedMessage());
        _response.setStatus(500);
    }
}

From source file:eu.codeplumbers.cosi.services.CosiContactService.java

/**
 * Make remote request to get all calls stored in Cozy
 *//*from w w w .  ja  va 2  s  .c  om*/
public void getRemoteContacts() {
    URL urlO = null;
    try {
        urlO = new URL(designUrl);
        HttpURLConnection conn = (HttpURLConnection) urlO.openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        conn.setRequestProperty("Authorization", authHeader);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");

        // read the response
        int status = conn.getResponseCode();
        InputStream in = null;

        if (status >= HttpURLConnection.HTTP_BAD_REQUEST) {
            in = conn.getErrorStream();
        } else {
            in = conn.getInputStream();
        }

        StringWriter writer = new StringWriter();
        IOUtils.copy(in, writer, "UTF-8");
        String result = writer.toString();

        JSONArray jsonArray = new JSONArray(result);

        if (jsonArray != null) {
            if (jsonArray.length() == 0) {
                EventBus.getDefault()
                        .post(new ContactSyncEvent(SYNC_MESSAGE, "Your Cozy has no contacts stored."));
                Contact.setAllUnsynced();
            } else {
                for (int i = 0; i < jsonArray.length(); i++) {
                    Log.d("Contact", i + "");
                    EventBus.getDefault().post(new ContactSyncEvent(SYNC_MESSAGE,
                            "Reading contacts on Cozy " + (i + 1) + "/" + jsonArray.length() + "..."));
                    try {

                        JSONObject contactJson = jsonArray.getJSONObject(i).getJSONObject("value");
                        Contact contact = Contact.getByRemoteId(contactJson.get("_id").toString());
                        if (contact == null) {
                            contact = Contact.getByName(contactJson.getString("n"));
                            if (contact == null) {
                                contact = new Contact(contactJson);
                            } else {
                                if (contactJson.has("n"))
                                    contact.setN(contactJson.getString("n"));
                                else
                                    contact.setN("");

                                if (contactJson.has("fn"))
                                    contact.setFn(contactJson.getString("fn"));
                                else
                                    contact.setFn("");

                                if (contactJson.has("revision")) {
                                    contact.setRevision(contactJson.getString("revision"));
                                } else {
                                    contact.setRevision(DateUtils.formatDate(new Date().getTime()));
                                }

                                if (contactJson.has("tags")
                                        && !contactJson.getString("tags").equalsIgnoreCase("")) {
                                    contact.setTags(contactJson.getJSONArray("tags").toString());
                                } else {
                                    contact.setTags(new JSONArray().toString());
                                }
                                contact.setPhotoBase64("");
                                contact.setAnniversary("");

                                if (contactJson.has("deviceId")) {
                                    contact.setDeviceId(contactJson.getString("deviceId"));
                                }

                                if (contactJson.has("systemId")) {
                                    contact.setSystemId(contactJson.getString("systemId"));
                                }

                                contact.setRemoteId(contactJson.getString("_id"));

                                if (contactJson.has("_attachments")) {
                                    JSONObject attachment = contactJson.getJSONObject("_attachments");

                                    contact.setAttachments(attachment.toString());

                                    if (attachment.has("picture")) {
                                        JSONObject picture = attachment.getJSONObject("picture");
                                        String attachmentName = new String(
                                                Base64.decode(picture.getString("digest").replace("md5-", ""),
                                                        Base64.DEFAULT));
                                        Log.d("Contact", attachmentName);
                                    }
                                }

                                contact.save();

                                if (contactJson.has("datapoints")) {
                                    JSONArray datapoints = contactJson.getJSONArray("datapoints");

                                    for (int j = 0; j < datapoints.length(); j++) {
                                        JSONObject datapoint = datapoints.getJSONObject(j);
                                        String value = datapoint.getString("value");
                                        ContactDataPoint contactDataPoint = ContactDataPoint.getByValue(contact,
                                                value);

                                        if (contactDataPoint == null && !value.isEmpty()) {
                                            contactDataPoint = new ContactDataPoint();
                                            contactDataPoint.setPref(false);
                                            contactDataPoint.setType(datapoint.getString("type"));
                                            contactDataPoint.setValue(value);
                                            contactDataPoint.setName(datapoint.getString("name"));
                                            contactDataPoint.setContact(contact);
                                            contactDataPoint.save();
                                        }
                                    }
                                }
                            }
                        } else {
                            if (contactJson.has("n"))
                                contact.setN(contactJson.getString("n"));
                            else
                                contact.setN("");

                            if (contactJson.has("fn"))
                                contact.setFn(contactJson.getString("fn"));
                            else
                                contact.setFn("");

                            if (contactJson.has("revision")) {
                                contact.setRevision(contactJson.getString("revision"));
                            } else {
                                contact.setRevision(DateUtils.formatDate(new Date().getTime()));
                            }

                            if (contactJson.has("tags")
                                    && !contactJson.getString("tags").equalsIgnoreCase("")) {
                                contact.setTags(contactJson.getJSONArray("tags").toString());
                            } else {
                                contact.setTags(new JSONArray().toString());
                            }
                            contact.setPhotoBase64("");
                            contact.setAnniversary("");

                            if (contactJson.has("deviceId")) {
                                contact.setDeviceId(contactJson.getString("deviceId"));
                            }

                            if (contactJson.has("systemId")) {
                                contact.setSystemId(contactJson.getString("systemId"));
                            }

                            contact.setRemoteId(contactJson.getString("_id"));

                            if (contactJson.has("_attachments")) {
                                JSONObject attachment = contactJson.getJSONObject("_attachments");

                                contact.setAttachments(attachment.toString());

                                if (attachment.has("picture")) {
                                    JSONObject picture = attachment.getJSONObject("picture");
                                    String attachmentName = new String(Base64.decode(
                                            picture.getString("digest").replace("md5-", ""), Base64.DEFAULT));
                                    Log.d("Contact", attachmentName);
                                }
                            }

                            contact.save();

                            if (contactJson.has("datapoints")) {
                                JSONArray datapoints = contactJson.getJSONArray("datapoints");

                                for (int j = 0; j < datapoints.length(); j++) {
                                    JSONObject datapoint = datapoints.getJSONObject(j);
                                    String value = datapoint.getString("value");
                                    ContactDataPoint contactDataPoint = ContactDataPoint.getByValue(contact,
                                            value);

                                    if (contactDataPoint == null && !value.isEmpty()) {
                                        contactDataPoint = new ContactDataPoint();
                                        contactDataPoint.setPref(false);
                                        contactDataPoint.setType(datapoint.getString("type"));
                                        contactDataPoint.setValue(value);
                                        contactDataPoint.setName(datapoint.getString("name"));
                                        contactDataPoint.setContact(contact);
                                        contactDataPoint.save();
                                    }
                                }
                            }
                        }

                        contact.save();

                        allContacts.add(contact);
                    } catch (JSONException e) {
                        EventBus.getDefault()
                                .post(new ContactSyncEvent(SERVICE_ERROR, e.getLocalizedMessage()));
                        stopSelf();
                    }
                }
            }
        } else {
            errorMessage = new JSONObject(result).getString("error");
            EventBus.getDefault().post(new ContactSyncEvent(SERVICE_ERROR, errorMessage));
            stopSelf();
        }

        in.close();
        conn.disconnect();

    } catch (MalformedURLException e) {
        EventBus.getDefault().post(new ContactSyncEvent(SERVICE_ERROR, e.getLocalizedMessage()));
        stopSelf();
    } catch (ProtocolException e) {
        EventBus.getDefault().post(new ContactSyncEvent(SERVICE_ERROR, e.getLocalizedMessage()));
        stopSelf();
    } catch (IOException e) {
        EventBus.getDefault().post(new ContactSyncEvent(SERVICE_ERROR, e.getLocalizedMessage()));
        stopSelf();
    } catch (JSONException e) {
        EventBus.getDefault().post(new ContactSyncEvent(SERVICE_ERROR, e.getLocalizedMessage()));
        stopSelf();
    }
}