List of usage examples for org.json JSONObject getInt
public int getInt(String key) throws JSONException
From source file:CNCServicesServlet.CNCServices.java
/** * Demo Call Get Card Service//from ww w .j a va 2 s. c om */ private String GetCard(HttpServletRequest request) { String txtData = ""; String txtAgentCode = request.getServletContext().getInitParameter("agentCode"); String txtAgentKey = request.getServletContext().getInitParameter("agentKey"); String tranidRequest = "2016062711420056"; GetCardRequest gcr = new GetCardRequest(txtAgentCode, tranidRequest); String gcrString = gcr.toString(); ResponseRequest rr = new ResponseRequest(); try { txtData = Util.Encrypt(txtAgentKey, gcrString); Softpin soft = new Softpin(); String result = soft.getSoftpinSoap12().getCard(txtAgentCode, txtData); JSONObject jsonObject = new JSONObject(result); String msg = jsonObject.isNull("msg") ? "" : jsonObject.getString("msg"); rr.setMsg(msg); String tranid = jsonObject.isNull("tranid") ? "" : jsonObject.getString("tranid"); rr.setTranid(tranid); String listCards = jsonObject.isNull("listCards") ? "" : jsonObject.getString("listCards"); Integer code = jsonObject.getInt("code"); rr.setCode(code); if (code == 1) { String listResult = Util.Decrypt(txtAgentKey, listCards); rr.setListCards(listResult); System.out.println("success"); } else { rr.setListCards(""); System.out.println("Error=" + msg); } } catch (Exception e) { rr.setListCards(""); System.out.println("error=" + e.getMessage()); } return rr.toString(); }
From source file:CNCServicesServlet.CNCServices.java
/** * Demo Call Topup Serivce// www. ja v a2 s . c o m */ private String Topup(HttpServletRequest request) { String txtData = ""; String txtAgentCode = request.getServletContext().getInitParameter("agentCode"); String txtAgentKey = request.getServletContext().getInitParameter("agentKey"); String tranidRequest = "2016062711420056"; TopupRequest tr = new TopupRequest(txtAgentCode, "VT", tranidRequest, "0902183903", 10000, "VTT"); String trString = tr.toString(); ResponseTopup rt = new ResponseTopup(); try { txtData = Util.Encrypt(txtAgentKey, trString); Softpin soft = new Softpin(); String result = soft.getSoftpinSoap12().topup(txtAgentCode, txtData); JSONObject jsonObject = new JSONObject(result); String msg = jsonObject.isNull("msg") ? "" : jsonObject.getString("msg"); rt.setMsg(msg); String tranid = jsonObject.isNull("tranid") ? "" : jsonObject.getString("tranid"); rt.setTranid(tranid); Integer code = jsonObject.getInt("code"); rt.setCode(code); } catch (Exception e) { System.out.println("error=" + e.getMessage()); } return rt.toString(); }
From source file:CNCServicesServlet.CNCServices.java
/** * Demo Call Check Transaction Topup Service *///w w w .j av a 2s . c o m private String CheckTranTopup(HttpServletRequest request) { String txtData = ""; String txtAgentCode = request.getServletContext().getInitParameter("agentCode"); String txtAgentKey = request.getServletContext().getInitParameter("agentKey"); String tranidRequest = "2016062711420056"; GetCardRequest gcr = new GetCardRequest(txtAgentCode, tranidRequest); String gcrString = gcr.toString(); ResponseTopup rt = new ResponseTopup(); try { txtData = Util.Encrypt(txtAgentKey, gcrString); Softpin soft = new Softpin(); String result = soft.getSoftpinSoap12().checkTranTopup(txtAgentCode, txtData); JSONObject jsonObject = new JSONObject(result); String msg = jsonObject.isNull("msg") ? "" : jsonObject.getString("msg"); rt.setMsg(msg); String tranid = jsonObject.isNull("tranid") ? "" : jsonObject.getString("tranid"); rt.setTranid(tranid); Integer code = jsonObject.getInt("code"); rt.setCode(code); } catch (Exception e) { System.out.println("error=" + e.getMessage()); } return rt.toString(); }
From source file:CNCServicesServlet.CNCServices.java
/** * Demo Call Check Count Service/* www . ja va 2 s. com*/ */ private String CheckCount(HttpServletRequest request) { String txtData = ""; String txtAgentCode = request.getServletContext().getInitParameter("agentCode"); String txtAgentKey = request.getServletContext().getInitParameter("agentKey"); CheckCountRequest ccrequest = new CheckCountRequest(txtAgentCode, "VT", 10000); String ccRequestString = ccrequest.toString(); CheckCountResponse ccResponse = new CheckCountResponse(); try { txtData = Util.Encrypt(txtAgentKey, ccRequestString); Softpin soft = new Softpin(); String result = soft.getSoftpinSoap12().checkCount(txtAgentCode, txtData); JSONObject jsonObject = new JSONObject(result); String msg = jsonObject.isNull("msg") ? "" : jsonObject.getString("msg"); ccResponse.setMsg(msg); String listprovider = jsonObject.isNull("listprovider") ? "" : jsonObject.getString("listprovider"); Integer code = jsonObject.getInt("code"); ccResponse.setCode(code); if (code == 1) { String listResult = Util.Decrypt(txtAgentKey, listprovider); ccResponse.setListprovider(listResult); System.out.println("success"); } else { ccResponse.setListprovider(""); System.out.println("Error=" + msg); } } catch (Exception e) { ccResponse.setListprovider(""); System.out.println("error=" + e.getMessage()); } return ccResponse.toString(); }
From source file:fr.immotronic.ubikit.pems.enocean.impl.item.data.EEP070905DataImpl.java
public static EEP070905DataImpl constructDataFromRecord(JSONObject lastKnownData) { if (lastKnownData == null) return new EEP070905DataImpl(VOC.VOCT, -1, null); try {/*w w w . j av a2s .co m*/ VOC VOCname = VOC.valueOf(lastKnownData.getString("VOCname")); int VOCppb = lastKnownData.getInt("VOCppb"); Date date = new Date(lastKnownData.getLong("date")); return new EEP070905DataImpl(VOCname, VOCppb, date); } catch (JSONException e) { Logger.error(LC.gi(), null, "constructDataFromRecord(): An exception while building a sensorData from a JSONObject SHOULD never happen. Check the code !", e); return new EEP070905DataImpl(VOC.VOCT, -1, null); } }
From source file:com.chinaftw.music.model.MusicProvider.java
private MediaMetadata buildFromJSON(JSONObject json) throws JSONException { String id = String.valueOf(json.getInt(JSON_SONG_ID)); String title = json.getString(JSON_TITLE); String album = json.getJSONObject(JSON_ALBUM).getString(JSON_ALBUM_NAME); String albumId = json.getJSONObject(JSON_ALBUM).getString(JSON_ALBUM_ID); String artist = json.getJSONArray(JSON_ARTIST).getJSONObject(0).getString(JSON_ARTIST_NAME); String artistId = json.getJSONObject(JSON_ALBUM).getString(JSON_ARTIST_ID); String imageId = json.getJSONObject(JSON_ALBUM).getString(JSON_ALBUM_IMAGE_ID); int duration = json.getInt(JSON_DURATION); // ms LogHelper.d(TAG, "Found music track: ", json); String source = MusicAPI.getSongUrl(id); String imageUri = MusicAPI.getPictureUrl(imageId); // Since we don't have a unique ID in the server, we fake one using the hashcode of // the music source. In a real world app, this could come from the server. // Adding the music source to the MediaMetadata (and consequently using it in the // mediaSession.setMetadata) is not a good idea for a real world music app, because // the session metadata can be accessed by notification listeners. This is done in this // sample for convenience only. return new MediaMetadata.Builder().putString(MediaMetadata.METADATA_KEY_MEDIA_ID, id) .putString(CUSTOM_METADATA_TRACK_SOURCE, source).putString(MediaMetadata.METADATA_KEY_ALBUM, album) .putString(MediaMetadata.METADATA_KEY_ARTIST, artist) .putLong(MediaMetadata.METADATA_KEY_DURATION, duration) .putString(MediaMetadata.METADATA_KEY_GENRE, "BILLBOARD") //TODO .putString(MediaMetadata.METADATA_KEY_TITLE, title) .putString(MediaMetadata.METADATA_KEY_ART_URI, imageUri) .putString(MediaMetadata.METADATA_KEY_ALBUM_ART_URI, imageUri) .putString(MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI, imageUri).build(); }
From source file:de.duenndns.ssl.MemorizingTrustManager.java
private List<String> getPoshFingerprintsFromServer(String domain, String url, int maxTtl, boolean followUrl) { Log.d("mtm", "downloading json for " + domain + " from " + url); try {/*from w ww .j a v a 2s . com*/ List<String> results = new ArrayList<>(); HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuilder builder = new StringBuilder(); while ((inputLine = in.readLine()) != null) { builder.append(inputLine); } JSONObject jsonObject = new JSONObject(builder.toString()); in.close(); int expires = jsonObject.getInt("expires"); if (expires <= 0) { return new ArrayList<>(); } if (maxTtl >= 0) { expires = Math.min(maxTtl, expires); } String redirect; try { redirect = jsonObject.getString("url"); } catch (JSONException e) { redirect = null; } if (followUrl && redirect != null && redirect.toLowerCase().startsWith("https")) { return getPoshFingerprintsFromServer(domain, redirect, expires, false); } JSONArray fingerprints = jsonObject.getJSONArray("fingerprints"); for (int i = 0; i < fingerprints.length(); i++) { JSONObject fingerprint = fingerprints.getJSONObject(i); String sha256 = fingerprint.getString("sha-256"); if (sha256 != null) { results.add(sha256); } } writeFingerprintsToCache(domain, results, 1000L * expires + System.currentTimeMillis()); return results; } catch (Exception e) { Log.d("mtm", "error fetching posh " + e.getMessage()); return new ArrayList<>(); } }
From source file:com.kevinquan.android.utils.JSONUtils.java
/** * Retrieve an integer stored at the provided key from the provided JSON object * @param obj The JSON object to retrieve from * @param key The key to retrieve//from w ww .j a v a 2 s.c o m * @return the integer stored in the key, or the default value if the key doesn't exist */ public static int safeGetInt(JSONObject obj, String key, int defaultValue) { if (obj == null || TextUtils.isEmpty(key)) return defaultValue; if (obj.has(key)) { try { return obj.getInt(key); } catch (JSONException e) { Log.w(TAG, "Could not get int from key " + key, e); } } return defaultValue; }
From source file:net.idlesoft.android.apps.github.activities.SingleActivityItem.java
private void loadActivityItemBox() { final TextView date = (TextView) findViewById(R.id.tv_activity_item_date); final ImageView gravatar = (ImageView) findViewById(R.id.iv_activity_item_gravatar); final ImageView icon = (ImageView) findViewById(R.id.iv_activity_item_icon); final TextView title_tv = (TextView) findViewById(R.id.tv_activity_item_title); try {/*w ww . j a va2s . c o m*/ final JSONObject entry = mJson; final JSONObject payload = entry.getJSONObject("payload"); String end; final SimpleDateFormat dateFormat = new SimpleDateFormat(Hubroid.GITHUB_ISSUES_TIME_FORMAT); final Date item_time = dateFormat.parse(entry.getString("created_at")); final Date current_time = dateFormat.parse(dateFormat.format(new Date())); final long ms = current_time.getTime() - item_time.getTime(); final long sec = ms / 1000; final long min = sec / 60; final long hour = min / 60; final long day = hour / 24; if (day > 0) { if (day == 1) { end = " day ago"; } else { end = " days ago"; } date.setText(day + end); } else if (hour > 0) { if (hour == 1) { end = " hour ago"; } else { end = " hours ago"; } date.setText(hour + end); } else if (min > 0) { if (min == 1) { end = " minute ago"; } else { end = " minutes ago"; } date.setText(min + end); } else { if (sec == 1) { end = " second ago"; } else { end = " seconds ago"; } date.setText(sec + end); } final String actor = entry.getString("actor"); final String eventType = entry.getString("type"); String title = actor + " did something..."; gravatar.setImageBitmap(GravatarCache.getDipGravatar(GravatarCache.getGravatarID(actor), 30.0f, getResources().getDisplayMetrics().density)); if (eventType.contains("PushEvent")) { icon.setImageResource(R.drawable.push); title = actor + " pushed to " + payload.getString("ref").split("/")[2] + " at " + entry.getJSONObject("repository").getString("owner") + "/" + entry.getJSONObject("repository").getString("name"); } else if (eventType.contains("WatchEvent")) { final String action = payload.getString("action"); if (action.equalsIgnoreCase("started")) { icon.setImageResource(R.drawable.watch_started); } else { icon.setImageResource(R.drawable.watch_stopped); } title = actor + " " + action + " watching " + entry.getJSONObject("repository").getString("owner") + "/" + entry.getJSONObject("repository").getString("name"); } else if (eventType.contains("GistEvent")) { final String action = payload.getString("action"); icon.setImageResource(R.drawable.gist); title = actor + " " + action + "d " + payload.getString("name"); } else if (eventType.contains("ForkEvent")) { icon.setImageResource(R.drawable.fork); title = actor + " forked " + entry.getJSONObject("repository").getString("name") + "/" + entry.getJSONObject("repository").getString("owner"); } else if (eventType.contains("CommitCommentEvent")) { icon.setImageResource(R.drawable.comment); title = actor + " commented on " + entry.getJSONObject("repository").getString("owner") + "/" + entry.getJSONObject("repository").getString("name"); } else if (eventType.contains("ForkApplyEvent")) { icon.setImageResource(R.drawable.merge); title = actor + " applied fork commits to " + entry.getJSONObject("repository").getString("owner") + "/" + entry.getJSONObject("repository").getString("name"); } else if (eventType.contains("FollowEvent")) { icon.setImageResource(R.drawable.follow); title = actor + " started following " + payload.getJSONObject("target").getString("login"); } else if (eventType.contains("CreateEvent")) { icon.setImageResource(R.drawable.create); if (payload.getString("object").contains("repository")) { title = actor + " created repository " + payload.getString("name"); } else if (payload.getString("object").contains("branch")) { title = actor + " created branch " + payload.getString("object_name") + " at " + entry.getJSONObject("repository").getString("owner") + "/" + entry.getJSONObject("repository").getString("name"); } else if (payload.getString("object").contains("tag")) { title = actor + " created tag " + payload.getString("object_name") + " at " + entry.getJSONObject("repository").getString("owner") + "/" + entry.getJSONObject("repository").getString("name"); } } else if (eventType.contains("IssuesEvent")) { if (payload.getString("action").equalsIgnoreCase("opened")) { icon.setImageResource(R.drawable.issues_open); } else { icon.setImageResource(R.drawable.issues_closed); } title = actor + " " + payload.getString("action") + " issue " + payload.getInt("number") + " on " + entry.getJSONObject("repository").getString("owner") + "/" + entry.getJSONObject("repository").getString("name"); } else if (eventType.contains("DeleteEvent")) { icon.setImageResource(R.drawable.delete); if (payload.getString("object").contains("repository")) { title = actor + " deleted repository " + payload.getString("name"); } else if (payload.getString("object").contains("branch")) { title = actor + " deleted branch " + payload.getString("object_name") + " at " + entry.getJSONObject("repository").getString("owner") + "/" + entry.getJSONObject("repository").getString("name"); } else if (payload.getString("object").contains("tag")) { title = actor + " deleted tag " + payload.getString("object_name") + " at " + entry.getJSONObject("repository").getString("owner") + "/" + entry.getJSONObject("repository").getString("name"); } } else if (eventType.contains("WikiEvent")) { icon.setImageResource(R.drawable.wiki); title = actor + " " + payload.getString("action") + " a page in the " + entry.getJSONObject("repository").getString("owner") + "/" + entry.getJSONObject("repository").getString("name") + " wiki"; } else if (eventType.contains("DownloadEvent")) { icon.setImageResource(R.drawable.download); title = actor + " uploaded a file to " + entry.getJSONObject("repository").getString("owner") + "/" + entry.getJSONObject("repository").getString("name"); } else if (eventType.contains("PublicEvent")) { icon.setImageResource(R.drawable.opensource); title = actor + " open sourced " + entry.getJSONObject("repository").getString("name"); } else if (eventType.contains("PullRequestEvent")) { final int number = (payload.get("pull_request") instanceof JSONObject) ? payload.getJSONObject("pull_request").getInt("number") : payload.getInt("number"); if (payload.getString("action").equalsIgnoreCase("opened")) { icon.setImageResource(R.drawable.issues_open); title = actor + " opened pull request " + number + " on " + entry.getJSONObject("repository").getString("owner") + "/" + entry.getJSONObject("repository").getString("name"); } else if (payload.getString("action").equalsIgnoreCase("closed")) { icon.setImageResource(R.drawable.issues_closed); title = actor + " closed pull request " + number + " on " + entry.getJSONObject("repository").getString("owner") + "/" + entry.getJSONObject("repository").getString("name"); } } else if (eventType.contains("MemberEvent")) { icon.setImageResource(R.drawable.follow); title = actor + " added " + payload.getString("member") + " to " + entry.getJSONObject("repository").getString("owner") + "/" + entry.getJSONObject("repository").getString("name"); } title_tv.setText(title); gravatar.setOnClickListener(new OnClickListener() { public void onClick(final View v) { final Intent i = new Intent(SingleActivityItem.this, Profile.class); i.putExtra("username", actor); startActivity(i); } }); } catch (final JSONException e) { e.printStackTrace(); } catch (final ParseException e) { e.printStackTrace(); } }
From source file:project.getphrase.PhraseService.java
public ArrayList<Phrase> getPhraseS(int bmilvl, int change, int w1, int w2, int w3) throws MalformedURLException, IOException, JSONException { String url = ""; url += Settings.BASE_PROTOCOL + Settings.PH_BASE_URL + Settings.PH_BASE_PORT + Settings.PH_BASE_PATH; url += "?" + Settings.PH_PARAM_BMI_LEVEL + "=" + bmilvl; url += "&" + Settings.PH_PARAM_CHANGE + "=" + change; url += "&" + Settings.PH_PARAM_WEATHER_TYPE_1 + "=" + w1; url += "&" + Settings.PH_PARAM_WEATHER_TYPE_2 + "=" + w2; url += "&" + Settings.PH_PARAM_WEATHER_TYPE_3 + "=" + w3; System.out.println("URL: " + url); URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod(REQ_TYPE);/*w w w . j a v a 2s . co m*/ BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); ArrayList<Phrase> retPhs = new ArrayList<Phrase>(); JSONArray phs = new JSONArray(response.toString()); for (int i = 0; i < phs.length(); i++) { JSONObject o = phs.getJSONObject(i); Phrase p = new Phrase(); p.setIdphrase(o.getInt(Settings.PH_JSON_ID_PHRASE)); p.setPhrase(o.getString(Settings.PH_JSON_PHRASE)); p.setWeathertype(o.getInt(Settings.PH_JSON_WEATHER_TYPE)); p.setBmirange(o.getInt(Settings.PH_JSON_BMIRANGE)); p.setChange(o.getInt(Settings.PH_JSON_CHANGE)); p.setActivity(o.getString(Settings.PH_JSON_ACTIVITY)); retPhs.add(p); } return retPhs; }