Example usage for org.json JSONArray getString

List of usage examples for org.json JSONArray getString

Introduction

In this page you can find the example usage for org.json JSONArray getString.

Prototype

public String getString(int index) throws JSONException 

Source Link

Document

Get the string associated with an index.

Usage

From source file:cn.ucai.fulicenter.adapter.MessageAdapter.java

private void setRobotMenuMessageLayout(LinearLayout parentView, JSONArray jsonArr) {
    try {//from   w w w . j av a2 s. c om
        parentView.removeAllViews();
        for (int i = 0; i < jsonArr.length(); i++) {
            final String itemStr = jsonArr.getString(i);
            final TextView textView = new TextView(context);
            textView.setText(itemStr);
            textView.setTextSize(15);
            try {
                XmlPullParser xrp = context.getResources().getXml(R.drawable.menu_msg_text_color);
                textView.setTextColor(ColorStateList.createFromXml(context.getResources(), xrp));
            } catch (Exception e) {
                e.printStackTrace();
            }
            textView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    ((ChatActivity) context).sendText(itemStr);
                }
            });
            LinearLayout.LayoutParams llLp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            llLp.bottomMargin = DensityUtil.dip2px(context, 3);
            llLp.topMargin = DensityUtil.dip2px(context, 3);
            parentView.addView(textView, llLp);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

}

From source file:com.xpple.jahoqy.adapter.MessageAdapter.java

private void setRobotMenuMessageLayout(LinearLayout parentView, JSONArray jsonArr) {
    try {// w ww .j ava 2s  . c  o m
        parentView.removeAllViews();
        for (int i = 0; i < jsonArr.length(); i++) {
            final String itemStr = jsonArr.getString(i);
            final TextView textView = new TextView(context);
            textView.setText(itemStr);
            textView.setTextSize(15);
            try {
                XmlPullParser xrp = context.getResources().getXml(R.drawable.menu_msg_text_color);
                textView.setTextColor(ColorStateList.createFromXml(context.getResources(), xrp));
            } catch (Exception e) {
                e.printStackTrace();
            }
            textView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    ((ChatActivity) context).sendText(itemStr);
                }
            });
            LinearLayout.LayoutParams llLp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            llLp.bottomMargin = DensityUtil.dip2px(context, 3);
            llLp.topMargin = DensityUtil.dip2px(context, 3);
            parentView.addView(textView, llLp);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

}

From source file:com.vladstirbu.cordova.CDVInstagramVideoPlugin.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

    this.cbContext = callbackContext;

    if (action.equals("share")) {
        String imageString = args.getString(0);
        String captionString = args.getString(1);
        this.share(imageString, captionString);
        return true;
    } else if (action.equals("isInstalled")) {
        this.isInstalled();
    } else {/*from   w w  w. ja v a2s .c  o  m*/
        callbackContext.error("Invalid Action");
    }
    return false;
}

From source file:com.extjs.JSBuilder2.java

