Search in sources :

Example 66 with JSONObject

use of org.json.JSONObject in project solo by b3log.

the class UserMgmtServiceTestCase method updateUser.

/**
     * Update User.
     * 
     * @throws Exception exception
     */
@Test(dependsOnMethods = "addUser")
public void updateUser() throws Exception {
    final UserMgmtService userMgmtService = getUserMgmtService();
    JSONObject requestJSONObject = new JSONObject();
    requestJSONObject.put(User.USER_NAME, "user2 name");
    requestJSONObject.put(User.USER_EMAIL, "test2@gmail.com");
    requestJSONObject.put(User.USER_PASSWORD, "pass2");
    requestJSONObject.put(User.USER_ROLE, Role.ADMIN_ROLE);
    final String id = userMgmtService.addUser(requestJSONObject);
    Assert.assertNotNull(id);
    requestJSONObject.put(Keys.OBJECT_ID, id);
    requestJSONObject.put(User.USER_NAME, "user2 new name");
    userMgmtService.updateUser(requestJSONObject);
    Assert.assertEquals(getUserQueryService().getUser(id).getJSONObject(User.USER).getString(User.USER_NAME), "user2 new name");
    // Do not update password
    requestJSONObject.put(Keys.OBJECT_ID, id);
    requestJSONObject.put(User.USER_NAME, "user2 name");
    requestJSONObject.put(User.USER_EMAIL, "test2@gmail.com");
    requestJSONObject.put(User.USER_PASSWORD, "pass2");
    userMgmtService.updateUser(requestJSONObject);
    Assert.assertEquals(getUserQueryService().getUser(id).getJSONObject(User.USER).getString(User.USER_PASSWORD), MD5.hash("pass2"));
}
Also used : JSONObject(org.json.JSONObject) Test(org.testng.annotations.Test)

Example 67 with JSONObject

use of org.json.JSONObject in project YCSB by brianfrankcooper.

the class RadosClient method read.

@Override
public Status read(String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) {
    byte[] buffer;
    try {
        RadosObjectInfo info = ioctx.stat(key);
        buffer = new byte[(int) info.getSize()];
        ReadOp rop = ioctx.readOpCreate();
        ReadResult readResult = rop.queueRead(0, info.getSize());
        // TODO: more size than byte length possible;
        // rop.operate(key, Rados.OPERATION_NOFLAG); // for rados-java 0.3.0
        rop.operate(key, 0);
        // readResult.raiseExceptionOnError("Error ReadOP(%d)", readResult.getRVal()); // for rados-java 0.3.0
        if (readResult.getRVal() < 0) {
            throw new RadosException("Error ReadOP", readResult.getRVal());
        }
        if (info.getSize() != readResult.getBytesRead()) {
            return new Status("ERROR", "Error the object size read");
        }
        readResult.getBuffer().get(buffer);
    } catch (RadosException e) {
        return new Status("ERROR-" + e.getReturnValue(), e.getMessage());
    }
    JSONObject json = new JSONObject(new String(buffer, java.nio.charset.StandardCharsets.UTF_8));
    Set<String> fieldsToReturn = (fields == null ? json.keySet() : fields);
    for (String name : fieldsToReturn) {
        result.put(name, new StringByteIterator(json.getString(name)));
    }
    return result.isEmpty() ? Status.ERROR : Status.OK;
}
Also used : RadosObjectInfo(com.ceph.rados.jna.RadosObjectInfo) Status(com.yahoo.ycsb.Status) JSONObject(org.json.JSONObject) StringByteIterator(com.yahoo.ycsb.StringByteIterator) ReadResult(com.ceph.rados.ReadOp.ReadResult) RadosException(com.ceph.rados.exceptions.RadosException) ReadOp(com.ceph.rados.ReadOp)

Example 68 with JSONObject

use of org.json.JSONObject in project baker-android by bakerframework.

the class GindActivity method createThumbnails.

