List of usage examples for org.json JSONObject has
public boolean has(String key)
From source file:produvia.com.scanner.LoginActivity.java
@Override public void onTaskCompleted(final int flag, final JSONObject data) { runOnUiThread(new Runnable() { @Override/*from www . j a v a2 s . c om*/ public void run() { try { JSONObject json = data; if (json.getBoolean("success")) { /************************************************************************** * The user has been logged in - let's move on to the lights list activity **************************************************************************/ Intent intent = new Intent(LoginActivity.this, DevicesActivity.class); startActivity(intent); finish(); } else { /************************************************************************** * Sign in wasn't successful: **************************************************************************/ showLoginView(); if (json.has("info")) Toast.makeText(LoginActivity.this, json.getString("info"), Toast.LENGTH_LONG).show(); } } catch (Exception e) { Toast.makeText(LoginActivity.this, e.getMessage(), Toast.LENGTH_LONG).show(); } } }); }
From source file:com.df.kia.carCheck.VehicleInfoLayout.java
/** * ???/*from ww w . j a v a 2 s. c om*/ * ?? * @param procedures * @param seriesId * @param modelId */ public void fillInData(JSONObject procedures, String seriesId, String modelId, OnUiUpdated listener) { try { mUiUpdatedCallback = listener; // setEditViewText(rootView, R.id.vin_edit, procedures.getString("vin")); // setEditViewText(rootView, R.id.engineSerial_edit, procedures.getString("engineSerial")); // setEditViewText(rootView, R.id.plateNumber_edit, procedures.getString("plateNumber")); // setEditViewText(rootView, R.id.licenseModel_edit, procedures.getString("licenseModel")); // setEditViewText(rootView, R.id.vehicleType_edit, procedures.getString("vehicleType")); // setEditViewText(rootView, R.id.mileage_edit, procedures.getString("mileage")); // setEditViewText(rootView, R.id.exteriorColor_edit, procedures.getString("exteriorColor")); // setEditViewText(rootView, R.id.regDate_edit, procedures.getString("regDate")); // setEditViewText(rootView, R.id.builtDate_edit, procedures.getString("builtDate")); setEditViewText(rootView, R.id.vin_edit, procedures.getString("vin")); if (procedures.has("engineSerial")) setEditViewText(rootView, R.id.engineSerial_edit, procedures.getString("engineSerial")); else setEditViewText(rootView, R.id.engineSerial_edit, procedures.getString("engineno")); if (procedures.has("plateNumber")) setEditViewText(rootView, R.id.plateNumber_edit, procedures.getString("plateNumber")); else setEditViewText(rootView, R.id.plateNumber_edit, procedures.getString("license")); if (procedures.has("licenseModel")) setEditViewText(rootView, R.id.licenseModel_edit, procedures.getString("licenseModel")); else setEditViewText(rootView, R.id.licenseModel_edit, procedures.getString("model2")); if (procedures.has("vehicleType")) setEditViewText(rootView, R.id.vehicleType_edit, procedures.getString("vehicleType")); else setEditViewText(rootView, R.id.vehicleType_edit, procedures.getString("licensetype")); if (procedures.has("mileage")) setEditViewText(rootView, R.id.mileage_edit, procedures.getString("mileage")); else setEditViewText(rootView, R.id.mileage_edit, procedures.getString("mileage")); if (procedures.has("exteriorColor")) setEditViewText(rootView, R.id.exteriorColor_edit, procedures.getString("exteriorColor")); else setEditViewText(rootView, R.id.exteriorColor_edit, procedures.getString("color")); if (procedures.has("regDate")) setEditViewText(rootView, R.id.regDate_edit, procedures.getString("regDate")); else setEditViewText(rootView, R.id.regDate_edit, procedures.getString("regdate")); if (procedures.has("builtDate")) setEditViewText(rootView, R.id.builtDate_edit, procedures.getString("builtDate")); else setEditViewText(rootView, R.id.builtDate_edit, procedures.getString("leavefactorydate")); // ?? Country country = null; Brand brand = null; Manufacturer manufacturer = null; Series series = null; Model model = null; boolean found = false; for (Country country1 : vehicleModel.getCountries()) { for (Brand brand1 : country1.brands) { for (Manufacturer manufacturer1 : brand1.manufacturers) { for (Series series1 : manufacturer1.serieses) { if (series1.id.equals(seriesId)) { manufacturer = manufacturer1; brand = brand1; country = country1; series = series1; model = series.getModelById(modelId); found = true; break; } } if (found) break; } if (found) break; } if (found) break; } mCarSettings.setCountry(country); mCarSettings.setBrand(brand); mCarSettings.setManufacturer(manufacturer); mCarSettings.setSeries(series); mCarSettings.setModel(model); getCarSettingsFromServer(seriesId + "," + modelId); } catch (JSONException e) { e.printStackTrace(); } }
From source file:de.kp.ames.web.core.search.SearcherImpl.java
public String search(String request, String start, String limit) throws Exception { /*/*from w w w.ja v a 2 s .c o m*/ * Build Apache Solr query */ JSONObject jQuery = new JSONObject(request); String term = jQuery.has(JsonConstants.J_TERM) ? jQuery.getString(JsonConstants.J_TERM) : "*"; String fp = setFacets(jQuery); SolrQuery query = new SolrQuery(); if (fp != null) query.addFilterQuery(fp); /* * Paging support from Apache Solr */ int s = Integer.valueOf(start); int r = Integer.valueOf(limit); query.setStart(s); query.setRows(r); String qs = term + " OR " + SearchConstants.TERMS_FIELD + ":" + term; query.setQuery(qs); QueryResponse response = solrProxy.executeQuery(query); SolrDocumentList docs = response.getResults(); long total = docs.getNumFound(); /* * Sort search result */ StringCollector collector = new StringCollector(); Iterator<SolrDocument> iter = docs.iterator(); while (iter.hasNext()) { int pos = -1; SolrDocument doc = iter.next(); JSONObject jDoc = new JSONObject(); /* * Identifier */ String id = (String) doc.getFieldValue(SearchConstants.S_ID); jDoc.put(JsonConstants.J_ID, id); /* * Name */ String name = (String) doc.getFieldValue(SearchConstants.S_NAME); jDoc.put(JsonConstants.J_NAME, name); /* * Source */ String source = (String) doc.getFieldValue(SearchConstants.S_FACET); pos = source.lastIndexOf(":"); jDoc.put(JsonConstants.J_FACET, source.substring(pos + 1)); /* * Description */ String desc = (String) doc.getFieldValue(SearchConstants.S_DESC); desc = (desc == null) ? FncMessages.NO_DESCRIPTION_DESC : desc; jDoc.put(JsonConstants.J_DESC, desc); collector.put(name, jDoc); } /* * Render result */ JSONArray jArray = new JSONArray(collector.values()); return renderer.createGrid(jArray, total); }
From source file:de.kp.ames.web.core.search.SearcherImpl.java
/** * A helper method to retrieve requested * facets from a JSON-based query object * /*from www. j ava2s .c om*/ * @param jQuery * @return * @throws Exception */ protected String setFacets(JSONObject jQuery) throws Exception { String fp = null; if (jQuery.has(JsonConstants.J_FACET)) { /* * Externally selected facets are mapped * onto a filter query */ fp = ""; JSONArray jFacets = jQuery.getJSONArray(JsonConstants.J_FACET); for (int i = 0; i < jFacets.length(); i++) { JSONObject jFacet = jFacets.getJSONObject(i); String field = jFacet.getString(JsonConstants.J_FIELD); String value = jFacet.getString(JsonConstants.J_VALUE); fp += "+" + field + ":\"" + value + "\""; } } return fp; }
From source file:com.nikitaend.instafeed.sola.instagram.InstagramSession.java
/** * Finds and returns a user with the given id. Throws an InstagramException * if none is found or the user with that id cannot be accessed * /*from w w w . j av a 2s.c om*/ * @param userId * id of the user * @return The user with the id passed */ public User getUserById(int userId) throws Exception { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("user_id", userId); String uri = uriConstructor.constructUri(UriFactory.Users.GET_DATA, map, true); JSONObject userObject = (new GetMethod(uri).call()).getJSON(); if (userObject.has("data")) { return new User(userObject.getJSONObject("data"), getAccessToken()); } else { throw new InstagramException("User with id = " + userId + " cannot be accessed" + " or may not exist"); } }
From source file:net.geco.model.iojson.PersistentStore.java
public void importCourses(JSONStore store, Registry registry, Factory factory) throws JSONException { JSONArray courses = store.getJSONArray(K.COURSES); for (int i = 0; i < courses.length(); i++) { JSONObject c = courses.getJSONObject(i); Course course = store.register(factory.createCourse(), c.getInt(K.ID)); course.setName(c.getString(K.NAME)); course.setLength(c.getInt(K.LENGTH)); course.setClimb(c.getInt(K.CLIMB)); course.setMassStartTime(new Date(c.optLong(K.START, TimeManager.NO_TIME_l))); // MIGR v2.x -> v2.2 course.setCourseSet(store.retrieve(c.optInt(K.COURSESET, 0), CourseSet.class)); JSONArray codez = c.getJSONArray(K.CODES); int[] codes = new int[codez.length()]; for (int j = 0; j < codes.length; j++) { codes[j] = codez.getInt(j);//w w w.j ava 2s . c om } course.setCodes(codes); if (c.has(K.SECTIONS)) { JSONArray sectionz = c.getJSONArray(K.SECTIONS); for (int j = 0; j < sectionz.length(); j++) { JSONObject sectionTuple = sectionz.getJSONObject(j); Section section = store.register(factory.createSection(), sectionTuple.getInt(K.ID)); section.setStartIndex(sectionTuple.getInt(K.START_ID)); section.setName(sectionTuple.getString(K.NAME)); section.setType(SectionType.valueOf(sectionTuple.getString(K.TYPE))); section.setNeutralized(sectionTuple.optBoolean(K.NEUTRALIZED, false)); course.putSection(section); } course.refreshSectionCodes(); } registry.addCourse(course); } registry.ensureAutoCourse(factory); }
From source file:net.geco.model.iojson.PersistentStore.java
public void importRunnersData(JSONStore store, Registry registry, Factory factory) throws JSONException { final int I_RUNNER = 0; final int I_ECARD = 1; final int I_RESULT = 2; JSONArray runnersData = store.getJSONArray(K.RUNNERS_DATA); for (int i = 0; i < runnersData.length(); i++) { JSONArray runnerTuple = runnersData.getJSONArray(i); JSONObject c = runnerTuple.getJSONObject(I_RUNNER); Runner runner = factory.createRunner(); runner.setStartId(c.getInt(K.START_ID)); runner.setFirstname(c.getString(K.FIRST)); runner.setLastname(c.getString(K.LAST)); runner.setEcard(c.getString(K.ECARD)); runner.setClub(store.retrieve(c.getInt(K.CLUB), Club.class)); runner.setCategory(store.retrieve(c.getInt(K.CAT), Category.class)); runner.setCourse(store.retrieve(c.getInt(K.COURSE), Course.class)); runner.setRegisteredStarttime(new Date(c.getLong(K.START))); runner.setArchiveId((Integer) c.opt(K.ARK)); runner.setRentedEcard(c.optBoolean(K.RENT)); runner.setNC(c.optBoolean(K.NC)); registry.addRunner(runner);/* w ww. j av a2 s.c o m*/ JSONObject d = runnerTuple.getJSONObject(I_ECARD); RunnerRaceData raceData = factory.createRunnerRaceData(); raceData.setStarttime(new Date(d.getLong(K.START))); raceData.setFinishtime(new Date(d.getLong(K.FINISH))); raceData.setControltime(new Date(d.getLong(K.CHECK))); raceData.setReadtime(new Date(d.getLong(K.READ))); JSONArray p = d.getJSONArray(K.PUNCHES); Punch[] punches = new Punch[p.length() / 2]; for (int j = 0; j < punches.length; j++) { punches[j] = factory.createPunch(); punches[j].setCode(p.getInt(2 * j)); punches[j].setTime(new Date(p.getLong(2 * j + 1))); } raceData.setPunches(punches); raceData.setRunner(runner); registry.addRunnerData(raceData); JSONObject r = runnerTuple.getJSONObject(I_RESULT); TraceData traceData = factory.createTraceData(); traceData.setNbMPs(r.getInt(K.MPS)); traceData.setNbExtraneous(r.optInt(K.EXTRA)); // MIGR v2.x -> v2.3 JSONArray t = r.getJSONArray(K.TRACE); Trace[] trace = new Trace[t.length() / 2]; for (int j = 0; j < trace.length; j++) { trace[j] = factory.createTrace(t.getString(2 * j), new Date(t.getLong(2 * j + 1))); } if (r.has(K.SECTION_DATA)) { SectionTraceData sectionData = (SectionTraceData) traceData; JSONArray sections = r.getJSONArray(K.SECTION_DATA); for (int j = 0; j < sections.length(); j++) { JSONArray section = sections.getJSONArray(j); sectionData.putSectionAt(store.retrieve(section.getInt(0), Section.class), section.getInt(1)); } } JSONArray neut = r.getJSONArray(K.NEUTRALIZED); for (int j = 0; j < neut.length(); j++) { trace[neut.getInt(j)].setNeutralized(true); } traceData.setTrace(trace); raceData.setTraceData(traceData); RunnerResult result = factory.createRunnerResult(); result.setRaceTime(r.optLong(K.RACE_TIME, TimeManager.NO_TIME_l)); // MIGR v2.x -> v2.2 result.setResultTime(r.getLong(K.TIME)); result.setStatus(Status.valueOf(r.getString(K.STATUS))); result.setTimePenalty(r.getLong(K.PENALTY)); result.setManualTimePenalty(r.optLong(K.MANUAL_PENALTY, 0)); // MIGR v2.x -> v2.3 raceData.setResult(result); } }
From source file:com.futureplatforms.kirin.internal.attic.ProxyGenerator.java
public <T> T javascriptProxyForRequest(final JSONObject obj, Class<T> baseInterface, Class<?>... otherClasses) { Class<?>[] allClasses;/* w ww . ja va 2 s . c o m*/ if (otherClasses.length == 0) { allClasses = new Class[] { baseInterface }; } else { allClasses = new Class[otherClasses.length + 1]; allClasses[0] = baseInterface; System.arraycopy(otherClasses, 0, allClasses, 1, otherClasses.length); } InvocationHandler h = new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String methodName = method.getName(); Class<?> returnType = method.getReturnType(); if (obj.has(methodName)) { String id = obj.optString("__id"); if (id != null) { // so assume it's a callback if (void.class.equals(returnType)) { mKirinHelper.jsCallbackObjectMethod(id, methodName, (Object[]) args); } else { return mKirinHelper.jsSyncCallbackObjectMethod(id, returnType, methodName, (Object[]) args); } } return null; } String propertyName = findGetter(methodName); if (propertyName != null) { return handleGetter(obj, returnType, propertyName); } if ("toString".equals(methodName) && String.class.equals(returnType)) { return obj.toString(); } return null; } }; Object proxy = Proxy.newProxyInstance(baseInterface.getClassLoader(), allClasses, h); return baseInterface.cast(proxy); }
From source file:edu.asu.msse.sgowdru.moviemediaplayerrpc.SearchMovie.java
public void searchButtonClicked(View view) { String searchString = info[0].getText().toString(); android.util.Log.w(getClass().getSimpleName(), searchString); //Check if the movie name is available in RPC server boolean isInRPC = isMovieInRPC(searchString); //If the movie name is available retrieve its detail //Set the video player visibility to VISIBLE if (isInRPC) { System.out.println(searchString + " Movie in RPC!"); //Movie not present in local DB, raise a toast message text = " being retrieved from RPC Server now."; Toast toast = Toast.makeText(context, "Movie " + searchString + text, duration); toast.show();/*from www. j av a 2 s .c o m*/ playButton.setVisibility(view.VISIBLE); getMovieDetails(searchString); } else { try { cur = crsDB.rawQuery("select Title from Movies where Title='" + searchString + "';", new String[] {}); android.util.Log.w(getClass().getSimpleName(), searchString); //If the Movie Exists in the Local Database, we will retrieve it from the Local DB if (cur.getCount() != 0) { //Raise toast message that the movie is already present in local DB text = " already present in DB, Retrieving.."; Toast toast = Toast.makeText(context, "Movie " + searchString + text, duration); toast.show(); //Retrieving the Movie since we know that the movie exists in DB cur = crsDB.rawQuery( "select Title, Genre, Years, Rated, Actors, VideoFile from Movies where Title = ?;", new String[] { searchString }); //Movie Already present hence disabling the Add Button addButton.setEnabled(false); //Move the Cursor and set the Fields cur.moveToNext(); info[1].setText(cur.getString(0)); info[2].setText(cur.getString(1)); info[3].setText(cur.getString(2)); info[4].setText(cur.getString(4)); //Set the Ratings dropdown if (cur.getString(3).equals("PG")) dropdown.setSelection(0); else if (cur.getString(3).equals("PG-13")) dropdown.setSelection(1); else if (cur.getString(3).equals("R")) dropdown.setSelection(2); String video = cur.getString(5); if (video != null) { playButton.setVisibility(view.VISIBLE); } } //If the Movie Does not exist in the Local Database, we will retrieve it from the OMDB else { //Movie not present in local DB, raise a toast message text = " being retrieved from OMDB now."; Toast toast = Toast.makeText(context, "Movie " + searchString + text, duration); toast.show(); //Encode the search string to be appropriate to be placed in a url String encodedUrl = null; try { encodedUrl = URLEncoder.encode(searchString, "UTF-8"); } catch (UnsupportedEncodingException e) { android.util.Log.e(getClass().getSimpleName(), e.getMessage()); } //ASync thread running the query from OMDB and retrieving the movie details as JSON JSONObject result = new MovieGetInfoAsync() .execute("http://www.omdbapi.com/?t=\"" + encodedUrl + "\"&r=json").get(); //Check if the Movie query was successful if (result.getString("Response").equals("True")) { info[1].setText(result.getString("Title")); info[2].setText(result.getString("Genre")); info[3].setText(result.getString("Year")); info[4].setText(result.getString("Actors")); if (result.getString("Rated").equals("PG")) dropdown.setSelection(0); else if (result.getString("Rated").equals("PG-13")) dropdown.setSelection(1); else if (result.getString("Rated").equals("R")) dropdown.setSelection(2); if (result.has("Filename")) { videoFile = result.getString("Filename"); } else { playButton.setVisibility(View.GONE); videoFile = null; } } //Search query was unsuccessful in getting movie with such a name else if (result.getString("Response").equals("False")) { //Raise a toast message text = " not present in OMDB, You can add it manually!"; toast = Toast.makeText(context, "Movie " + searchString + text, duration); toast.show(); } } } catch (Exception e) { android.util.Log.w(getClass().getSimpleName(), e.getMessage()); } } }
From source file:com.serenegiant.media.TLMediaEncoder.java
private static final MediaFormat asMediaFormat(final String format_str) { MediaFormat format = new MediaFormat(); try {/*from w w w .j av a2s .c o m*/ final JSONObject map = new JSONObject(format_str); if (map.has(MediaFormat.KEY_MIME)) format.setString(MediaFormat.KEY_MIME, (String) map.get(MediaFormat.KEY_MIME)); if (map.has(MediaFormat.KEY_WIDTH)) format.setInteger(MediaFormat.KEY_WIDTH, (Integer) map.get(MediaFormat.KEY_WIDTH)); if (map.has(MediaFormat.KEY_HEIGHT)) format.setInteger(MediaFormat.KEY_HEIGHT, (Integer) map.get(MediaFormat.KEY_HEIGHT)); if (map.has(MediaFormat.KEY_BIT_RATE)) format.setInteger(MediaFormat.KEY_BIT_RATE, (Integer) map.get(MediaFormat.KEY_BIT_RATE)); if (map.has(MediaFormat.KEY_COLOR_FORMAT)) format.setInteger(MediaFormat.KEY_COLOR_FORMAT, (Integer) map.get(MediaFormat.KEY_COLOR_FORMAT)); if (map.has(MediaFormat.KEY_FRAME_RATE)) format.setInteger(MediaFormat.KEY_FRAME_RATE, (Integer) map.get(MediaFormat.KEY_FRAME_RATE)); if (map.has(MediaFormat.KEY_I_FRAME_INTERVAL)) format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, (Integer) map.get(MediaFormat.KEY_I_FRAME_INTERVAL)); if (map.has(MediaFormat.KEY_REPEAT_PREVIOUS_FRAME_AFTER)) format.setLong(MediaFormat.KEY_REPEAT_PREVIOUS_FRAME_AFTER, (Long) map.get(MediaFormat.KEY_REPEAT_PREVIOUS_FRAME_AFTER)); if (map.has(MediaFormat.KEY_MAX_INPUT_SIZE)) format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, (Integer) map.get(MediaFormat.KEY_MAX_INPUT_SIZE)); if (map.has(MediaFormat.KEY_DURATION)) format.setInteger(MediaFormat.KEY_DURATION, (Integer) map.get(MediaFormat.KEY_DURATION)); if (map.has(MediaFormat.KEY_CHANNEL_COUNT)) format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, (Integer) map.get(MediaFormat.KEY_CHANNEL_COUNT)); if (map.has(MediaFormat.KEY_SAMPLE_RATE)) format.setInteger(MediaFormat.KEY_SAMPLE_RATE, (Integer) map.get(MediaFormat.KEY_SAMPLE_RATE)); if (map.has(MediaFormat.KEY_CHANNEL_MASK)) format.setInteger(MediaFormat.KEY_CHANNEL_MASK, (Integer) map.get(MediaFormat.KEY_CHANNEL_MASK)); if (map.has(MediaFormat.KEY_AAC_PROFILE)) format.setInteger(MediaFormat.KEY_AAC_PROFILE, (Integer) map.get(MediaFormat.KEY_AAC_PROFILE)); if (map.has(MediaFormat.KEY_AAC_SBR_MODE)) format.setInteger(MediaFormat.KEY_AAC_SBR_MODE, (Integer) map.get(MediaFormat.KEY_AAC_SBR_MODE)); if (map.has(MediaFormat.KEY_MAX_INPUT_SIZE)) format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, (Integer) map.get(MediaFormat.KEY_MAX_INPUT_SIZE)); if (map.has(MediaFormat.KEY_IS_ADTS)) format.setInteger(MediaFormat.KEY_IS_ADTS, (Integer) map.get(MediaFormat.KEY_IS_ADTS)); if (map.has("what")) format.setInteger("what", (Integer) map.get("what")); if (map.has("csd-0")) format.setByteBuffer("csd-0", asByteBuffer((String) map.get("csd-0"))); if (map.has("csd-1")) format.setByteBuffer("csd-1", asByteBuffer((String) map.get("csd-1"))); } catch (JSONException e) { Log.e(TAG_STATIC, "writeFormat:" + format_str, e); format = null; } return format; }