private static void createTargetsWithDeps() {
    try {//from  ww  w  .j a  va2s  . c  om
        int len = pkgs.length();
        for (int i = 0; i < len; i++) {
            /* Build pkg and include file deps */
            JSONObject pkg = pkgs.getJSONObject(i);

            /* if we need to includeDeps, they shoudl already be built. */
            if (pkg.optBoolean("includeDeps", false)) {
                String targFileName = pkg.getString("file");
                if (targFileName.contains(".js")) {
                    targFileName = FileHelper.insertFileSuffix(pkg.getString("file"), debugSuffix);
                }

                if (verbose) {
                    System.out.format("Building the '%s' package as '%s'%n", pkg.getString("name"),
                            targFileName);
                    System.out.println("This package is built by included dependencies.");
                }

                /* create file and write out header */
                File targetFile = new File(deployDir.getCanonicalPath() + File.separatorChar + targFileName);
                outputFiles.add(targetFile);
                targetFile.getParentFile().mkdirs();
                FileHelper.writeStringToFile("", targetFile, false);

                /* get necessary pkg includes for this specific package */
                JSONArray pkgDeps = pkg.getJSONArray("pkgDeps");
                int pkgDepsLen = pkgDeps.length();
                if (verbose) {
                    System.out.format("- There are %d package include(s).%n", pkgDepsLen);
                }

                /* loop over file includes */
                for (int j = 0; j < pkgDepsLen; j++) {
                    /* open each file, read into string and append to target */
                    String pkgDep = pkgDeps.getString(j);
                    if (verbose) {
                        System.out.format("- - %s%n", pkgDep);
                    }

                    String nameWithorWithoutSuffix = pkgDep;
                    if (pkgDep.contains(".js")) {
                        nameWithorWithoutSuffix = FileHelper.insertFileSuffix(pkgDep, debugSuffix);
                    }

                    String subFileName = deployDir.getCanonicalPath() + File.separatorChar
                            + nameWithorWithoutSuffix;
                    File subFile = new File(subFileName);
                    String tempString = FileHelper.readFileToString(subFile);
                    FileHelper.writeStringToFile(tempString, targetFile, true);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("Failed to create target with package dependencies.");
    }
}

From source file:tk.vigaro.helix.config.ConfigurationEsperNet.java

public ConfigurationEsperNet() throws IllegalAccessException, InstantiationException, IOException {
    this.setName("true".equals(System.getProperty("helix.isDebug"))
            ? Helix.properties.getProperty("irc.nickname") + "|debug"
            : Helix.properties.getProperty("irc.nickname"));
    this.setFinger("VBot");
    this.setVersion("VBot");
    this.setRealName("VBot");
    this.setAutoNickChange(true);
    this.setLogin(Helix.properties.getProperty("irc.nickserv.login"));
    this.setNickservPassword(Helix.properties.getProperty("irc.nickserv.pw"));
    this.setServer("irc.esper.net", 6697);
    this.setSocketFactory(new UtilSSLSocketFactory().trustAllCertificates());
    this.setAutoReconnect(true);
    JSONArray chans = new JSONArray(Helix.properties.getProperty("irc.channels"));
    for (int i = 0; i < chans.length(); i++)
        this.addAutoJoinChannel(chans.getString(i));
    this.setListenerManager(Helix.backgroundListenerManager);
    ClassPath classPath = ClassPath.from(Thread.currentThread().getContextClassLoader());
    for (ClassPath.ClassInfo classInfo : classPath.getTopLevelClasses("tk.vigaro.helix.listener")) {
        this.addListener(((Class<? extends ListenerAdapter>) classInfo.load()).newInstance());
    }/*from ww  w.  ja  v a2  s.  c om*/
    for (ClassPath.ClassInfo classInfo : classPath.getTopLevelClasses("tk.vigaro.helix.backgroundlistener")) {
        Helix.backgroundListenerManager
                .addListener(((Class<? extends ListenerAdapter>) classInfo.load()).newInstance(), true);
    }
}

From source file:com.tcs.base64.Base64ImagePlugin.java

@Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {
    boolean result = false;
    Log.v(TAG, "execute: action=" + action);
    //        Context context = getContext();
    if (!action.equals("saveImage")) {

        callbackContext.error("Invalid action : " + action);
        result = false;/*from  w ww.jav a2s  . c o m*/
    }

    try {
        Log.v(TAG, data.getString(0));
        Log.v(TAG, data.getJSONObject(1).toString());
        String b64String = data.getString(0);
        if (b64String.startsWith("data:image")) {
            b64String = b64String.substring(22);
        } else {
            b64String = data.getString(0);
        }
        JSONObject params = data.getJSONObject(1);

        //Optional parameter
        String filename = params.has("filename") ? params.getString("filename") + ".png"
                : "b64Image_" + System.currentTimeMillis() + ".png";
        String storagetype = params.has("externalStorage") ? Environment.getExternalStorageDirectory() + ""
                : getApplicationContext().getFilesDir().getAbsolutePath();
        callbackContext.error("external ==" + Environment.getExternalStorageDirectory());
        String folder = params.has("folder") ? params.getString("folder") : storagetype + "/Pictures";

        Boolean overwrite = params.has("overwrite") ? params.getBoolean("overwrite") : false;

        result = this.saveImage(b64String, filename, folder, overwrite, callbackContext);

    } catch (JSONException e) {
        Log.v(TAG, e.getMessage());
        callbackContext.error("Exception :" + e.getMessage());
        result = false;
    }
    return result;
}

From source file:com.suarez.cordova.mapsforge.MapsforgePlugin.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if ("global-status".equals(action)) {
        this.status();
        callbackContext.success();//from   w ww  . j a  va  2  s  .  c o m
        return true;
    } else if (action.contains("native-")) {
        if ("native-set-center".equals(action)) {
            try {
                MapsforgeNative.INSTANCE.setCenter(args.getDouble(0), args.getDouble(1));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-set-zoom".equals(action)) {
            try {
                MapsforgeNative.INSTANCE.setZoom(Byte.parseByte(args.getString(0)));
                callbackContext.success();
            } catch (NumberFormatException nfe) {
                callbackContext.error("Incorrect argument format. Should be: (byte zoom)");
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-show".equals(action)) {
            try {
                MapsforgeNative.INSTANCE.show();
                callbackContext.success();
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }
            return true;
        } else if ("native-hide".equals(action)) {
            MapsforgeNative.INSTANCE.hide();
            return true;
        } else if ("native-marker".equals(action)) {
            try {
                Activity context = this.cordova.getActivity();

                int markerId = context.getResources().getIdentifier(args.getString(0), "drawable",
                        context.getPackageName());
                if (markerId == 0) {
                    Log.i(MapsforgePlugin.TAG, "Marker not found...using default marker: marker_green");
                    markerId = context.getResources().getIdentifier("marker_green", "drawable",
                            context.getPackageName());
                }

                int markerKey = MapsforgeNative.INSTANCE.addMarker(markerId, args.getDouble(1),
                        args.getDouble(2));
                callbackContext.success(markerKey);
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-polyline".equals(action)) {

            try {
                JSONArray points = args.getJSONArray(2);

                if (points.length() % 2 != 0)
                    throw new JSONException("Invalid array of coordinates. Length should be multiple of 2");

                int polylineKey = MapsforgeNative.INSTANCE.addPolyline(args.getInt(0), args.getInt(1), points);

                callbackContext.success(polylineKey);
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-delete-layer".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.deleteLayer(args.getInt(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-initialize".equals(action)) {

            try {

                MapsforgeNative.createInstance(this.cordova.getActivity(), args.getString(0), args.getInt(1),
                        args.getInt(2));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (IllegalArgumentException e) {
                callbackContext.error(e.getMessage());
            } catch (IOException e) {
                callbackContext.error(e.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-set-max-zoom".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.setMaxZoom(Byte.parseByte(args.getString(0)));
                callbackContext.success();
            } catch (NumberFormatException nfe) {
                callbackContext.error("Incorrect argument format. Should be: (byte zoom)");
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-set-min-zoom".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.setMinZoom(Byte.parseByte(args.getString(0)));
                callbackContext.success();
            } catch (NumberFormatException nfe) {
                callbackContext.error("Incorrect argument format. Should be: (byte zoom)");
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-show-controls".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.setBuiltInZoomControls(args.getBoolean(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-clickable".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.setClickable(args.getBoolean(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-show-scale".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.showScaleBar(args.getBoolean(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-destroy-cache".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.destroyCache(args.getBoolean(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            }

            return true;
        } else if ("native-map-path".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.setMapFilePath(args.getString(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (IllegalArgumentException iae) {
                callbackContext.error(iae.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-cache-name".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.setCacheName(args.getString(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-theme-path".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.setRenderThemePath(args.getString(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (IllegalArgumentException e) {
                callbackContext.error(e.getMessage());
            } catch (IOException e) {
                callbackContext.error(e.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-stop".equals(action)) {
            try {
                MapsforgeNative.INSTANCE.onStop();
                callbackContext.success();
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-start".equals(action)) {
            try {
                MapsforgeNative.INSTANCE.onStart();
                callbackContext.success();
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-destroy".equals(action)) {
            try {
                MapsforgeNative.INSTANCE.onDestroy();
                callbackContext.success();
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-online".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.setOnline(args.getString(0), args.getString(1), args.getString(2),
                        args.getString(3), args.getInt(4));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-offline".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.setOffline(args.getString(0), args.getString(1));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (IllegalArgumentException e) {
                callbackContext.error(e.getMessage());
            } catch (IOException e) {
                callbackContext.error(e.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        }
    } else if (action.contains("cache-")) {
        if ("cache-get-tile".equals(action)) {

            try {
                final long x = args.getLong(0);
                final long y = args.getLong(1);
                final byte z = Byte.parseByte(args.getString(2));
                final CallbackContext callbacks = callbackContext;

                cordova.getThreadPool().execute(new Runnable() {
                    public void run() {
                        try {
                            String path = MapsforgeCache.INSTANCE.getTilePath(x, y, z);
                            callbacks.success(path);
                        } catch (IOException e) {
                            callbacks.error(e.getMessage());
                        } catch (Exception e) {
                            callbacks.error(e.getMessage());
                        }
                    }
                });
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (NumberFormatException nfe) {
                callbackContext.error(nfe.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-initialize".equals(action)) {

            try {
                MapsforgeCache.createInstance(cordova.getActivity(), args.getString(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (IllegalArgumentException e) {
                callbackContext.error(e.getMessage());
            } catch (IOException e) {
                callbackContext.error(e.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-map-path".equals(action)) {

            try {
                final String mapFile = args.getString(0);
                final CallbackContext callbacks = callbackContext;

                cordova.getThreadPool().execute(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            MapsforgeCache.INSTANCE.setMapFilePath(mapFile);
                            callbacks.success();
                        } catch (IllegalArgumentException e) {
                            callbacks.error(e.getMessage());
                        } catch (FileNotFoundException e) {
                            callbacks.error(e.getMessage());
                        } catch (Exception e) {
                            callbacks.error(e.getMessage());
                        }
                    }
                });
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-max-size".equals(action)) {

            try {
                MapsforgeCache.INSTANCE.setMaxCacheSize(args.getInt(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-max-age".equals(action)) {

            try {
                MapsforgeCache.INSTANCE.setMaxCacheAge(args.getLong(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-cleaning-trigger".equals(action)) {

            try {
                MapsforgeCache.INSTANCE.setCleanCacheTrigger(args.getInt(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-enabled".equals(action)) {

            try {
                MapsforgeCache.INSTANCE.setCacheEnabled(args.getBoolean(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-external".equals(action)) {

            try {
                MapsforgeCache.INSTANCE.setExternalCache(args.getBoolean(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-name".equals(action)) {

            try {
                MapsforgeCache.INSTANCE.setCacheName(args.getString(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-tile-size".equals(action)) {

            try {
                MapsforgeCache.INSTANCE.setTileSize(args.getInt(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-clean-destroy".equals(action)) {

            try {
                MapsforgeCache.INSTANCE.setCleanOnDestroy(args.getBoolean(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-theme-path".equals(action)) {

            try {
                final CallbackContext callbacks = callbackContext;
                final String themePath = args.getString(0);

                cordova.getThreadPool().execute(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            MapsforgeCache.INSTANCE.setRenderTheme(themePath);
                            callbacks.success();
                        } catch (IllegalArgumentException e) {
                            callbacks.error(e.getMessage());
                        } catch (FileNotFoundException e) {
                            callbacks.error(e.getMessage());
                        } catch (Exception e) {
                            callbacks.error(e.getMessage());
                        }
                    }
                });
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-screen-ratio".equals(action)) {

            try {
                MapsforgeCache.INSTANCE.setScreenRatio(Float.parseFloat(args.getString(0)));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (NumberFormatException nfe) {
                callbackContext.error(nfe.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-overdraw".equals(action)) {

            try {
                MapsforgeCache.INSTANCE.setOverdrawFactor(Float.parseFloat(args.getString(0)));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (NumberFormatException nfe) {
                callbackContext.error(nfe.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-destroy".equals(action)) {
            try {
                MapsforgeCache.INSTANCE.onDestroy();
                callbackContext.success();
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        }
    }
    return false; // Returning false results in a "MethodNotFound" error.
}

From source file:com.rathravane.drumlin.examples.accounts.exampleAcctPersistence.java

@Override
public Collection<DrumlinApiKey> loadApiKeysForUser(String userId) throws DrumlinAccountsException {
    JSONArray apiKeys = null;

    final recordInfo ri = readIndexedObject(kIndex_UserList, userId, kValue_User);
    if (ri != null) {
        apiKeys = ri.record.optJSONArray(kField_ApiKey);
    }/*from   w  w  w.j  a v  a  2 s.co  m*/

    final LinkedList<DrumlinApiKey> result = new LinkedList<DrumlinApiKey>();
    if (apiKeys != null) {
        for (int i = 0; i < apiKeys.length(); i++) {
            try {
                final DrumlinApiKey a = loadApiKey(apiKeys.getString(i));
                if (a != null) {
                    result.add(a);
                }
            } catch (JSONException e) {
                log.warn("Couldn't load string " + i + " from API key list on user " + userId + ".");
            }
        }
    }
    return result;
}

From source file:de.joinout.criztovyl.tools.json.JSONMap.java

/**
 * Creates a new {@link JSONMap} from a {@link JSONObject}.
 * @param json the JSON data//from ww  w .jav a 2 s .  co  m
 * @param keyJ the {@link JSONCreator} for the keys
 * @param valJ the {@link JSONCreator} for the values
 */
public JSONMap(JSONObject json, JSONCreator<K> keyJ, JSONCreator<V> valJ) {

    //Set up variables
    this.json = json;
    map = new HashMap<>();

    //Check whether key can be string
    if (keyJ.canBeString()) { //If so, add as string

        //Iterate over map names.
        //If map is empty, names will be null so create an empty array if names are null.
        for (String key : new JSONStringArrayIterator(json.names() == null ? new JSONArray() : json.names()))

            //If value also can be a string, but as one
            map.put(keyJ.fromString(key), valJ.canBeString() ? valJ.fromString(json.getString(key))
                    : valJ.fromJSON(json.getJSONObject(key)));
    } else { //If not, put the JSON-representation

        //Set up arrays of keys and values
        JSONArray keys = json.getJSONArray(KEYS);
        JSONArray values = json.getJSONArray(VALUES);

        //Iterate over key array and take values from the arrays
        for (int i = 0; i < keys.length(); i++)

            //If value can be a string, load as one
            map.put(keyJ.fromJSON(keys.getJSONObject(i)),
                    valJ.canBeString() ? valJ.fromString(values.getString(i))
                            : valJ.fromJSON(values.getJSONObject(i)));
    }
}

From source file:at.alladin.rmbt.android.util.ControlServerConnection.java

private JSONArray sendRequest(final URI hostUrl, final JSONObject requestData, final String fieldName) {
    // getting JSON string from URL
    //Log.d(DEBUG_TAG, "request to "+ hostUrl);       
    final JSONObject response = jParser.sendJSONToUrl(hostUrl, requestData);

    if (response != null)
        try {/*from  w  w w  . j  ava  2  s . com*/
            final JSONArray errorList = response.optJSONArray("error");

            if (errorList == null || errorList.length() == 0) {
                return getResponseField(response, fieldName);
            } else {
                hasError = true;
                for (int i = 0; i < errorList.length(); i++) {

                    if (i > 0)
                        errorMsg += "\n";
                    errorMsg += errorList.getString(i);
                }

                System.out.println(errorMsg);

                //return getResponseField(response, fieldName);
            }

            // }
        } catch (final JSONException e) {
            hasError = true;
            errorMsg = "Error parsing server response";
            e.printStackTrace();
        }
    else {
        hasError = true;
        errorMsg = "No response";
    }

    return null;

}