List of usage examples for org.json JSONObject getJSONObject
public JSONObject getJSONObject(String key) throws JSONException
From source file:net.mandaria.radioreddit.apis.RedditAPI.java
public static RedditAccount login(Context context, String username, String password) { RedditAccount account = new RedditAccount(); try {/*w ww . jav a 2 s . com*/ String url = context.getString(R.string.reddit_login) + "/" + username; // post values ArrayList<NameValuePair> post_values = new ArrayList<NameValuePair>(); BasicNameValuePair user = new BasicNameValuePair("user", username); post_values.add(user); BasicNameValuePair passwd = new BasicNameValuePair("passwd", password); post_values.add(passwd); BasicNameValuePair api_type = new BasicNameValuePair("api_type", "json"); post_values.add(api_type); String outputLogin = HTTPUtil.post(context, url, post_values); JSONTokener reddit_login_tokener = new JSONTokener(outputLogin); JSONObject reddit_login_json = new JSONObject(reddit_login_tokener); JSONObject json = reddit_login_json.getJSONObject("json"); if (json.getJSONArray("errors").length() > 0) { String error = json.getJSONArray("errors").getJSONArray(0).getString(1); account.ErrorMessage = error; } else { JSONObject data = json.getJSONObject("data"); // success! String cookie = data.getString("cookie"); String modhash = data.getString("modhash"); account.Username = username; account.Cookie = cookie; account.Modhash = modhash; account.ErrorMessage = ""; } } catch (Exception ex) { // We fail to get the streams... CustomExceptionHandler ceh = new CustomExceptionHandler(context); ceh.sendEmail(ex); ex.printStackTrace(); account.ErrorMessage = ex.toString(); } return account; }
From source file:net.mandaria.radioreddit.apis.RedditAPI.java
public static String Vote(Context context, RedditAccount account, int voteDirection, String fullname) { String errorMessage = ""; try {//from w w w . j a va2s .c om try { account.Modhash = updateModHash(context); if (account.Modhash == null) { errorMessage = context.getString(R.string.error_ThereWasAProblemVotingPleaseTryAgain); return errorMessage; } } catch (Exception ex) { errorMessage = ex.getMessage(); return errorMessage; } String url = context.getString(R.string.reddit_vote); // post values ArrayList<NameValuePair> post_values = new ArrayList<NameValuePair>(); BasicNameValuePair id = new BasicNameValuePair("id", fullname); post_values.add(id); BasicNameValuePair dir = new BasicNameValuePair("dir", Integer.toString(voteDirection)); post_values.add(dir); // not required //BasicNameValuePair r = new BasicNameValuePair("r", "radioreddit"); // TODO: shouldn't be hard coded, could be talkradioreddit //post_values.add(r); BasicNameValuePair uh = new BasicNameValuePair("uh", account.Modhash); post_values.add(uh); BasicNameValuePair api_type = new BasicNameValuePair("api_type", "json"); post_values.add(api_type); String outputVote = HTTPUtil.post(context, url, post_values); JSONTokener reddit_vote_tokener = new JSONTokener(outputVote); JSONObject reddit_vote_json = new JSONObject(reddit_vote_tokener); if (reddit_vote_json.has("json")) { JSONObject json = reddit_vote_json.getJSONObject("json"); if (json.has("errors") && json.getJSONArray("errors").length() > 0) { String error = json.getJSONArray("errors").getJSONArray(0).getString(1); errorMessage = error; } } // success! } catch (Exception ex) { // We fail to vote... CustomExceptionHandler ceh = new CustomExceptionHandler(context); ceh.sendEmail(ex); ex.printStackTrace(); errorMessage = ex.toString(); } return errorMessage; }
From source file:net.mandaria.radioreddit.apis.RedditAPI.java
public static String updateModHash(Context context) { // Calls me.json to get the current modhash for the user String output = ""; boolean errorGettingModHash = false; try {/* w w w.j a v a 2s .c o m*/ try { output = HTTPUtil.get(context, context.getString(R.string.reddit_me)); } catch (Exception ex) { ex.printStackTrace(); errorGettingModHash = true; // For now, not used. It is acceptable to error out and not alert the user // radiosong.ErrorMessage = "Unable to connect to reddit";//context.getString(R.string.error_RadioRedditServerIsDownNotification); } if (!errorGettingModHash && output.length() > 0) { JSONTokener reddit_me_tokener = new JSONTokener(output); JSONObject reddit_me_json = new JSONObject(reddit_me_tokener); JSONObject data = reddit_me_json.getJSONObject("data"); String modhash = data.getString("modhash"); return modhash; } else { return null; } } catch (Exception ex) { // CustomExceptionHandler ceh = new CustomExceptionHandler(context); // ceh.sendEmail(ex); ex.printStackTrace(); return null; } }
From source file:net.dv8tion.jda.core.handle.GuildMemberRemoveHandler.java
@Override protected Long handleInternally(JSONObject content) { final long id = content.getLong("guild_id"); if (api.getGuildLock().isLocked(id)) return id; GuildImpl guild = (GuildImpl) api.getGuildMap().get(id); if (guild == null) { //We probably just left the guild and this event is trying to remove us from the guild, therefore ignore return null; }/*from w ww.j a v a 2s. c o m*/ final long userId = content.getJSONObject("user").getLong("id"); MemberImpl member = (MemberImpl) guild.getMembersMap().remove(userId); if (member == null) { WebSocketClient.LOG .debug("Received GUILD_MEMBER_REMOVE for a Member that does not exist in the specified Guild."); return null; } if (member.getVoiceState().inVoiceChannel())//If this user was in a VoiceChannel, fire VoiceLeaveEvent. { GuildVoiceStateImpl vState = (GuildVoiceStateImpl) member.getVoiceState(); VoiceChannel channel = vState.getChannel(); vState.setConnectedChannel(null); ((VoiceChannelImpl) channel).getConnectedMembersMap().remove(member.getUser().getIdLong()); api.getEventManager().handle(new GuildVoiceLeaveEvent(api, responseNumber, member, channel)); } //The user is not in a different guild that we share // The user also is not a friend of this account in the case that the logged in account is a client account. if (userId != api.getSelfUser().getIdLong() // don't remove selfUser from cache && api.getGuildMap().valueCollection().stream() .noneMatch(g -> ((GuildImpl) g).getMembersMap().containsKey(userId)) && !(api.getAccountType() == AccountType.CLIENT && api.asClient().getFriendById(userId) != null)) { UserImpl user = (UserImpl) api.getUserMap().remove(userId); if (user.hasPrivateChannel()) { PrivateChannelImpl priv = (PrivateChannelImpl) user.getPrivateChannel(); user.setFake(true); priv.setFake(true); api.getFakeUserMap().put(user.getIdLong(), user); api.getFakePrivateChannelMap().put(priv.getIdLong(), priv); } else if (api.getAccountType() == AccountType.CLIENT) { //While the user might not have a private channel, if this is a client account then the user // could be in a Group, and if so we need to change the User object to be fake and // place it in the FakeUserMap for (Group grp : api.asClient().getGroups()) { if (grp.getNonFriendUsers().contains(user)) { user.setFake(true); api.getFakeUserMap().put(user.getIdLong(), user); break; //Breaks from groups loop } } } } api.getEventManager().handle(new GuildMemberLeaveEvent(api, responseNumber, guild, member)); return null; }
From source file:org.wso2.carbon.connector.integration.test.googleanalytics.GoogleanalyticsConnectorIntegrationTest.java
/** * Positive test case for getReportData method with mandatory parameters. * * @throws org.json.JSONException//from www . j a v a2s.c om * @throws java.io.IOException */ @Test(groups = { "wso2.esb" }, description = "googleanalytics {getReportData} integration test with mandatory parameters.", dependsOnMethods = { "testListAccountSummariesWithMandatoryParameters" }) public void testGetReportDataWithMandatoryParameters() throws IOException, JSONException { esbRequestHeadersMap.put("Action", "urn:getReportData"); RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap, "esb_getReportData_mandatory.json"); final JSONObject esbResponse = esbRestResponse.getBody(); final String apiEndpoint = apiEndpointUrl + "/data/ga?ids=" + connectorProperties.getProperty("coreReportIds") + "&start-date=" + connectorProperties.getProperty("coreReportStartDate") + "&end-date=" + connectorProperties.getProperty("coreReportEndDate") + "&metrics=" + connectorProperties.getProperty("coreReportMetrics"); RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndpoint, "GET", apiRequestHeadersMap); final JSONObject apiResponse = apiRestResponse.getBody(); Assert.assertEquals(esbResponse.getString("kind"), apiResponse.getString("kind")); Assert.assertEquals(esbResponse.getString("id"), apiResponse.getString("id")); Assert.assertEquals(esbResponse.getString("selfLink"), apiResponse.getString("selfLink")); Assert.assertEquals(esbResponse.getJSONObject("query").getString("start-date"), apiResponse.getJSONObject("query").getString("start-date")); Assert.assertEquals(esbResponse.getJSONObject("query").getString("end-date"), apiResponse.getJSONObject("query").getString("end-date")); }
From source file:org.wso2.carbon.connector.integration.test.googleanalytics.GoogleanalyticsConnectorIntegrationTest.java
/** * Positive test case for getReportData method with optional parameters. * * @throws org.json.JSONException/*from ww w.j a va 2 s . c o m*/ * @throws java.io.IOException */ @Test(groups = { "wso2.esb" }, description = "googleanalytics {getReportData} integration test with optional parameters.", dependsOnMethods = { "testListAccountSummariesWithMandatoryParameters" }) public void testGetReportDataWithOptionalParameters() throws IOException, JSONException { esbRequestHeadersMap.put("Action", "urn:getReportData"); RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap, "esb_getReportData_optional.json"); final JSONObject esbResponse = esbRestResponse.getBody(); final String apiEndpoint = apiEndpointUrl + "/data/ga?ids=" + connectorProperties.getProperty("coreReportIds") + "&start-date=" + connectorProperties.getProperty("coreReportStartDate") + "&end-date=" + connectorProperties.getProperty("coreReportEndDate") + "&metrics=" + connectorProperties.getProperty("coreReportMetrics") + "&max-results=" + connectorProperties.getProperty("coreReportMaxResults") + "&start-index=" + connectorProperties.getProperty("coreReportStartIndex"); RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndpoint, "GET", apiRequestHeadersMap); final JSONObject apiResponse = apiRestResponse.getBody(); Assert.assertEquals(esbResponse.getString("kind"), apiResponse.getString("kind")); Assert.assertEquals(esbResponse.getString("id"), apiResponse.getString("id")); Assert.assertEquals(esbResponse.getString("selfLink"), apiResponse.getString("selfLink")); Assert.assertEquals(connectorProperties.getProperty("coreReportMaxResults"), apiResponse.getJSONObject("query").getString("max-results")); Assert.assertEquals(connectorProperties.getProperty("coreReportStartIndex"), apiResponse.getJSONObject("query").getString("start-index")); }
From source file:org.wso2.carbon.connector.integration.test.googleanalytics.GoogleanalyticsConnectorIntegrationTest.java
/** * Positive test case for createAccountUserLink method with mandatory parameters. * * @throws org.json.JSONException// ww w.ja va 2 s . co m * @throws java.io.IOException */ @Test(groups = { "wso2.esb" }, description = "googleanalytics {createAccountUserLink} integration test with mandatory parameters.", dependsOnMethods = { "testListAccountSummariesWithMandatoryParameters" }) public void testCreateAccountUserLinkWithMandatoryParameters() throws IOException, JSONException { esbRequestHeadersMap.put("Action", "urn:createAccountUserLink"); RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap, "esb_createAccountUserLink_mandatory.json"); final JSONObject esbResponse = esbRestResponse.getBody(); final String userLinkId = esbResponse.getString("id"); connectorProperties.put("userLinkId", userLinkId); final String apiEndpoint = apiEndpointUrl + "/management/accounts/" + connectorProperties.getProperty("accountId") + "/entityUserLinks"; RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndpoint, "GET", apiRequestHeadersMap); final JSONArray itemArray = apiRestResponse.getBody().getJSONArray("items"); String id; String email = ""; String permissions = ""; String selfLink = ""; for (int i = 0; i < itemArray.length(); i++) { id = itemArray.getJSONObject(i).getString("id"); if (id.equals(userLinkId)) { permissions = itemArray.getJSONObject(i).getJSONObject("permissions").toString(); email = itemArray.getJSONObject(i).getJSONObject("userRef").getString("email"); selfLink = itemArray.getJSONObject(i).getString("selfLink"); break; } } Assert.assertEquals(connectorProperties.getProperty("userLinkEmail"), email); Assert.assertEquals(esbResponse.getJSONObject("permissions").toString(), permissions); Assert.assertEquals(esbResponse.getString("selfLink"), selfLink); }
From source file:org.wso2.carbon.connector.integration.test.googleanalytics.GoogleanalyticsConnectorIntegrationTest.java
/** * Positive test case for updateAccountUserLink method with mandatory parameters. * * @throws org.json.JSONException/*from w w w . j a v a 2 s. com*/ * @throws java.io.IOException */ @Test(groups = { "wso2.esb" }, description = "googleanalytics {updateAccountUserLink} integration test with mandatory parameters.", dependsOnMethods = { "testCreateAccountUserLinkWithMandatoryParameters", "testListAccountSummariesWithMandatoryParameters" }) public void testUpdateAccountUserLinkWithMandatoryParameters() throws IOException, JSONException { final String apiEndpoint = apiEndpointUrl + "/management/accounts/" + connectorProperties.getProperty("accountId") + "/entityUserLinks"; RestResponse<JSONObject> apiRestResponseBefore = sendJsonRestRequest(apiEndpoint, "GET", apiRequestHeadersMap); final String userLinkId = connectorProperties.getProperty("userLinkId"); final JSONArray itemArrayBefore = apiRestResponseBefore.getBody().getJSONArray("items"); String id; String permissions = ""; for (int i = 0; i < itemArrayBefore.length(); i++) { id = itemArrayBefore.getJSONObject(i).getString("id"); if (id.equals(userLinkId)) { permissions = itemArrayBefore.getJSONObject(i).getJSONObject("permissions").toString(); break; } } esbRequestHeadersMap.put("Action", "urn:updateAccountUserLink"); RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap, "esb_updatedAccountUserLink_mandatory.json"); final JSONObject esbResponse = esbRestResponse.getBody(); RestResponse<JSONObject> apiRestResponseAfter = sendJsonRestRequest(apiEndpoint, "GET", apiRequestHeadersMap); final JSONArray itemArrayAfter = apiRestResponseAfter.getBody().getJSONArray("items"); String idAfter; String permissionsAfter = ""; for (int i = 0; i < itemArrayAfter.length(); i++) { idAfter = itemArrayAfter.getJSONObject(i).getString("id"); if (idAfter.equals(userLinkId)) { permissionsAfter = itemArrayAfter.getJSONObject(i).getJSONObject("permissions").toString(); break; } } Assert.assertNotEquals(permissions, permissionsAfter); Assert.assertEquals(esbResponse.getJSONObject("permissions").toString(), permissionsAfter); }
From source file:org.wso2.carbon.connector.integration.test.googleanalytics.GoogleanalyticsConnectorIntegrationTest.java
/** * Positive test case for listMultiChannelFunnelsReportData method with mandatory parameters. * * @throws org.json.JSONException/*from w w w. java 2 s . co m*/ * @throws java.io.IOException */ @Test(groups = { "wso2.esb" }, description = "googleanalytics {listMultiChannelFunnelsReportData} integration test with mandatory parameters.", dependsOnMethods = { "testListAccountSummariesWithMandatoryParameters" }) public void testListMultiChannelFunnelReportDataWithMandatoryParameters() throws IOException, JSONException { esbRequestHeadersMap.put("Action", "urn:listMultiChannelFunnelsReportData"); RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap, "esb_listMultiChannelFunnelData_mandatory.json"); final JSONObject esbResponse = esbRestResponse.getBody(); final String apiEndpoint = apiEndpointUrl + "/data/mcf?ids=" + connectorProperties.getProperty("multiChannelIds") + "&start-date=" + connectorProperties.getProperty("multiChannelStartDate") + "&end-date=" + connectorProperties.getProperty("multiChannelEndDate") + "&metrics=" + connectorProperties.getProperty("multiChannelMetrics"); RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndpoint, "GET", apiRequestHeadersMap); final JSONObject apiResponse = apiRestResponse.getBody(); Assert.assertEquals(esbResponse.getString("kind"), apiResponse.getString("kind")); Assert.assertEquals(esbResponse.getJSONObject("query").getString("start-date"), apiResponse.getJSONObject("query").getString("start-date")); Assert.assertEquals(esbResponse.getJSONObject("query").getString("end-date"), apiResponse.getJSONObject("query").getString("end-date")); Assert.assertEquals(esbResponse.getString("itemsPerPage"), apiResponse.getString("itemsPerPage")); Assert.assertEquals(esbResponse.getString("totalResults"), apiResponse.getString("totalResults")); }
From source file:org.wso2.carbon.connector.integration.test.googleanalytics.GoogleanalyticsConnectorIntegrationTest.java
/** * Positive test case for listMultiChannelFunnelsReportData method with optional parameters. * * @throws org.json.JSONException/* www . j a va2 s . c om*/ * @throws java.io.IOException */ @Test(groups = { "wso2.esb" }, description = "googleanalytics {listMultiChannelFunnelsReportData} integration test with optional parameters.", dependsOnMethods = { "testListAccountSummariesWithMandatoryParameters" }) public void testListMultiChannelFunnelReportDataWithOptionalParameters() throws IOException, JSONException { esbRequestHeadersMap.put("Action", "urn:listMultiChannelFunnelsReportData"); RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap, "esb_listMultiChannelFunnelData_optional.json"); final JSONObject esbResponse = esbRestResponse.getBody(); final String apiEndpoint = apiEndpointUrl + "/data/mcf?ids=" + connectorProperties.getProperty("multiChannelIds") + "&start-date=" + connectorProperties.getProperty("multiChannelStartDate") + "&end-date=" + connectorProperties.getProperty("multiChannelEndDate") + "&metrics=" + connectorProperties.getProperty("multiChannelMetrics") + "&max-results=" + connectorProperties.getProperty("coreReportMaxResults") + "&start-index=" + connectorProperties.getProperty("coreReportStartIndex"); ; RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndpoint, "GET", apiRequestHeadersMap); final JSONObject apiResponse = apiRestResponse.getBody(); Assert.assertEquals(esbResponse.getString("kind"), apiResponse.getString("kind")); Assert.assertEquals(connectorProperties.getProperty("coreReportMaxResults"), apiResponse.getJSONObject("query").getString("max-results")); Assert.assertEquals(connectorProperties.getProperty("coreReportStartIndex"), apiResponse.getJSONObject("query").getString("start-index")); Assert.assertEquals(esbResponse.getString("itemsPerPage"), apiResponse.getString("itemsPerPage")); Assert.assertEquals(esbResponse.getString("totalResults"), apiResponse.getString("totalResults")); }