Example usage for org.json JSONArray getJSONObject

List of usage examples for org.json JSONArray getJSONObject

Introduction

In this page you can find the example usage for org.json JSONArray getJSONObject.

Prototype

public JSONObject getJSONObject(int index) throws JSONException 

Source Link

Document

Get the JSONObject associated with an index.

Usage

From source file:com.roiland.crm.sm.core.service.impl.ContacterAPIImpl.java

@SuppressWarnings("unchecked")
@Override//from   www .  jav a 2 s . c o  m
public List<Contacter> getContacterList(String userID, String dealerOrgID, String projectID, String customerID)
        throws ResponseException {
    ReleasableList<Contacter> contacterList = null;
    try {

        // ??.
        if (userID == null || dealerOrgID == null) {
            throw new ResponseException("userID or dealerOrgID is null.");
        }

        JSONObject params = new JSONObject();
        params.put("userID", userID);
        params.put("dealerOrgID", dealerOrgID);
        params.put("projectID", projectID);
        params.put("customerID", customerID);

        // ?Key
        String key = getKey(URLContact.METHOD_GET_CONTACTER_LIST, params);

        // ????
        contacterList = lruCache.get(key);
        if (contacterList != null && !contacterList.isExpired()) {
            return contacterList;
        }

        RLHttpResponse response = getHttpClient()
                .executePostJSON(getURLAddress(URLContact.METHOD_GET_CONTACTER_LIST), params, null);
        if (response.isSuccess()) {
            contacterList = new ArrayReleasableList<Contacter>();
            String data = getSimpleString(response);
            Log.i("getContacterList", data);
            JSONObject jsonBean = new JSONObject(data);
            JSONArray contacter = jsonBean.getJSONArray("result");
            int n = contacter.length();
            for (int i = 0; i < n; i++) {
                JSONObject json = contacter.getJSONObject(i);
                Contacter resultContacter = new Contacter();
                resultContacter.setContacterID(json.getString("contacterID"));
                resultContacter.setProjectID(String.valueOf(json.getString("projectID")));
                resultContacter.setCustomerID(String.valueOf(json.getString("customerID")));
                resultContacter.setContName(parsingString(json.get("contName")));
                resultContacter.setContMobile(parsingString(json.get("contMobile")));
                resultContacter.setContOtherPhone(parsingString(json.get("contOtherPhone")));
                resultContacter.setIsPrimContanter(String.valueOf(json.getBoolean("isPrimContanter")));
                resultContacter.setContGenderCode(parsingString(json.get("contGenderCode")));
                resultContacter.setContGender(parsingString(json.get("contGender")));
                resultContacter
                        .setContBirthday(String.valueOf(DataVerify.isZero(json.getString("contBirthday"))));
                resultContacter.setIdNumber(parsingString(json.get("idNumber")));
                resultContacter.setAgeScopeCode(parsingString(json.get("ageScopeCode")));
                resultContacter.setAgeScope(parsingString(json.get("ageScope")));
                resultContacter.setContType(parsingString(json.get("contType")));
                resultContacter.setContTypeCode(String.valueOf(json.getString("contTypeCode")));
                resultContacter.setContRelationCode(String.valueOf(json.getString("contRelationCode")));
                resultContacter.setContRelation(parsingString(json.get("contRelation")));
                resultContacter
                        .setLicenseValid(StringUtils.toLong(DataVerify.isZero(json.getString("licenseValid"))));
                contacterList.add(resultContacter);

            }
            // ?
            if (contacterList != null) {
                lruCache.put(key, contacterList);
            }
            return contacterList;
        }
        throw new ResponseException();
    } catch (IOException e) {
        Log.e(tag, "Connection network error.", e);
        throw new ResponseException(e);
    } catch (JSONException e) {
        Log.e(tag, "Parsing data error.", e);
        throw new ResponseException(e);
    }

}

From source file:com.roiland.crm.sm.core.service.impl.ContacterAPIImpl.java

@Override
public List<Dictionary> getEmployeeList() throws ResponseException {
    List<Dictionary> employeeList = null;
    try {/*from w  w  w.ja  v a  2s .co m*/
        JSONObject params = new JSONObject();
        params.put("status", "3");
        RLHttpResponse response = getHttpClient()
                .executePostJSON(getURLAddress(URLContact.METHOD_GET_EMPLOYEE_LIST), params, null);
        if (response.isSuccess()) {
            employeeList = new ArrayList<Dictionary>();
            JSONObject result = new JSONObject(getSimpleString(response));
            JSONArray array = result.getJSONArray("list");
            for (int i = 0; i < array.length(); i++) {
                Dictionary dic = new Dictionary();
                JSONObject object = array.getJSONObject(i);
                dic.setDicKey(object.getString("id"));
                dic.setDicValue(object.getString("name"));
                employeeList.add(dic);
            }
        }
    } catch (IOException e) {
        Log.e(tag, "Parsing data error.", e);
        throw new ResponseException(e);
    } catch (JSONException e) {
        Log.e(tag, "Parsing data error.", e);
        throw new ResponseException(e);
    }

    return employeeList;
}

