Search in sources :

Example 61 with JSONException

use of org.json.JSONException in project weiciyuan by qii.

the class FriendsTimeLineTagDao method getGSONMsgList.

public List<TagBean> getGSONMsgList() throws WeiboException {
    String json = getMsgListJson();
    List<TagBean> tagBeanList = new ArrayList<TagBean>();
    try {
        JSONArray array = new JSONArray(json);
        int size = array.length();
        for (int i = 0; i < size; i++) {
            TagBean bean = new TagBean();
            JSONObject jsonObject = array.getJSONObject(i);
            Iterator<String> iterator = jsonObject.keys();
            while (iterator.hasNext()) {
                String key = iterator.next();
                if (key.equalsIgnoreCase("weight")) {
                    String value = jsonObject.optString(key);
                    bean.setWeight(value);
                } else {
                    String value = jsonObject.optString(key);
                    bean.setId(Integer.valueOf(key));
                    bean.setName(value);
                }
            }
            tagBeanList.add(bean);
        }
    } catch (JSONException e) {
        AppLogger.e(e.getMessage());
    }
    return tagBeanList;
}
Also used : JSONObject(org.json.JSONObject) TagBean(org.qii.weiciyuan.bean.TagBean) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 62 with JSONException

use of org.json.JSONException in project platformlayer by platformlayer.

the class ServiceAuthorizationResource method createService.

// We deliberately don't support this at the moment... it's quite restrictive on our data store
// @GET
// @Produces({ APPLICATION_XML, APPLICATION_JSON })
// public <T> ServiceAuthorizationCollection getAll() {
// List<ServiceAuthorization> items = authorizationRepository.getByAccountId(getAccountId());
// ServiceAuthorizationCollection collection = new ServiceAuthorizationCollection();
// collection.items = items;
// return collection;
// }
@POST
@Consumes({ XML, JSON })
@Produces({ XML, JSON })
public <T> ServiceAuthorization createService(final ServiceAuthorization authorization) throws OpsException, RepositoryException {
    ServiceType serviceType = getServiceType();
    authorization.serviceType = serviceType.getKey();
    final ServiceProvider serviceProvider = opsSystem.getServiceProvider(serviceType);
    if (serviceProvider == null) {
        log.warn("Unknown serviceProvider: " + serviceType);
        throw new WebApplicationException(404);
    }
    String data = authorization.data;
    if (Strings.isNullOrEmpty(data)) {
        throw new IllegalArgumentException("Data is required");
    }
    data = data.trim();
    if (data.startsWith("{")) {
        // Convert to XML
        SettingCollection settings = new SettingCollection();
        settings.items = Lists.newArrayList();
        // We presume it's a simple map of keys and values
        try {
            JSONObject json = new JSONObject(data);
            @SuppressWarnings("unchecked") Iterator<String> keys = json.keys();
            while (keys.hasNext()) {
                String key = keys.next();
                String value = json.getString(key);
                Setting setting = new Setting();
                setting.key = key;
                setting.value = value;
                settings.items.add(setting);
            }
        } catch (JSONException e) {
            throw new IllegalArgumentException("Error parsing data", e);
        }
        JaxbHelper jaxbHelper = JaxbHelper.get(SettingCollection.class);
        String xml;
        try {
            xml = jaxbHelper.marshal(settings, false);
        } catch (JAXBException e) {
            throw new IllegalArgumentException("Error converting JSON to XML", e);
        }
        authorization.data = xml;
    }
    // Authentication authentication = getAuthentication();
    //
    // OpsContextBuilder opsContextBuilder = opsSystem.getInjector().getInstance(OpsContextBuilder.class);
    // final OpsContext opsContext = opsContextBuilder.buildOpsContext(serviceType, authentication, false);
    //
    // OpsContext.runInContext(opsContext, new CheckedCallable<Object, Exception>() {
    // @Override
    // public Object call() throws Exception {
    // serviceProvider.validateAuthorization(authorization);
    // return null;
    // }
    // });
    // serviceProvider.validateAuthorization(authorization);
    ServiceAuthorization created = authorizationRepository.createAuthorization(getProject(), authorization);
    // For security, never return the data
    created.data = null;
    return created;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) SettingCollection(org.platformlayer.xaas.model.SettingCollection) Setting(org.platformlayer.xaas.model.Setting) JAXBException(javax.xml.bind.JAXBException) JSONException(org.json.JSONException) ServiceAuthorization(org.platformlayer.xaas.model.ServiceAuthorization) JSONObject(org.json.JSONObject) ServiceType(org.platformlayer.ids.ServiceType) ServiceProvider(org.platformlayer.xaas.services.ServiceProvider) JaxbHelper(org.platformlayer.xml.JaxbHelper) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 63 with JSONException