public void createThumbnails(final JSONArray jsonArray) {
    Log.d(this.getClass().getName(), "Shelf json contains " + jsonArray.length() + " elements.");
    JSONObject json;
    try {
        this.setContentView(R.layout.activity_gind);
        loadHeader();
        loadBackground();
        if (this.getActionBar() != null) {
            this.getActionBar().show();
            // Modify the action bar to use a custom layout to center the title.
            this.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            this.getActionBar().setCustomView(R.layout.custom_actionbar);
        }
        flowLayout = (FlowLayout) findViewById(R.id.thumbsContainer);
        int length = jsonArray.length();
        for (int i = 0; i < length; i++) {
            json = new JSONObject(jsonArray.getString(i));
            Log.i(this.getClass().getName(), "Parsing JSON object " + json);
            LinearLayout inner = new LinearLayout(this);
            inner.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1));
            inner.setGravity(Gravity.CENTER_HORIZONTAL);
            // Building magazine data
            Date date = sdfInput.parse(json.getString("date"));
            String dateString = sdfOutput.format(date);
            int size = 0;
            if (json.has("size"))
                size = json.getInt("size");
            String encoding = "UTF-8";
            Magazine magazine = new Magazine();
            magazine.setName(new String(json.getString("name").getBytes(encoding), encoding));
            magazine.setTitle(new String(json.getString("title").getBytes(encoding), encoding));
            magazine.setInfo(new String(json.getString("info").getBytes(encoding), encoding));
            magazine.setDate(dateString);
            magazine.setSize(size);
            magazine.setCover(new String(json.getString("cover").getBytes(encoding), encoding));
            magazine.setUrl(new String(json.getString("url").getBytes(encoding), encoding));
            magazine.setStandalone(STANDALONE_MODE);
            if (json.has("liveUrl")) {
                String liveUrl = new String(json.getString("liveUrl").getBytes(encoding), encoding);
                liveUrl = liveUrl.replace("/" + this.getString(R.string.book), "");
                while (liveUrl.endsWith("/")) {
                    liveUrl = liveUrl.substring(0, liveUrl.length() - 1);
                }
                magazine.setLiveUrl(liveUrl);
                Log.d(this.getClass().toString(), "The liveUrl for the magazine " + magazine.getName() + " will be " + liveUrl);
            }
            // Starting the ThumbLayout
            MagazineThumb thumb = new MagazineThumb(this, magazine, thumbnailDownloaderHandler);
            thumb.setId(i + i);
            thumb.init(this, null);
            thumbnailIds.add(thumb.getId());
            if (this.magazineExists(magazine.getName())) {
                thumb.enableReadArchiveActions();
            } else if (STANDALONE_MODE) {
                thumb.enableReadButton();
            }
            // Add layout
            flowLayout.addView(thumb);
        }
        // Start downloading the thumbnails.
        this.downloadNextThumbnail();
        isLoading = false;
    } catch (Exception e) {
        Log.e(this.getClass().getName(), "Error loading the shelf elements.", e);
        //TODO: Notify the user about the issue.
        e.printStackTrace();
    }
}
Also used : MagazineThumb(com.baker.abaker.views.MagazineThumb) JSONObject(org.json.JSONObject) LinearLayout(android.widget.LinearLayout) Date(java.util.Date) Magazine(com.baker.abaker.model.Magazine) JSONException(org.json.JSONException) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 69 with JSONObject

use of org.json.JSONObject in project baker-android by bakerframework.