From source file:org.wso2.carbon.connector.integration.test.nexmo.NexmoConnectorIntegrationTest.java

/**
 * Positive test case for getOutboundPricing method with mandatory parameters.
 *///ww  w.  java  2  s .co  m
@Test(priority = 1, description = "nexmo {getOutboundPricing} integration test with mandatory parameters.")
public void testGetOutboundPricingWithMandatoryParameters() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:getOutboundPricing");

    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_getOutboundPricing_mandatory.json");

    JSONArray esbResponseArray = esbRestResponse.getBody().getJSONArray("networks");

    String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/account/get-pricing/outbound?api_key="
            + connectorProperties.getProperty("apiKey") + "&api_secret="
            + connectorProperties.getProperty("apiSecret") + "&country=US";

    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);

    JSONArray apiResponseArray = apiRestResponse.getBody().getJSONArray("networks");

    Assert.assertEquals(esbRestResponse.getBody().getString("country"),
            apiRestResponse.getBody().getString("country"));
    Assert.assertEquals(esbRestResponse.getBody().getString("name"),
            apiRestResponse.getBody().getString("name"));
    Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("code"),
            apiResponseArray.getJSONObject(0).getString("code"));
}

From source file:org.wso2.carbon.connector.integration.test.nexmo.NexmoConnectorIntegrationTest.java

/**
 * Positive test case for sendMessage method with mandatory parameters.
 *///  w  w w. jav a2  s . c  om
@Test(priority = 1, description = "nexmo {sendMessage} integration test with mandatory parameters.")
public void testSendMessageWithMandatoryParameters() throws IOException, JSONException, InterruptedException {

    esbRequestHeadersMap.put("Action", "urn:sendMessage");

    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_sendMessage_mandatory.json");

    JSONArray esbMessagesArray = esbRestResponse.getBody().getJSONArray("messages");
    String messageId = esbMessagesArray.getJSONObject(0).getString("message-id");
    connectorProperties.put("messageId", messageId);

    String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/search/message?api_key="
            + connectorProperties.getProperty("apiKey") + "&api_secret="
            + connectorProperties.getProperty("apiSecret") + "&id=" + messageId;

    Thread.sleep(Long.parseLong(connectorProperties.getProperty("timeOut")));
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);
    Assert.assertEquals(esbRestResponse.getBody().getInt("message-count"), 1);
    Assert.assertEquals(esbMessagesArray.getJSONObject(0).getString("to"),
            apiRestResponse.getBody().getString("to"));
    Assert.assertEquals(esbMessagesArray.getJSONObject(0).getString("message-price"),
            apiRestResponse.getBody().getString("price"));
    Assert.assertTrue(
            apiRestResponse.getBody().getString("body").contains(connectorProperties.getProperty("message")));

}

From source file:org.wso2.carbon.connector.integration.test.nexmo.NexmoConnectorIntegrationTest.java

/**
 * Positive test case for sendMessage method with optional parameters.
 *//*from w  ww .jav a  2 s.  c o m*/
@Test(dependsOnMethods = {
        "testSendMessageWithMandatoryParameters" }, description = "nexmo {sendMessage} integration test with optional parameters.")
public void testSendMessageWithOptionalParameters() throws IOException, JSONException, InterruptedException {

    esbRequestHeadersMap.put("Action", "urn:sendMessage");

    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_sendMessage_optional.json");

    JSONArray esbMessagesArray = esbRestResponse.getBody().getJSONArray("messages");
    String messageId2 = esbMessagesArray.getJSONObject(0).getString("message-id");
    connectorProperties.put("messageId2", messageId2);

    String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/search/message?api_key="
            + connectorProperties.getProperty("apiKey") + "&api_secret="
            + connectorProperties.getProperty("apiSecret") + "&id=" + messageId2;

    Thread.sleep(Long.parseLong(connectorProperties.getProperty("timeOut")));
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);

    Assert.assertEquals(esbRestResponse.getBody().getInt("message-count"), 1);
    Assert.assertEquals(esbMessagesArray.getJSONObject(0).getString("to"),
            apiRestResponse.getBody().getString("to"));
    Assert.assertEquals(esbMessagesArray.getJSONObject(0).getString("client-ref"),
            connectorProperties.getProperty("clientRef"));
    Assert.assertTrue(
            apiRestResponse.getBody().getString("body").contains(connectorProperties.getProperty("message")));
}

From source file:org.wso2.carbon.connector.integration.test.nexmo.NexmoConnectorIntegrationTest.java

/**
 * Negative test case for sendMessage method.
 *///from w w  w. ja  va2  s  . co  m
@Test(dependsOnMethods = {
        "testSendMessageWithOptionalParameters" }, description = "nexmo {sendMessage} integration test with negative cases.")