use of org.json.JSONException in project platformlayer by platformlayer.

the class ActionsResource method doActionJson.

@POST
@Consumes({ JSON })
@Produces({ XML, JSON })
public JobData doActionJson(String json) throws IOException, RepositoryException, OpsException {
    JSONObject jsonObject;
    String actionType = null;
    try {
        jsonObject = new JSONObject(json);
        Object typeObject = jsonObject.remove("type");
        if (typeObject != null) {
            actionType = typeObject.toString();
        }
        // Remove type attribute
        json = jsonObject.toString();
    } catch (JSONException e) {
        throw new IllegalArgumentException("Malformed JSON", e);
    }
    if (Strings.isNullOrEmpty(actionType)) {
        throw new IllegalArgumentException("Must pass type attribute");
    }
    Map<String, Class<? extends Action>> actionMap = Maps.newHashMap();
    actionMap.put("configureaction", ConfigureAction.class);
    actionMap.put("validateaction", ValidateAction.class);
    // actionMap.put("deleteaction", DeleteAction.class);
    actionMap.put("backupaction", BackupAction.class);
    for (Class<? extends Action> action : getServiceProvider().getActions()) {
        String key = action.getSimpleName().toLowerCase();
        actionMap.put(key, action);
    }
    Class<? extends Action> actionClass = actionMap.get(actionType.toLowerCase());
    if (actionClass == null) {
        throw new IllegalArgumentException("Unknown action: " + actionType);
    }
    Action action = jsonMapper.readItem(actionClass, json);
    return doAction(action);
}
Also used : ValidateAction(org.platformlayer.core.model.ValidateAction) ConfigureAction(org.platformlayer.core.model.ConfigureAction) BackupAction(org.platformlayer.core.model.BackupAction) Action(org.platformlayer.core.model.Action) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 64 with JSONException

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

the class AutomaticActivity method beginTest.

private void beginTest() {
    mFinalCallbacks.add(new FinalCallback() {

        @Override
        void report(String name, float value) {
            Log.d(LOG_TAG, name + " " + value);
        }

        ;
    });
    File inputFile = new File(Environment.getExternalStorageDirectory(), "CanvasCompareInput.json");
    if (inputFile.exists() && inputFile.canRead() && inputFile.length() > 0) {
        try {
            FileInputStream inputStream = new FileInputStream(inputFile);
            Log.d(LOG_TAG, "Parsing input file...");
            StringBuffer content = new StringBuffer((int) inputFile.length());
            byte[] buffer = new byte[1024];
            while (inputStream.read(buffer) != -1) {
                content.append(new String(buffer));
            }
            mInputJson = new JSONObject(content.toString());
            inputStream.close();
            Log.d(LOG_TAG, "Parsed input file with " + mInputJson.length() + " entries");
        } catch (JSONException e) {
            Log.e(LOG_TAG, "error parsing input json", e);
        } catch (IOException e) {
            Log.e(LOG_TAG, "error reading input json from sd", e);
        }
    }
    mOutputJson = new JSONObject();
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 65 with JSONException

use of org.json.JSONException in project androidquery by androidquery.

the class AjaxLoadingActivity method cookieCb.

public void cookieCb(String url, JSONObject jo, AjaxStatus status) {
    JSONObject result = new JSONObject();
    try {
        result.putOpt("cookies", jo.optJSONObject("cookies"));
    } catch (JSONException e) {
    }
    showResult(result, status);
}
Also used : JSONObject(org.json.JSONObject) 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