List of usage examples for org.json JSONObject getJSONObject
public JSONObject getJSONObject(String key) throws JSONException
From source file:com.fabernovel.alertevoirie.webservice.HttpPostRequest.java
public String sendRequest() throws AVServiceErrorException { try {/*from ww w . jav a 2 s . c o m*/ httpPost = new HttpPost(url); HttpParams httpParameters = new BasicHttpParams(); // Set the timeout in milliseconds until a connection is established. int timeoutConnection = 20000; HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); // Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data. int timeoutSocket = 5000; HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); httpPost.addHeader(HEADER_APP_VERSION, "1.0.0"); httpPost.addHeader(HEADER_APP_PLATFORM, "android_family"); httpPost.addHeader(HEADER_APP_DEVICE_MODEL, (Build.MANUFACTURER + " " + Build.DEVICE).trim()); httpPost.addHeader(HEADER_APP_REQUEST_SIGNATURE, sha1(MAGIC_KEY + params.get(0).getValue())); // Log.i(Constants.PROJECT_TAG,MAGIC_KEY + params.get(0).getValue()); final HttpResponse response = httpClient.execute(httpPost); final HttpEntity entity = response.getEntity(); content = entity.getContent(); contentString = convertStreamToString(content); Log.d(Constants.PROJECT_TAG, "answer = " + contentString); } catch (final UnsupportedEncodingException uee) { Log.e(Constants.PROJECT_TAG, "UnsupportedEncodingException", uee); throw new AVServiceErrorException(999); } catch (final IOException ioe) { Log.e(Constants.PROJECT_TAG, "IOException", ioe); throw new AVServiceErrorException(999); } catch (final IllegalStateException ise) { Log.e(Constants.PROJECT_TAG, "IllegalStateException", ise); throw new AVServiceErrorException(999); } catch (NoSuchAlgorithmException e) { Log.e(Constants.PROJECT_TAG, "NoSuchAlgorithmException", e); throw new AVServiceErrorException(999); } catch (Exception e) { Log.e(Constants.PROJECT_TAG, "error in sendRequest : ", e); throw new AVServiceErrorException(999); } try { Log.d(Constants.PROJECT_TAG, "contenString: " + contentString); JSONObject jo = new JSONObject(contentString); int resultnum = jo.getJSONObject(JsonData.PARAM_ANSWER).getInt(JsonData.PARAM_STATUS); Log.i(Constants.PROJECT_TAG, "AV Status:" + resultnum); if (resultnum != 0) throw new AVServiceErrorException(resultnum); } catch (JSONException e) { Log.w(Constants.PROJECT_TAG, "JSONException in onPostExecute"); //throw new AVServiceErrorException(999); } return contentString; }
From source file:com.nextgis.firereporter.SettingsSupport.java
public SettingsSupport(Context context, PreferenceScreen screen) { this.screen = screen; this.context = context; // Load the preferences from an XML resource //addPreferencesFromResource(R.xml.preferences); mSendDataIntervalPref = (ListPreference) screen.findPreference(SettingsActivity.KEY_PREF_INTERVAL); if (mSendDataIntervalPref != null) { int index = mSendDataIntervalPref.findIndexOfValue(mSendDataIntervalPref.getValue()); if (index >= 0) { mSendDataIntervalPref.setSummary(mSendDataIntervalPref.getEntries()[index]); } else {// ww w . j av a2 s. c o m mSendDataIntervalPref.setSummary((String) mSendDataIntervalPref.getValue()); } } mSaveBattPref = (CheckBoxPreference) screen.findPreference(SettingsActivity.KEY_PREF_SERVICE_BATT_SAVE); if (mSaveBattPref != null) mSaveBattPref.setSummary(mSaveBattPref.isChecked() ? R.string.stOn : R.string.stOff); mNotifyLEDPref = (CheckBoxPreference) screen.findPreference(SettingsActivity.KEY_PREF_NOTIFY_LED); if (mNotifyLEDPref != null) mNotifyLEDPref.setSummary(mNotifyLEDPref.isChecked() ? R.string.stOn : R.string.stOff); mPlaySoundPref = (CheckBoxPreference) screen.findPreference(SettingsActivity.KEY_PREF_NOTIFY_SOUND); if (mPlaySoundPref != null) mPlaySoundPref.setSummary(mPlaySoundPref.isChecked() ? R.string.stOn : R.string.stOff); mVibroPref = (CheckBoxPreference) screen.findPreference(SettingsActivity.KEY_PREF_NOTIFY_VIBRO); if (mVibroPref != null) mVibroPref.setSummary(mVibroPref.isChecked() ? R.string.stOn : R.string.stOff); mRowCountPref = (EditTextPreference) screen.findPreference(SettingsActivity.KEY_PREF_ROW_COUNT); if (mRowCountPref != null) mRowCountPref.setSummary((String) mRowCountPref.getText()); mFireSearchRadiusPref = (EditTextPreference) screen .findPreference(SettingsActivity.KEY_PREF_FIRE_SEARCH_RADIUS); if (mFireSearchRadiusPref != null) mFireSearchRadiusPref .setSummary((String) mFireSearchRadiusPref.getText() + " " + context.getString(R.string.km)); mDayIntervalPref = (ListPreference) screen.findPreference(SettingsActivity.KEY_PREF_SEARCH_DAY_INTERVAL); if (mDayIntervalPref != null) { int index = mDayIntervalPref.findIndexOfValue(mDayIntervalPref.getValue()); if (index >= 0) { mDayIntervalPref.setSummary(mDayIntervalPref.getEntries()[index]); } else { mDayIntervalPref.setSummary((String) mDayIntervalPref.getValue()); } } mSearchCurrentDatePref = (CheckBoxPreference) screen .findPreference(SettingsActivity.KEY_PREF_SEARCH_CURR_DAY); if (mSearchCurrentDatePref != null) mSearchCurrentDatePref.setSummary(mSearchCurrentDatePref.isChecked() ? R.string.stSearchCurrentDayOn : R.string.stSearchCurrentDayOff); mNasaServerPref = (EditTextPreference) screen.findPreference(SettingsActivity.KEY_PREF_SRV_NASA); if (mNasaServerPref != null) mNasaServerPref.setSummary((String) mNasaServerPref.getText()); mNasaServerUserPref = (EditTextPreference) screen.findPreference(SettingsActivity.KEY_PREF_SRV_NASA_USER); if (mNasaServerUserPref != null) mNasaServerUserPref.setSummary((String) mNasaServerUserPref.getText()); mUserServerPref = (EditTextPreference) screen.findPreference(SettingsActivity.KEY_PREF_SRV_USER); if (mUserServerPref != null) mUserServerPref.setSummary((String) mUserServerPref.getText()); mUserServerUserPref = (EditTextPreference) screen.findPreference(SettingsActivity.KEY_PREF_SRV_USER_USER); if (mUserServerUserPref != null) mUserServerUserPref.setSummary((String) mUserServerUserPref.getText()); mScanServerUserPref = (EditTextPreference) screen.findPreference(SettingsActivity.KEY_PREF_SRV_SCAN_USER); if (mScanServerUserPref != null) mScanServerUserPref.setSummary((String) mScanServerUserPref.getText()); mNasaServerPassPref = (EditTextPreference) screen.findPreference(SettingsActivity.KEY_PREF_SRV_NASA_PASS); mUserServerPassPref = (EditTextPreference) screen.findPreference(SettingsActivity.KEY_PREF_SRV_USER_PASS); mScanServerPassPref = (EditTextPreference) screen.findPreference(SettingsActivity.KEY_PREF_SRV_SCAN_PASS); final Preference checkNasaConnPref = (Preference) screen .findPreference(SettingsActivity.KEY_PREF_SRV_NASA_CHECK_CONN); final Preference checkUserConnPref = (Preference) screen .findPreference(SettingsActivity.KEY_PREF_SRV_USER_CHECK_CONN); final Preference checkScanConnPref = (Preference) screen .findPreference(SettingsActivity.KEY_PREF_SRV_SCAN_CHECK_CONN); mReturnHandler = new Handler() { @SuppressLint("NewApi") public void handleMessage(Message msg) { Bundle resultData = msg.getData(); boolean bHaveErr = resultData.getBoolean(GetFiresService.ERROR); int nType = resultData.getInt(GetFiresService.SOURCE); if (bHaveErr) { Toast.makeText(SettingsSupport.this.context, resultData.getString(GetFiresService.ERR_MSG), Toast.LENGTH_LONG).show(); if (nType == 1) {//user checkUserConnPref.setSummary(R.string.stConnectionFailed); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) checkUserConnPref.setIcon(R.drawable.ic_alerts_and_states_error); } else if (nType == 2) {//nasa checkNasaConnPref.setSummary(R.string.stConnectionFailed); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) checkNasaConnPref.setIcon(R.drawable.ic_alerts_and_states_error); } else if (nType == 3) {//scanex checkScanConnPref.setSummary(R.string.stConnectionFailed); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) checkScanConnPref.setIcon(R.drawable.ic_alerts_and_states_error); } } else { String sData = resultData.getString(GetFiresService.JSON); if (nType == 1) {//user JSONObject jsonMainObject; try { jsonMainObject = new JSONObject(sData); if (jsonMainObject.getBoolean("error")) { String sMsg = jsonMainObject.getString("msg"); Toast.makeText(SettingsSupport.this.context, sMsg, Toast.LENGTH_LONG).show(); checkUserConnPref.setSummary(R.string.stConnectionFailed); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) checkUserConnPref.setIcon(R.drawable.ic_alerts_and_states_error); } else { checkUserConnPref.setSummary(R.string.stConnectionSucceeded); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) checkUserConnPref.setIcon(R.drawable.ic_navigation_accept); } } catch (JSONException e) { Toast.makeText(SettingsSupport.this.context, e.toString(), Toast.LENGTH_LONG).show(); e.printStackTrace(); checkUserConnPref.setSummary(R.string.sCheckDBConnSummary); } } else if (nType == 2) {//nasa JSONObject jsonMainObject; try { jsonMainObject = new JSONObject(sData); if (jsonMainObject.getBoolean("error")) { String sMsg = jsonMainObject.getString("msg"); Toast.makeText(SettingsSupport.this.context, sMsg, Toast.LENGTH_LONG).show(); checkNasaConnPref.setSummary(R.string.stConnectionFailed); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) checkNasaConnPref.setIcon(R.drawable.ic_alerts_and_states_error); } else { checkNasaConnPref.setSummary(R.string.stConnectionSucceeded); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) checkNasaConnPref.setIcon(R.drawable.ic_navigation_accept); } } catch (JSONException e) { Toast.makeText(SettingsSupport.this.context, e.toString(), Toast.LENGTH_LONG).show(); e.printStackTrace(); checkNasaConnPref.setSummary(R.string.sCheckDBConnSummary); } } else if (nType == 3) {//scanex if (sData.length() == 0) { String sMsg = "Connect failed"; Toast.makeText(SettingsSupport.this.context, sMsg, Toast.LENGTH_LONG).show(); checkScanConnPref.setSummary(R.string.stConnectionFailed); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) checkScanConnPref.setIcon(R.drawable.ic_alerts_and_states_error); } else { checkScanConnPref.setSummary(R.string.stConnectionSucceeded); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) checkScanConnPref.setIcon(R.drawable.ic_navigation_accept); new HttpGetter(SettingsSupport.this.context, 4, SettingsSupport.this.context.getString(R.string.stChecking), mReturnHandler, true).execute( "http://fires.kosmosnimki.ru/SAPI/Account/Get/?CallBackName=" + GetFiresService.USER_ID, sData); } } else if (nType == 4) {//scanex detailes try { String sSubData = GetFiresService.removeJsonT(sData); JSONObject rootobj = new JSONObject(sSubData); String sStatus = rootobj.getString("Status"); if (sStatus.equals("OK")) { JSONObject resobj = rootobj.getJSONObject("Result"); String sName = ""; if (!resobj.isNull("FullName")) sName = resobj.getString("FullName"); String sPhone = ""; if (!resobj.isNull("Phone")) sPhone = resobj.getString("Phone"); //add properties if (sPhone.length() > 0) { Preference PhonePref = new Preference(SettingsSupport.this.context); PhonePref.setTitle(R.string.stScanexServerUserPhone); PhonePref.setSummary(sPhone); PhonePref.setOrder(2); SettingsSupport.this.screen.addPreference(PhonePref); } if (sName.length() > 0) { Preference NamePref = new Preference(SettingsSupport.this.context); NamePref.setTitle(R.string.stScanexServerUserFullName); NamePref.setSummary(sName); NamePref.setOrder(2); SettingsSupport.this.screen.addPreference(NamePref); } } else { Toast.makeText(SettingsSupport.this.context, rootobj.getString("ErrorInfo"), Toast.LENGTH_LONG).show(); } } catch (JSONException e) { Toast.makeText(SettingsSupport.this.context, e.toString(), Toast.LENGTH_LONG).show(); e.printStackTrace(); } } } }; }; if (checkNasaConnPref != null) checkNasaConnPref.setOnPreferenceClickListener(new OnPreferenceClickListener() { public boolean onPreferenceClick(Preference preference) { String sURL = mNasaServerPref.getText() + "?function=test_conn_nasa&user=" + mNasaServerUserPref.getText() + "&pass=" + mNasaServerPassPref.getText(); new HttpGetter(SettingsSupport.this.context, 2, SettingsSupport.this.context.getString(R.string.stChecking), mReturnHandler, true) .execute(sURL); return true; } }); if (checkUserConnPref != null) checkUserConnPref.setOnPreferenceClickListener(new OnPreferenceClickListener() { public boolean onPreferenceClick(Preference preference) { String sURL = mUserServerPref.getText() + "?function=test_conn_user&user=" + mUserServerUserPref.getText() + "&pass=" + mUserServerPassPref.getText(); new HttpGetter(SettingsSupport.this.context, 1, SettingsSupport.this.context.getString(R.string.stChecking), mReturnHandler, true) .execute(sURL); return true; } }); if (checkScanConnPref != null) checkScanConnPref.setOnPreferenceClickListener(new OnPreferenceClickListener() { public boolean onPreferenceClick(Preference preference) { //String sURL = mUserServerPref.getText() + "?function=test_conn_nasa&user=" + mUserServerUserPref.getText() + "&pass=" + mUserServerPassPref.getText(); new ScanexHttpLogin(SettingsSupport.this.context, 3, SettingsSupport.this.context.getString(R.string.stChecking), mReturnHandler, true) .execute(mScanServerUserPref.getText(), mScanServerPassPref.getText()); return true; } }); }
From source file:org.uiautomation.ios.grid.IOSCapabilitiesMonitor.java
private List<DesiredCapabilities> getNodeCapabilities() { try {//from w w w . j av a 2 s . co m List<DesiredCapabilities> capabilities = new ArrayList<DesiredCapabilities>(); JSONObject status = getNodeStatusJson(); if (status == null) { return null; } String ios = status.getJSONObject("value").getJSONObject("ios").optString("simulatorVersion"); JSONArray supportedApps = status.getJSONObject("value").getJSONArray("supportedApps"); for (int i = 0; i < supportedApps.length(); i++) { Map<String, Object> capability = new HashMap<String, Object>(); capability.put("maxInstances", "1"); if (ios.isEmpty()) { capability.put("ios", "5.1"); capability.put("browserName", "IOS Device"); capability.put(IOSCapabilities.DEVICE, DeviceType.iphone); } else { capability.put("ios", ios); capability.put("browserName", "IOS Simulator"); } JSONObject app = supportedApps.getJSONObject(i); for (String key : JSONObject.getNames(app)) { if ("locales".equals(key)) { JSONArray loc = app.getJSONArray(key); List<String> locales = new ArrayList<String>(); for (int j = 0; j < loc.length(); j++) { locales.add(loc.getString(j)); } capability.put("locales", locales); } else { Object o = app.get(key); capability.put(key, o); } } capabilities.add(new DesiredCapabilities(capability)); } return capabilities; } catch (Exception e) { return null; } }
From source file:com.ota.updates.json.ROMJSONParser.java
/** * Parse the Rom object within the selected JSON string *///from w w w. j a v a 2s . c o m public void parse() { try { JSONObject jObj = new JSONObject(mJSONString); JSONObject romObj = jObj.getJSONObject(NAME_ROM); } catch (JSONException e) { Log.e(TAG, e.getMessage()); } }
From source file:com.asd.littleprincesbeauty.data.TaskList.java
@Override public void setContentByLocalJSON(JSONObject js) { if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE)) { Log.w(TAG, "setContentByLocalJSON: nothing is avaiable"); }//from w w w . j a v a2s . c o m try { JSONObject folder = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) { String name = folder.getString(NoteColumns.SNIPPET); setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + name); } else if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) { if (folder.getLong(NoteColumns.ID) == Notes.ID_ROOT_FOLDER) setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT); else if (folder.getLong(NoteColumns.ID) == Notes.ID_CALL_RECORD_FOLDER) setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_CALL_NOTE); else Log.e(TAG, "invalid system folder"); } else { Log.e(TAG, "error type"); } } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); } }
From source file:cc.redpen.server.api.RedPenConfigurationResourceTest.java
@Test public void allConfigurationsIfLangNotSpecified() throws Exception { RedPenService service = mock(RedPenService.class); RedPen redPen = mock(RedPen.class, RETURNS_DEEP_STUBS); doReturn(ImmutableMap.of("en", redPen, "ja", redPen, "et", redPen)).when(service).getRedPens(); resource = spy(resource);/*from ww w. j a v a 2s. co m*/ doReturn(service).when(resource).getRedPenService(); JSONObject response = (JSONObject) resource.getRedPens(null).getEntity(); JSONObject redpens = response.getJSONObject("redpens"); assertEquals(3, redpens.length()); assertNotNull(redpens.getJSONObject("en")); assertNotNull(redpens.getJSONObject("ja")); assertNotNull(redpens.getJSONObject("et")); }
From source file:cc.redpen.server.api.RedPenConfigurationResourceTest.java
@Test public void redPenFields() throws Exception { JSONObject response = (JSONObject) resource.getRedPens(null).getEntity(); JSONObject redpens = response.getJSONObject("redpens"); JSONObject ja = redpens.getJSONObject("ja"); assertEquals("ja", ja.getString("lang")); assertEquals("zenkaku", ja.getString("variant")); assertEquals(JapaneseTokenizer.class.getName(), ja.getString("tokenizer")); assertTrue(!ja.getString("validators").isEmpty()); assertTrue(!ja.getString("symbols").isEmpty()); }
From source file:com.microsoft.sharepointservices.SharepointClient.java
protected ListenableFuture<String> getFormDigest() { HttpConnection connection = Platform.createHttpConnection(); Request request = new Request("POST"); request.setUrl(getSiteUrl() + "_api/contextinfo"); prepareRequest(request);/*w ww.j a va 2 s .c o m*/ log("Generate request for getFormDigest", LogLevel.Verbose); request.log(getLogger()); final SettableFuture<String> result = SettableFuture.create(); ListenableFuture<Response> future = connection.execute(request); Futures.addCallback(future, new FutureCallback<Response>() { @Override public void onFailure(Throwable t) { result.setException(t); } @Override public void onSuccess(Response response) { try { int statusCode = response.getStatus(); if (isValidStatus(statusCode)) { String responseContent = response.readToEnd(); JSONObject json = new JSONObject(responseContent); result.set(json.getJSONObject("d").getJSONObject("GetContextWebInformation") .getString("FormDigestValue")); } else { result.setException( new Exception("Invalid status code " + statusCode + ": " + response.readToEnd())); } } catch (Exception e) { log(e); } } }); return result; }
From source file:com.microsoft.sharepointservices.SharepointClient.java
public ListenableFuture<String> getWebTitle() { final SettableFuture<String> result = SettableFuture.create(); ListenableFuture<JSONObject> request = executeRequestJson(mServerUrl + "_api/web/title", "GET"); Futures.addCallback(request, new FutureCallback<JSONObject>() { @Override//from www. j a v a 2 s . co m public void onFailure(Throwable t) { result.setException(t); } @Override public void onSuccess(JSONObject json) { try { result.set(json.getJSONObject("d").getString("Title")); } catch (JSONException e) { log(e); } } }); return result; }
From source file:org.eclipse.orion.server.tests.servlets.git.GitConfigTest.java
@Test public void testGetListOfConfigEntries() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); IPath[] clonePaths = createTestProjects(workspaceLocation); for (IPath clonePath : clonePaths) { // clone a repo String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION); // get project metadata WebRequest request = getGetRequest(contentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject project = new JSONObject(response.getText()); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG); JSONObject configResponse = listConfigEntries(gitConfigUri); JSONArray configEntries = configResponse.getJSONArray(ProtocolConstants.KEY_CHILDREN); for (int i = 0; i < configEntries.length(); i++) { JSONObject configEntry = configEntries.getJSONObject(i); assertNotNull(configEntry.optString(GitConstants.KEY_CONFIG_ENTRY_KEY, null)); assertNotNull(configEntry.optString(GitConstants.KEY_CONFIG_ENTRY_VALUE, null)); assertConfigUri(configEntry.getString(ProtocolConstants.KEY_LOCATION)); assertCloneUri(configEntry.getString(GitConstants.KEY_CLONE)); assertEquals(ConfigOption.TYPE, configEntry.getString(ProtocolConstants.KEY_TYPE)); }/*from w w w . j ava 2 s . c o m*/ } }