public void testSendMessageWithNegativeCase() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:sendMessage");

    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_sendMessage_negative.json");

    String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/sms/json";

    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "POST", apiRequestHeadersMap,
            "api_sendMessage_negative.json");

    JSONArray esbMessagesArray = esbRestResponse.getBody().getJSONArray("messages");
    JSONArray apiMessagesArray = apiRestResponse.getBody().getJSONArray("messages");

    Assert.assertEquals(esbMessagesArray.getJSONObject(0).getString("status"),
            apiMessagesArray.getJSONObject(0).getString("status"));
    Assert.assertEquals(esbMessagesArray.getJSONObject(0).getString("error-text"),
            apiMessagesArray.getJSONObject(0).getString("error-text"));

}

From source file:org.wso2.carbon.connector.integration.test.nexmo.NexmoConnectorIntegrationTest.java

/**
 * Positive test case for searchMessages method with mandatory parameters.
 */// www.j a v  a  2s  . co m
@Test(dependsOnMethods = {
        "testSendMessageWithNegativeCase" }, description = "nexmo {searchMessages} integration test with mandatory parameters.")
public void testSearchMessagesWithMandatoryParameters() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:searchMessages");

    final String currentDateString = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    connectorProperties.setProperty("currentDateString", currentDateString);

    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_searchMessages_mandatory.json");

    String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/search/messages?api_key="
            + connectorProperties.getProperty("apiKey") + "&api_secret="
            + connectorProperties.getProperty("apiSecret") + "&date=" + currentDateString + "&to="
            + connectorProperties.getProperty("recipientPhoneNumber");

    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);

    JSONArray esbItemsArray = esbRestResponse.getBody().getJSONArray("items");
    JSONArray apiItemsArray = apiRestResponse.getBody().getJSONArray("items");

    Assert.assertEquals(esbRestResponse.getBody().getString("count"),
            apiRestResponse.getBody().getString("count"));
    if (apiItemsArray.length() > 0 && esbItemsArray.length() > 0) {
        Assert.assertEquals(esbItemsArray.length(), apiItemsArray.length());
        Assert.assertEquals(esbItemsArray.getJSONObject(0).getString("body"),
                apiItemsArray.getJSONObject(0).getString("body"));
        Assert.assertEquals(esbItemsArray.getJSONObject(0).getString("message-id"),
                apiItemsArray.getJSONObject(0).getString("message-id"));
    } else {
        Assert.assertTrue(false);
    }
}

From source file:org.wso2.carbon.connector.integration.test.nexmo.NexmoConnectorIntegrationTest.java

/**
 * Positive test case for searchMessages method with optional parameters.
 *//*from w w w .  ja va2  s  .  c o m*/
@Test(dependsOnMethods = {
        "testSendMessageWithOptionalParameters" }, description = "nexmo {searchMessages} integration test with optional parameters.")
public void testSearchMessagesWithOptionalParameters() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:searchMessages");

    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_searchMessages_optional.json");

    String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/search/messages?api_key="
            + connectorProperties.getProperty("apiKey") + "&api_secret="
            + connectorProperties.getProperty("apiSecret") + "&ids="
            + connectorProperties.getProperty("messageId") + "&ids="
            + connectorProperties.getProperty("messageId2");

    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);

    JSONArray esbItemsArray = esbRestResponse.getBody().getJSONArray("items");
    JSONArray apiItemsArray = apiRestResponse.getBody().getJSONArray("items");

    Assert.assertEquals(esbRestResponse.getBody().getString("count"),
            apiRestResponse.getBody().getString("count"));
    if (apiItemsArray.length() > 0 && esbItemsArray.length() > 0) {
        Assert.assertEquals(esbItemsArray.length(), apiItemsArray.length());
        Assert.assertEquals(esbItemsArray.getJSONObject(0).getString("body"),
                apiItemsArray.getJSONObject(0).getString("body"));
        Assert.assertEquals(esbItemsArray.getJSONObject(0).getString("message-id"),
                apiItemsArray.getJSONObject(0).getString("message-id"));
    } else {
        Assert.assertTrue(false);
    }
}

From source file:com.poguico.palmabici.parsers.Parser.java

public static ArrayList<Station> parseNetworkJSON(String data) {
    ArrayList<Station> stations = new ArrayList<Station>();
    JSONArray jsonArray;
    JSONObject jsonObject;//from  w ww .j  a  va2  s .c om
    Long lngAcum = 0L, latAcum = 0L;
    int id;

    try {
        jsonArray = new JSONArray(data);

        for (int i = 0; i < jsonArray.length(); i++) {
            jsonObject = jsonArray.getJSONObject(i);

            lngAcum += jsonObject.getLong("lng");
            latAcum += jsonObject.getLong("lat");

            id = jsonObject.getInt("id");
            stations.add(new Station(id, jsonObject.getString("name"), jsonObject.getDouble("lng") / 1e6,
                    jsonObject.getDouble("lat") / 1e6, jsonObject.getInt("free"), jsonObject.getInt("bikes"),
                    false, NetworkStationAlarm.hasAlarm(id)));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return stations;
}