the class GcmBroadcastReceiver method onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    if (testing == false) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        receiveNotifications = sharedPreferences.getBoolean(Configuration.PREF_RECEIVE_NOTIFICATIONS, true);
        receiveNotificationsDownload = sharedPreferences.getBoolean(Configuration.PREF_RECEIVE_NOTIFICATIONS_DOWNLOAD, true);
        receiveNotificationsDownloadOnlyWifi = sharedPreferences.getBoolean(Configuration.PREF_RECEIVE_NOTIFICATIONS_DOWNLOAD_ONLY_WIFI, true);
        canDownload = !receiveNotificationsDownloadOnlyWifi | Configuration.connectionIsWiFi(context);
    }
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
    messageType = gcm.getMessageType(intent);
    if (!receiveNotifications) {
        processFinishedCode = 1;
        return;
    }
    if (!extras.isEmpty()) {
        // has effect of unparcelling Bundle
        Log.i(this.getClass().toString(), "Received: " + extras.toString());
        /**
             * Filter messages based on message type. Since it is likely that GCM
             * will be extended in the future with new message types, just ignore
             * any message types you're not interested in, or that you don't
             * recognize.
             **/
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
            sendNotification(context, "Error notification", "Send error: " + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
            sendNotification(context, "Deleted messages", "Deleted messages on server: " + extras.toString());
        // If it's a regular GCM message, do some work.
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            try {
                JSONObject json = new JSONObject(extras.getString("notificationData"));
                // Values can be either "standard-notification" or "background-download". This value is required.
                String type = json.getString("type");
                if ("standard-notification".equals(type)) {
                    // A title to show at the notification bar in android. This value is optional.
                    String title = json.has("title") ? json.getString("title") : "";
                    // The message description for the notification to show at the notifications bar in android. This value is optional.
                    String message = json.has("message") ? json.getString("message") : "";
                    this.sendNotification(context, title, message);
                } else if ("background-download".equals(type)) {
                    if (receiveNotificationsDownload) {
                        if (canDownload) {
                            processFinishedCode = 4;
                            if (json.has("issueName")) {
                                // Values can be "latest" or the name of the issue, for example "magazine-12". This value is required.
                                String issueName = json.getString("issueName");
                                Intent gindIntent = new Intent(context, GindActivity.class);
                                gindIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                gindIntent.putExtra("START_DOWNLOAD", issueName);
                                context.startActivity(gindIntent);
                            }
                        } else {
                            processFinishedCode = 3;
                            return;
                        }
                    } else {
                        processFinishedCode = 2;
                        return;
                    }
                }
            } catch (Exception ex) {
                // Do nothing, if it fails we simply do not process the notification.
                Log.e(this.getClass().toString(), ex.getMessage());
            }
        // Post notification of received message.
        //sendNotification(context, extras.getString("collapse_key"), extras.getString("message"));
        }
    }
}
Also used : JSONObject(org.json.JSONObject) SharedPreferences(android.content.SharedPreferences) Bundle(android.os.Bundle) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) GoogleCloudMessaging(com.google.android.gms.gcm.GoogleCloudMessaging)

Example 70 with JSONObject

use of org.json.JSONObject in project baker-android by bakerframework.

the class GindActivity method startDownloadLastContent.

private void startDownloadLastContent(final JSONArray jsonArray) {
    try {
        ArrayList<Date> list = new ArrayList<Date>();
        JSONObject jsonObject;
        Date date;
        for (int i = 0; i < jsonArray.length(); i++) {
            jsonObject = new JSONObject(jsonArray.getString(i));
            String rawDate = jsonObject.getString("date");
            date = sdfInput.parse(rawDate);
            list.add(date);
        }
        Collections.sort(list, new Comparator<Date>() {

            @Override
            public int compare(Date s, Date s2) {
                return s2.compareTo(s);
            }
        });
        for (int i = 0; i < flowLayout.getChildCount(); i++) {
            MagazineThumb thumb = (MagazineThumb) flowLayout.getChildAt(i);
            String dateString = sdfOutput.format(list.get(0));
            if (thumb.getMagazine().getDate().equals(dateString)) {
                if (!this.magazineExists(thumb.getMagazine().getName())) {
                    Log.d(this.getClass().toString(), "Automatically starting download of " + thumb.getMagazine().getName());
                    thumb.startPackageDownload();
                } else {
                    Log.d(this.getClass().toString(), "The magazine with name '" + thumb.getMagazine().getName() + "' already exists.");
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : MagazineThumb(com.baker.abaker.views.MagazineThumb) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) Date(java.util.Date) JSONException(org.json.JSONException) ParseException(java.text.ParseException) IOException(java.io.IOException)

Aggregations

JSONObject (org.json.JSONObject)4075 JSONException (org.json.JSONException)1601 JSONArray (org.json.JSONArray)1156 Test (org.junit.Test)526 IOException (java.io.IOException)456 ArrayList (java.util.ArrayList)402 HashMap (java.util.HashMap)290 Test (org.testng.annotations.Test)175 File (java.io.File)145 Date (java.util.Date)144 ServiceException (org.b3log.latke.service.ServiceException)125 Bundle (android.os.Bundle)109 VolleyError (com.android.volley.VolleyError)104 Map (java.util.Map)96 BufferedReader (java.io.BufferedReader)93 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)92 InputStreamReader (java.io.InputStreamReader)90 List (java.util.List)85 Response (com.android.volley.Response)84 InputStream (java.io.InputStream)73